Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
55456d37
提交
55456d37
authored
8月 28, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
63caa729
隐藏空白字符变更
内嵌
并排
正在显示
14 个修改的文件
包含
335 行增加
和
353 行删除
+335
-353
memory.go
cache/memory.go
+1
-1
ctx.go
gin/ctxUtils/ctx.go
+0
-307
auth.go
gin/middleware/auth.go
+1
-1
logging.go
gin/middleware/logging.go
+1
-1
logging_test.go
gin/middleware/logging_test.go
+1
-1
recovery.go
gin/middleware/recovery.go
+1
-1
requstid.go
gin/middleware/requstid.go
+19
-29
sync_ratelimiter.go
gin/middleware/sync_ratelimiter/sync_ratelimiter.go
+1
-1
response.go
gin/response/response.go
+1
-1
logger.go
goredis/logger.go
+1
-1
entity.go
httpcli/entity/entity.go
+1
-1
httpcli.go
httpcli/httpcli.go
+1
-1
ctxUtil.go
utils/ctxUtils/ctxUtil.go
+305
-6
wg.go
utils/wg/wg.go
+1
-1
没有找到文件。
cache/memory.go
浏览文件 @
55456d37
...
...
@@ -11,8 +11,8 @@ import (
"github.com/spf13/cast"
"gitlab.wanzhuangkj.com/tush/xpkg/encoding"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
xslice
"gitlab.wanzhuangkj.com/tush/xpkg/utils/sliceUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
)
...
...
gin/ctxUtils/ctx.go
deleted
100644 → 0
浏览文件 @
63caa729
package
ctxUtils
import
(
"context"
"errors"
"fmt"
"strings"
"time"
xsf
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"go.uber.org/zap"
)
const
(
GinContextKey
=
"ginContext"
HeaderXTimestampKey
=
"Timestamp"
KeyReqBody
=
"reqBody"
KeyRspBody
=
"rspBody"
KeyRspCode
=
"rspCode"
KeyOApiReqBody
=
"oApiReqBody%s"
KeyOApiRspBody
=
"oApiRspBody%s"
KeyAppName
=
"appName"
KeyUser
=
"user"
KeyApiStartTime
=
"apiStartTime"
KeyClientIP
=
"clientIP"
KeyUID
=
"uid"
KeyUType
=
"uType"
KeyCompanyID
=
"companyID"
KeyShopID
=
"shopID"
KeyUName
=
"uname"
KeyToken
=
"token"
KeyCost
=
"X-Request-Cost"
KeyRspBodyMax
=
"rsp-body-max"
)
var
(
HeaderXRequestIDKey
=
"X-Request-ID"
ContextTraceIDKey
=
"X-Request-ID"
)
var
(
ErrorGinContextNotFound
=
errors
.
New
(
"gin context not found"
)
)
func
Set
(
c
*
gin
.
Context
,
key
string
,
val
any
)
{
c
.
Set
(
key
,
val
)
}
func
SetRspBodyMax
(
ctx
context
.
Context
,
max
int
)
{
if
c
,
_
:=
ToGinCtx
(
ctx
);
c
!=
nil
{
Set
(
c
,
KeyRspBodyMax
,
max
)
}
}
func
Get
[
T
any
](
c
*
gin
.
Context
,
key
string
)
(
T
,
bool
)
{
if
val
,
ok
:=
c
.
Get
(
key
);
ok
{
if
v
,
ok1
:=
val
.
(
T
);
ok1
{
return
v
,
true
}
}
var
a
T
return
a
,
false
}
// GetGinCtxTraceID get request id from gin.Context
func
GetGinCtxTraceID
(
c
*
gin
.
Context
)
string
{
if
v
,
isExist
:=
c
.
Get
(
ContextTraceIDKey
);
isExist
{
if
requestID
,
ok
:=
v
.
(
string
);
ok
{
return
requestID
}
}
return
""
}
// GinTraceIDField get request id field from gin.Context
func
GinTraceIDField
(
c
*
gin
.
Context
)
zap
.
Field
{
return
zap
.
String
(
ContextTraceIDKey
,
GetGinCtxTraceID
(
c
))
}
// CtxRequestID get request id from context.Context
func
CtxRequestID
(
c
context
.
Context
)
string
{
v
:=
c
.
Value
(
ContextTraceIDKey
)
if
str
,
ok
:=
v
.
(
string
);
ok
{
return
str
}
return
""
}
// CtxXRequestID get request id from context.Context
func
CtxXRequestID
(
c
context
.
Context
)
string
{
v
:=
c
.
Value
(
HeaderXRequestIDKey
)
if
str
,
ok
:=
v
.
(
string
);
ok
{
return
str
}
return
""
}
func
CtxGetGinCtx
(
c
context
.
Context
)
*
gin
.
Context
{
if
str
,
ok
:=
c
.
Value
(
GinContextKey
)
.
(
*
gin
.
Context
);
ok
{
return
str
}
return
nil
}
// CtxTraceIDField get request id field from context.Context
func
CtxTraceIDField
(
c
context
.
Context
)
zap
.
Field
{
return
zap
.
String
(
ContextTraceIDKey
,
CtxRequestID
(
c
))
}
func
NewEmptyCtx
()
context
.
Context
{
return
context
.
WithValue
(
context
.
Background
(),
ContextTraceIDKey
,
GenerateTid
())
}
func
NewCtx
(
ctx
context
.
Context
)
context
.
Context
{
return
context
.
WithValue
(
context
.
Background
(),
ContextTraceIDKey
,
ctx
.
Value
(
ContextTraceIDKey
))
}
func
GetGinUserID
(
c
*
gin
.
Context
)
xsf
.
ID
{
return
getGinVal
[
xsf
.
ID
](
c
,
KeyUID
)
}
func
GetGinShopID
(
c
*
gin
.
Context
)
xsf
.
ID
{
return
getGinVal
[
xsf
.
ID
](
c
,
KeyShopID
)
}
func
GetCtxUserID
(
c
context
.
Context
)
xsf
.
ID
{
return
getCtxVal
[
xsf
.
ID
](
c
,
KeyUID
)
}
func
GetCtxShopID
(
c
context
.
Context
)
xsf
.
ID
{
return
getCtxVal
[
xsf
.
ID
](
c
,
KeyShopID
)
}
func
GetCtxString
(
c
context
.
Context
,
key
string
)
string
{
val
:=
c
.
Value
(
key
)
if
val
!=
nil
{
return
cast
.
ToString
(
val
)
}
return
""
}
func
GetCtxUserToken
(
c
context
.
Context
)
string
{
token
:=
c
.
Value
(
KeyToken
)
if
token
!=
nil
{
return
cast
.
ToString
(
token
)
}
return
""
}
func
GetGinCtxTid
(
c
*
gin
.
Context
)
string
{
tid
:=
c
.
GetString
(
ContextTraceIDKey
)
if
tid
==
""
{
tid
=
GenerateTid
()
c
.
Set
(
ContextTraceIDKey
,
tid
)
}
return
tid
}
func
GenerateTid
()
string
{
return
xsf
.
GenerateID
()
.
String
()
}
func
GetCtxTid
(
ctx
context
.
Context
)
string
{
tid
:=
""
tidVal
:=
ctx
.
Value
(
ContextTraceIDKey
)
if
tidVal
!=
nil
{
if
str
,
ok
:=
tidVal
.
(
string
);
ok
{
tid
=
str
}
}
if
tid
==
""
{
tid
=
GenerateTid
()
}
return
tid
}
// GetClientIP get ip from context.Context, the client ip is set in gin.Context
func
GetClientIP
(
ctx
context
.
Context
)
string
{
ip
:=
""
ipVal
:=
ctx
.
Value
(
KeyClientIP
)
if
ipVal
!=
nil
{
if
str
,
ok
:=
ipVal
.
(
string
);
ok
{
ip
=
str
}
}
return
ip
}
func
WrapCtx
(
c
*
gin
.
Context
)
context
.
Context
{
ctx
:=
context
.
WithValue
(
c
.
Request
.
Context
(),
ContextTraceIDKey
,
c
.
GetString
(
ContextTraceIDKey
))
for
k
,
v
:=
range
c
.
Keys
{
ctx
=
context
.
WithValue
(
ctx
,
k
,
v
)
}
ctx
=
context
.
WithValue
(
ctx
,
GinContextKey
,
c
)
//nolint
return
ctx
}
func
ToGinCtx
(
ctx
context
.
Context
)
(
*
gin
.
Context
,
error
)
{
if
c
,
ok
:=
ctx
.
Value
(
GinContextKey
)
.
(
*
gin
.
Context
);
ok
{
return
c
,
nil
}
return
nil
,
ErrorGinContextNotFound
}
func
GetGinCtx
(
ctx
context
.
Context
)
(
*
gin
.
Context
,
error
)
{
if
c
,
ok
:=
ctx
.
Value
(
GinContextKey
)
.
(
*
gin
.
Context
);
ok
{
return
c
,
nil
}
return
nil
,
ErrorGinContextNotFound
}
func
GetCtxUID
(
c
context
.
Context
)
uint
{
return
getCtxVal
[
uint
](
c
,
KeyUID
)
}
func
GetCtxUType
(
c
context
.
Context
)
uint
{
return
getCtxVal
[
uint
](
c
,
KeyUType
)
}
func
GetCtxCompanyID
(
c
context
.
Context
)
uint
{
return
getCtxVal
[
uint
](
c
,
KeyCompanyID
)
}
func
getGinVal
[
T
any
](
c
*
gin
.
Context
,
key
string
)
T
{
val
,
exists
:=
c
.
Get
(
key
)
if
exists
{
if
ret
,
ok
:=
val
.
(
T
);
ok
{
return
ret
}
}
var
t
T
return
t
}
func
getCtxVal
[
T
any
](
c
context
.
Context
,
key
string
)
T
{
val
:=
c
.
Value
(
key
)
if
val
!=
nil
{
if
ret
,
ok
:=
val
.
(
T
);
ok
{
return
ret
}
}
var
t
T
return
t
}
func
GetGinReq
(
c
*
gin
.
Context
)
[]
byte
{
v
,
ok1
:=
c
.
Get
(
KeyReqBody
)
if
ok1
{
ret
,
ok2
:=
v
.
([]
byte
)
if
ok2
{
return
ret
}
}
return
nil
}
func
GetGinRsp
(
c
*
gin
.
Context
)
[]
byte
{
v
,
ok1
:=
c
.
Get
(
KeyRspBody
)
if
ok1
{
ret
,
ok2
:=
v
.
([]
byte
)
if
ok2
{
return
ret
}
}
return
nil
}
func
SetVal
(
ctx
context
.
Context
,
key
string
,
val
interface
{})
context
.
Context
{
if
ginCtx
,
err
:=
GetGinCtx
(
ctx
);
err
==
nil
&&
ginCtx
!=
nil
{
ginCtx
.
Set
(
key
,
val
)
}
return
context
.
WithValue
(
ctx
,
key
,
val
)
}
func
AddApiCost
(
c
*
gin
.
Context
,
appName
string
,
start
time
.
Time
)
{
cost
:=
c
.
Writer
.
Header
()
.
Get
(
KeyCost
)
currentCost
:=
fmt
.
Sprintf
(
"%s:%s"
,
appName
,
formatDuration
(
time
.
Since
(
start
)))
if
cost
==
""
{
cost
=
currentCost
}
else
{
cost
=
fmt
.
Sprintf
(
"%s,%s"
,
currentCost
,
cost
)
}
c
.
Writer
.
Header
()
.
Set
(
KeyCost
,
cost
)
}
func
formatDuration
(
d
time
.
Duration
)
string
{
ms
:=
float64
(
d
.
Nanoseconds
())
/
1e6
return
fmt
.
Sprintf
(
"%.2fms"
,
ms
)
}
func
SetApiCost
(
ctx
context
.
Context
,
header
map
[
string
][]
string
)
{
if
ginCtx
,
_
:=
GetGinCtx
(
ctx
);
ginCtx
!=
nil
&&
len
(
header
)
>
0
{
cost
:=
""
for
k
,
v
:=
range
header
{
if
k
==
KeyCost
{
cost
=
strings
.
Join
(
v
,
","
)
}
}
if
cost
!=
""
{
ginCtx
.
Writer
.
Header
()
.
Set
(
KeyCost
,
cost
)
}
}
}
gin/middleware/auth.go
浏览文件 @
55456d37
...
...
@@ -6,10 +6,10 @@ import (
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/response"
"gitlab.wanzhuangkj.com/tush/xpkg/jwt"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
)
...
...
gin/middleware/logging.go
浏览文件 @
55456d37
...
...
@@ -8,8 +8,8 @@ import (
"strings"
"time"
ctxUtils
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/ips"
ctxUtils
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
...
...
gin/middleware/logging_test.go
浏览文件 @
55456d37
...
...
@@ -7,11 +7,11 @@ import (
"github.com/gin-gonic/gin"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/response"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
)
func
init
()
{
...
...
gin/middleware/recovery.go
浏览文件 @
55456d37
...
...
@@ -3,7 +3,7 @@ package middleware
import
(
"net/http"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/
gin
/ctxUtils"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/
utils
/ctxUtils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
...
...
gin/middleware/requstid.go
浏览文件 @
55456d37
...
...
@@ -3,7 +3,7 @@ package middleware
import
(
"context"
"github.com/gin-gonic/gin"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin
/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils
/ctxUtils"
"go.uber.org/zap"
"net/http"
"time"
...
...
@@ -20,8 +20,8 @@ type requestIDOptions struct {
func
defaultRequestIDOptions
()
*
requestIDOptions
{
return
&
requestIDOptions
{
contextRequestIDKey
:
ctxUtil
.
ContextTraceIDKey
,
headerXRequestIDKey
:
ctxUtil
.
HeaderXRequestIDKey
,
contextRequestIDKey
:
ctxUtil
s
.
ContextTraceIDKey
,
headerXRequestIDKey
:
ctxUtil
s
.
HeaderXRequestIDKey
,
}
}
...
...
@@ -32,11 +32,11 @@ func (o *requestIDOptions) apply(opts ...RequestIDOption) {
}
func
(
o
*
requestIDOptions
)
setRequestIDKey
()
{
if
o
.
contextRequestIDKey
!=
ctxUtil
.
ContextTraceIDKey
{
ctxUtil
.
ContextTraceIDKey
=
o
.
contextRequestIDKey
if
o
.
contextRequestIDKey
!=
ctxUtil
s
.
ContextTraceIDKey
{
ctxUtil
s
.
ContextTraceIDKey
=
o
.
contextRequestIDKey
}
if
o
.
headerXRequestIDKey
!=
ctxUtil
.
HeaderXRequestIDKey
{
ctxUtil
.
HeaderXRequestIDKey
=
o
.
headerXRequestIDKey
if
o
.
headerXRequestIDKey
!=
ctxUtil
s
.
HeaderXRequestIDKey
{
ctxUtil
s
.
HeaderXRequestIDKey
=
o
.
headerXRequestIDKey
}
}
...
...
@@ -64,7 +64,7 @@ func WithHeaderRequestIDKey(key string) RequestIDOption {
type
CtxKeyString
string
// RequestIDKey request_id
var
RequestIDKey
=
CtxKeyString
(
ctxUtil
.
ContextTraceIDKey
)
var
RequestIDKey
=
CtxKeyString
(
ctxUtil
s
.
ContextTraceIDKey
)
// -------------------------------------------------------------------------------------------
...
...
@@ -76,21 +76,21 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
o
.
setRequestIDKey
()
return
func
(
c
*
gin
.
Context
)
{
requestID
:=
c
.
Request
.
Header
.
Get
(
ctxUtil
.
HeaderXRequestIDKey
)
requestID
:=
c
.
Request
.
Header
.
Get
(
ctxUtil
s
.
HeaderXRequestIDKey
)
// Create request id
if
requestID
==
""
{
requestID
=
ctxUtil
.
GenerateTid
()
c
.
Request
.
Header
.
Set
(
ctxUtil
.
HeaderXRequestIDKey
,
requestID
)
requestID
=
ctxUtil
s
.
GenerateTid
()
c
.
Request
.
Header
.
Set
(
ctxUtil
s
.
HeaderXRequestIDKey
,
requestID
)
}
st
:=
time
.
Now
()
// Expose it for use in the application
c
.
Set
(
ctxUtil
.
ContextTraceIDKey
,
requestID
)
c
.
Set
(
ctxUtil
.
KeyApiStartTime
,
st
)
c
.
Set
(
ctxUtil
s
.
ContextTraceIDKey
,
requestID
)
c
.
Set
(
ctxUtil
s
.
KeyApiStartTime
,
st
)
// Set X-Request-Id header
c
.
Writer
.
Header
()
.
Set
(
ctxUtil
.
HeaderXRequestIDKey
,
requestID
)
c
.
Writer
.
Header
()
.
Set
(
ctxUtil
s
.
HeaderXRequestIDKey
,
requestID
)
c
.
Next
()
}
...
...
@@ -98,12 +98,12 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
// HeaderRequestID get request id from the header
func
HeaderRequestID
(
c
*
gin
.
Context
)
string
{
return
c
.
Request
.
Header
.
Get
(
ctxUtil
.
HeaderXRequestIDKey
)
return
c
.
Request
.
Header
.
Get
(
ctxUtil
s
.
HeaderXRequestIDKey
)
}
// HeaderRequestIDField get request id field from header
func
HeaderRequestIDField
(
c
*
gin
.
Context
)
zap
.
Field
{
return
zap
.
String
(
ctxUtil
.
HeaderXRequestIDKey
,
HeaderRequestID
(
c
))
return
zap
.
String
(
ctxUtil
s
.
HeaderXRequestIDKey
,
HeaderRequestID
(
c
))
}
// -------------------------------------------------------------------------------------------
...
...
@@ -111,22 +111,12 @@ func HeaderRequestIDField(c *gin.Context) zap.Field {
// RequestHeaderKey request header key
var
RequestHeaderKey
=
"request_header_key"
// WrapCtx wrap context, put the Keys and Header of gin.Context into context
func
WrapCtx
(
c
*
gin
.
Context
)
context
.
Context
{
ctx
:=
context
.
WithValue
(
c
.
Request
.
Context
(),
ctxUtil
.
ContextTraceIDKey
,
c
.
GetString
(
ctxUtil
.
ContextTraceIDKey
))
//nolint
for
k
,
v
:=
range
c
.
Keys
{
ctx
=
context
.
WithValue
(
ctx
,
k
,
v
)
//nolint
}
ctx
=
context
.
WithValue
(
ctx
,
ctxUtil
.
GinContextKey
,
c
)
//nolint
return
ctx
}
func
WrapAsyncCtx
(
c
*
gin
.
Context
)
context
.
Context
{
ctx
:=
context
.
WithValue
(
context
.
Background
(),
ctxUtil
.
ContextTraceIDKey
,
c
.
GetString
(
ctxUtil
.
ContextTraceIDKey
))
//nolint
ctx
:=
context
.
WithValue
(
context
.
Background
(),
ctxUtil
s
.
ContextTraceIDKey
,
c
.
GetString
(
ctxUtils
.
ContextTraceIDKey
))
//nolint
for
k
,
v
:=
range
c
.
Keys
{
ctx
=
context
.
WithValue
(
ctx
,
k
,
v
)
//nolint
}
ctx
=
context
.
WithValue
(
ctx
,
ctxUtil
.
GinContextKey
,
c
)
//nolint
ctx
=
context
.
WithValue
(
ctx
,
ctxUtil
s
.
GinContextKey
,
c
)
//nolint
return
ctx
}
...
...
@@ -134,7 +124,7 @@ func WrapAsyncCtx(c *gin.Context) context.Context {
func
AdaptCtx
(
ctx
context
.
Context
)
(
*
gin
.
Context
,
context
.
Context
)
{
c
,
ok
:=
ctx
.
(
*
gin
.
Context
)
if
ok
{
ctx
=
WrapCtx
(
c
)
ctx
=
ctxUtils
.
WrapCtx
(
c
)
}
return
c
,
ctx
}
...
...
gin/middleware/sync_ratelimiter/sync_ratelimiter.go
浏览文件 @
55456d37
...
...
@@ -4,7 +4,7 @@ import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"gitlab.wanzhuangkj.com/tush/xpkg/
gin
/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/
utils
/ctxUtils"
"go.uber.org/zap"
"strconv"
"sync"
...
...
gin/response/response.go
浏览文件 @
55456d37
...
...
@@ -14,8 +14,8 @@ import (
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
ctxUtils
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
ctxUtils
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
)
...
...
goredis/logger.go
浏览文件 @
55456d37
...
...
@@ -7,8 +7,8 @@ import (
"runtime"
"time"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
...
...
httpcli/entity/entity.go
浏览文件 @
55456d37
...
...
@@ -5,7 +5,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/
gin
/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/
utils
/ctxUtils"
)
type
CopyHttpReq
struct
{
...
...
httpcli/httpcli.go
浏览文件 @
55456d37
...
...
@@ -17,8 +17,8 @@ import (
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/httpContentType"
"github.com/duke-git/lancet/v2/retry"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
)
const
defaultTimeout
=
30
*
time
.
Second
...
...
utils/ctxUtils/ctxUtil.go
浏览文件 @
55456d37
package
ctxUtils
//
//func AddApiCost(c *gin.Context, appName string, start time.Time) {
// cost := c.Writer.Header().Get(ctxUtil.KeyCost)
// cost += fmt.Sprintf("%s:%d", appName, time.Since(start).Milliseconds())
// c.Writer.Header().Set(ctxUtil.KeyCost, cost)
//}
import
(
"context"
"errors"
"fmt"
"strings"
"time"
xsf
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"go.uber.org/zap"
)
const
(
GinContextKey
=
"ginContext"
HeaderXTimestampKey
=
"Timestamp"
KeyReqBody
=
"reqBody"
KeyRspBody
=
"rspBody"
KeyRspCode
=
"rspCode"
KeyOApiReqBody
=
"oApiReqBody%s"
KeyOApiRspBody
=
"oApiRspBody%s"
KeyAppName
=
"appName"
KeyUser
=
"user"
KeyApiStartTime
=
"apiStartTime"
KeyClientIP
=
"clientIP"
KeyUID
=
"uid"
KeyUType
=
"uType"
KeyCompanyID
=
"companyID"
KeyShopID
=
"shopID"
KeyUName
=
"uname"
KeyToken
=
"token"
KeyCost
=
"X-Request-Cost"
KeyRspBodyMax
=
"rsp-body-max"
)
var
(
HeaderXRequestIDKey
=
"X-Request-ID"
ContextTraceIDKey
=
"X-Request-ID"
)
var
(
ErrorGinContextNotFound
=
errors
.
New
(
"gin context not found"
)
)
func
Set
(
c
*
gin
.
Context
,
key
string
,
val
any
)
{
c
.
Set
(
key
,
val
)
}
func
SetRspBodyMax
(
ctx
context
.
Context
,
max
int
)
{
if
c
,
_
:=
ToGinCtx
(
ctx
);
c
!=
nil
{
Set
(
c
,
KeyRspBodyMax
,
max
)
}
}
func
Get
[
T
any
](
c
*
gin
.
Context
,
key
string
)
(
T
,
bool
)
{
if
val
,
ok
:=
c
.
Get
(
key
);
ok
{
if
v
,
ok1
:=
val
.
(
T
);
ok1
{
return
v
,
true
}
}
var
a
T
return
a
,
false
}
// GetGinCtxTraceID get request id from gin.Context
func
GetGinCtxTraceID
(
c
*
gin
.
Context
)
string
{
if
v
,
isExist
:=
c
.
Get
(
ContextTraceIDKey
);
isExist
{
if
requestID
,
ok
:=
v
.
(
string
);
ok
{
return
requestID
}
}
return
""
}
// GinTraceIDField get request id field from gin.Context
func
GinTraceIDField
(
c
*
gin
.
Context
)
zap
.
Field
{
return
zap
.
String
(
ContextTraceIDKey
,
GetGinCtxTraceID
(
c
))
}
// CtxRequestID get request id from context.Context
func
CtxRequestID
(
c
context
.
Context
)
string
{
v
:=
c
.
Value
(
ContextTraceIDKey
)
if
str
,
ok
:=
v
.
(
string
);
ok
{
return
str
}
return
""
}
// CtxXRequestID get request id from context.Context
func
CtxXRequestID
(
c
context
.
Context
)
string
{
v
:=
c
.
Value
(
HeaderXRequestIDKey
)
if
str
,
ok
:=
v
.
(
string
);
ok
{
return
str
}
return
""
}
func
CtxGetGinCtx
(
c
context
.
Context
)
*
gin
.
Context
{
if
str
,
ok
:=
c
.
Value
(
GinContextKey
)
.
(
*
gin
.
Context
);
ok
{
return
str
}
return
nil
}
// CtxTraceIDField get request id field from context.Context
func
CtxTraceIDField
(
c
context
.
Context
)
zap
.
Field
{
return
zap
.
String
(
ContextTraceIDKey
,
CtxRequestID
(
c
))
}
func
NewEmptyCtx
()
context
.
Context
{
return
context
.
WithValue
(
context
.
Background
(),
ContextTraceIDKey
,
GenerateTid
())
}
func
NewCtx
(
ctx
context
.
Context
)
context
.
Context
{
return
context
.
WithValue
(
context
.
Background
(),
ContextTraceIDKey
,
ctx
.
Value
(
ContextTraceIDKey
))
}
func
GetGinUserID
(
c
*
gin
.
Context
)
xsf
.
ID
{
return
getGinVal
[
xsf
.
ID
](
c
,
KeyUID
)
}
func
GetGinShopID
(
c
*
gin
.
Context
)
xsf
.
ID
{
return
getGinVal
[
xsf
.
ID
](
c
,
KeyShopID
)
}
func
GetCtxUserID
(
c
context
.
Context
)
xsf
.
ID
{
return
getCtxVal
[
xsf
.
ID
](
c
,
KeyUID
)
}
func
GetCtxShopID
(
c
context
.
Context
)
xsf
.
ID
{
return
getCtxVal
[
xsf
.
ID
](
c
,
KeyShopID
)
}
func
GetCtxString
(
c
context
.
Context
,
key
string
)
string
{
val
:=
c
.
Value
(
key
)
if
val
!=
nil
{
return
cast
.
ToString
(
val
)
}
return
""
}
func
GetCtxUserToken
(
c
context
.
Context
)
string
{
token
:=
c
.
Value
(
KeyToken
)
if
token
!=
nil
{
return
cast
.
ToString
(
token
)
}
return
""
}
func
GetGinCtxTid
(
c
*
gin
.
Context
)
string
{
tid
:=
c
.
GetString
(
ContextTraceIDKey
)
if
tid
==
""
{
tid
=
GenerateTid
()
c
.
Set
(
ContextTraceIDKey
,
tid
)
}
return
tid
}
func
GenerateTid
()
string
{
return
xsf
.
GenerateID
()
.
String
()
}
func
GetCtxTid
(
ctx
context
.
Context
)
string
{
tid
:=
""
tidVal
:=
ctx
.
Value
(
ContextTraceIDKey
)
if
tidVal
!=
nil
{
if
str
,
ok
:=
tidVal
.
(
string
);
ok
{
tid
=
str
}
}
if
tid
==
""
{
tid
=
GenerateTid
()
}
return
tid
}
// GetClientIP get ip from context.Context, the client ip is set in gin.Context
func
GetClientIP
(
ctx
context
.
Context
)
string
{
ip
:=
""
ipVal
:=
ctx
.
Value
(
KeyClientIP
)
if
ipVal
!=
nil
{
if
str
,
ok
:=
ipVal
.
(
string
);
ok
{
ip
=
str
}
}
return
ip
}
func
WrapCtx
(
c
*
gin
.
Context
)
context
.
Context
{
ctx
:=
context
.
WithValue
(
c
.
Request
.
Context
(),
ContextTraceIDKey
,
c
.
GetString
(
ContextTraceIDKey
))
for
k
,
v
:=
range
c
.
Keys
{
ctx
=
context
.
WithValue
(
ctx
,
k
,
v
)
}
ctx
=
context
.
WithValue
(
ctx
,
GinContextKey
,
c
)
//nolint
return
ctx
}
func
ToGinCtx
(
ctx
context
.
Context
)
(
*
gin
.
Context
,
error
)
{
if
c
,
ok
:=
ctx
.
Value
(
GinContextKey
)
.
(
*
gin
.
Context
);
ok
{
return
c
,
nil
}
return
nil
,
ErrorGinContextNotFound
}
func
GetGinCtx
(
ctx
context
.
Context
)
(
*
gin
.
Context
,
error
)
{
if
c
,
ok
:=
ctx
.
Value
(
GinContextKey
)
.
(
*
gin
.
Context
);
ok
{
return
c
,
nil
}
return
nil
,
ErrorGinContextNotFound
}
func
GetCtxUID
(
c
context
.
Context
)
uint
{
return
getCtxVal
[
uint
](
c
,
KeyUID
)
}
func
GetCtxUType
(
c
context
.
Context
)
uint
{
return
getCtxVal
[
uint
](
c
,
KeyUType
)
}
func
GetCtxCompanyID
(
c
context
.
Context
)
uint
{
return
getCtxVal
[
uint
](
c
,
KeyCompanyID
)
}
func
getGinVal
[
T
any
](
c
*
gin
.
Context
,
key
string
)
T
{
val
,
exists
:=
c
.
Get
(
key
)
if
exists
{
if
ret
,
ok
:=
val
.
(
T
);
ok
{
return
ret
}
}
var
t
T
return
t
}
func
getCtxVal
[
T
any
](
c
context
.
Context
,
key
string
)
T
{
val
:=
c
.
Value
(
key
)
if
val
!=
nil
{
if
ret
,
ok
:=
val
.
(
T
);
ok
{
return
ret
}
}
var
t
T
return
t
}
func
GetGinReq
(
c
*
gin
.
Context
)
[]
byte
{
v
,
ok1
:=
c
.
Get
(
KeyReqBody
)
if
ok1
{
ret
,
ok2
:=
v
.
([]
byte
)
if
ok2
{
return
ret
}
}
return
nil
}
func
GetGinRsp
(
c
*
gin
.
Context
)
[]
byte
{
v
,
ok1
:=
c
.
Get
(
KeyRspBody
)
if
ok1
{
ret
,
ok2
:=
v
.
([]
byte
)
if
ok2
{
return
ret
}
}
return
nil
}
func
SetVal
(
ctx
context
.
Context
,
key
string
,
val
interface
{})
context
.
Context
{
if
ginCtx
,
err
:=
GetGinCtx
(
ctx
);
err
==
nil
&&
ginCtx
!=
nil
{
ginCtx
.
Set
(
key
,
val
)
}
return
context
.
WithValue
(
ctx
,
key
,
val
)
}
func
AddApiCost
(
c
*
gin
.
Context
,
appName
string
,
start
time
.
Time
)
{
cost
:=
c
.
Writer
.
Header
()
.
Get
(
KeyCost
)
currentCost
:=
fmt
.
Sprintf
(
"%s:%s"
,
appName
,
formatDuration
(
time
.
Since
(
start
)))
if
cost
==
""
{
cost
=
currentCost
}
else
{
cost
=
fmt
.
Sprintf
(
"%s,%s"
,
currentCost
,
cost
)
}
c
.
Writer
.
Header
()
.
Set
(
KeyCost
,
cost
)
}
func
formatDuration
(
d
time
.
Duration
)
string
{
ms
:=
float64
(
d
.
Nanoseconds
())
/
1e6
return
fmt
.
Sprintf
(
"%.2fms"
,
ms
)
}
func
SetApiCost
(
ctx
context
.
Context
,
header
map
[
string
][]
string
)
{
if
ginCtx
,
_
:=
GetGinCtx
(
ctx
);
ginCtx
!=
nil
&&
len
(
header
)
>
0
{
cost
:=
""
for
k
,
v
:=
range
header
{
if
k
==
KeyCost
{
cost
=
strings
.
Join
(
v
,
","
)
}
}
if
cost
!=
""
{
ginCtx
.
Writer
.
Header
()
.
Set
(
KeyCost
,
cost
)
}
}
}
utils/wg/wg.go
浏览文件 @
55456d37
...
...
@@ -2,8 +2,8 @@ package wg
import
(
"context"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"sync"
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论