Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
739d10d6
提交
739d10d6
authored
6月 12, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1. http cli retry count
2. sfid add func ToInt
上级
e9fe6fb4
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
57 行增加
和
5 行删除
+57
-5
logging.go
gin/middleware/logging.go
+10
-0
httpcli.go
httpcli/httpcli.go
+43
-5
xsf.go
utils/xsf/xsf.go
+4
-0
没有找到文件。
gin/middleware/logging.go
浏览文件 @
739d10d6
...
@@ -124,11 +124,21 @@ func getResponseBody(buf *bytes.Buffer, maxLen int) []byte {
...
@@ -124,11 +124,21 @@ func getResponseBody(buf *bytes.Buffer, maxLen int) []byte {
if
n
==
0
{
if
n
==
0
{
return
emptyBody
return
emptyBody
}
else
if
n
<
maxLen
{
}
else
if
n
<
maxLen
{
if
isLastByteNewline
(
body
)
{
return
body
[
:
n
-
1
]
return
body
[
:
n
-
1
]
}
}
return
body
[
:
n
]
}
return
append
(
body
[
:
maxLen
-
len
(
contentMark
)],
contentMark
...
)
return
append
(
body
[
:
maxLen
-
len
(
contentMark
)],
contentMark
...
)
}
}
func
isLastByteNewline
(
data
[]
byte
)
bool
{
if
len
(
data
)
==
0
{
return
false
}
return
data
[
len
(
data
)
-
1
]
==
'\n'
}
// If there is sensitive information in the body, you can use WithIgnoreRoutes set the route to ignore logging
// If there is sensitive information in the body, you can use WithIgnoreRoutes set the route to ignore logging
func
getRequestBody
(
buf
*
bytes
.
Buffer
,
maxLen
int
)
[]
byte
{
func
getRequestBody
(
buf
*
bytes
.
Buffer
,
maxLen
int
)
[]
byte
{
l
:=
buf
.
Len
()
l
:=
buf
.
Len
()
...
...
httpcli/httpcli.go
浏览文件 @
739d10d6
...
@@ -14,8 +14,8 @@ import (
...
@@ -14,8 +14,8 @@ import (
"time"
"time"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xjson"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xjson"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"github.com/duke-git/lancet/v2/retry"
"github.com/duke-git/lancet/v2/retry"
...
@@ -35,6 +35,7 @@ type Request struct {
...
@@ -35,6 +35,7 @@ type Request struct {
body
string
// Body data
body
string
// Body data
bodyJSON
interface
{}
// JSON marshal body data
bodyJSON
interface
{}
// JSON marshal body data
timeout
time
.
Duration
// Client timeout
timeout
time
.
Duration
// Client timeout
retryCount
uint
headers
map
[
string
]
string
headers
map
[
string
]
string
request
*
http
.
Request
request
*
http
.
Request
...
@@ -118,6 +119,10 @@ func (req *Request) SetTimeout(t time.Duration) *Request {
...
@@ -118,6 +119,10 @@ func (req *Request) SetTimeout(t time.Duration) *Request {
req
.
timeout
=
t
req
.
timeout
=
t
return
req
return
req
}
}
func
(
req
*
Request
)
SetRetry
(
count
uint
)
*
Request
{
req
.
retryCount
=
count
return
req
}
// SetContentType set ContentType
// SetContentType set ContentType
func
(
req
*
Request
)
SetContentType
(
a
string
)
*
Request
{
func
(
req
*
Request
)
SetContentType
(
a
string
)
*
Request
{
...
@@ -314,12 +319,15 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
...
@@ -314,12 +319,15 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Second
*
20
)
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Second
*
20
)
defer
cancel
()
defer
cancel
()
err
:=
retry
.
Retry
(
func
()
error
{
var
err
error
if
req
.
retryCount
>
0
{
err
=
retry
.
Retry
(
func
()
error
{
response
,
err
:=
client
.
Do
(
req
.
request
)
response
,
err
:=
client
.
Do
(
req
.
request
)
if
err
!=
nil
{
if
err
!=
nil
{
logger
.
Info
(
"httpcli fail"
,
logger
.
Any
(
"err"
,
err
),
xctx
.
CtxTraceIDField
(
ctx
))
logger
.
Info
(
"httpcli fail"
,
logger
.
Any
(
"err"
,
err
),
xctx
.
CtxTraceIDField
(
ctx
))
return
xerror
.
New
(
err
.
Error
()
)
return
xerror
.
Wrap
(
err
,
"client do"
)
}
}
if
response
!=
nil
{
resp
.
Status
=
response
.
Status
resp
.
Status
=
response
.
Status
resp
.
StatusCode
=
response
.
StatusCode
resp
.
StatusCode
=
response
.
StatusCode
for
k
,
v
:=
range
response
.
Header
{
for
k
,
v
:=
range
response
.
Header
{
...
@@ -331,16 +339,46 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
...
@@ -331,16 +339,46 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
return
xerror
.
New
(
err
.
Error
())
return
xerror
.
New
(
err
.
Error
())
}
}
logger
.
Info
(
"httpcli rsp"
,
logger
.
Any
(
"body"
,
bytes
.
NewBuffer
(
body
)
.
String
()),
xctx
.
CtxTraceIDField
(
ctx
))
logger
.
Info
(
"httpcli rsp"
,
logger
.
Any
(
"body"
,
bytes
.
NewBuffer
(
body
)
.
String
()),
xctx
.
CtxTraceIDField
(
ctx
))
if
body
!=
nil
{
resp
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
body
))
resp
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
body
))
}
}
return
nil
return
nil
},
retry
.
RetryWithLinearBackoff
(
time
.
Second
),
retry
.
Context
(
ctx
))
},
retry
.
RetryTimes
(
req
.
retryCount
),
retry
.
RetryWithLinearBackoff
(
2
*
time
.
Second
),
retry
.
Context
(
ctx
),
)
}
else
{
response
,
e
:=
client
.
Do
(
req
.
request
)
if
e
!=
nil
{
err
=
xerror
.
New
(
e
.
Error
())
}
if
response
!=
nil
{
resp
.
Status
=
response
.
Status
resp
.
StatusCode
=
response
.
StatusCode
for
k
,
v
:=
range
response
.
Header
{
resp
.
Header
[
k
]
=
v
}
defer
response
.
Body
.
Close
()
body
,
e
:=
io
.
ReadAll
(
response
.
Body
)
if
e
!=
nil
{
err
=
xerror
.
New
(
e
.
Error
())
}
logger
.
Info
(
"httpcli rsp"
,
logger
.
Any
(
"body"
,
bytes
.
NewBuffer
(
body
)
.
String
()),
xctx
.
CtxTraceIDField
(
ctx
))
if
body
!=
nil
{
resp
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
body
))
}
}
}
if
err
!=
nil
{
if
err
!=
nil
{
err
=
xerror
.
New
(
err
.
Error
())
logger
.
Error
(
"httpcli fail"
,
logger
.
Any
(
"err"
,
err
),
xctx
.
CtxTraceIDField
(
ctx
))
logger
.
Error
(
"httpcli fail"
,
logger
.
Any
(
"err"
,
err
),
xctx
.
CtxTraceIDField
(
ctx
))
}
}
req
.
response
=
resp
req
.
response
=
resp
req
.
err
=
err
req
.
err
=
err
resp
.
err
=
err
return
resp
,
resp
.
err
return
resp
,
resp
.
err
}
}
...
...
utils/xsf/xsf.go
浏览文件 @
739d10d6
...
@@ -86,6 +86,10 @@ func (x ID) ToInt64() int64 {
...
@@ -86,6 +86,10 @@ func (x ID) ToInt64() int64 {
return
snowflake
.
ID
(
x
)
.
Int64
()
return
snowflake
.
ID
(
x
)
.
Int64
()
}
}
func
(
x
ID
)
ToInt
()
int
{
return
int
(
snowflake
.
ID
(
x
)
.
Int64
())
}
func
GenerateID
()
ID
{
func
GenerateID
()
ID
{
sid
:=
Generate
()
sid
:=
Generate
()
return
ID
(
sid
)
return
ID
(
sid
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论