提交 739d10d6 authored 作者: mooncake9527's avatar mooncake9527

1. http cli retry count

2. sfid add func ToInt
上级 e9fe6fb4
......@@ -124,11 +124,21 @@ func getResponseBody(buf *bytes.Buffer, maxLen int) []byte {
if n == 0 {
return emptyBody
} else if n < maxLen {
if isLastByteNewline(body) {
return body[:n-1]
}
return body[:n]
}
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
func getRequestBody(buf *bytes.Buffer, maxLen int) []byte {
l := buf.Len()
......
......@@ -14,8 +14,8 @@ import (
"time"
"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/utils/xjson"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"github.com/duke-git/lancet/v2/retry"
......@@ -35,6 +35,7 @@ type Request struct {
body string // Body data
bodyJSON interface{} // JSON marshal body data
timeout time.Duration // Client timeout
retryCount uint
headers map[string]string
request *http.Request
......@@ -118,6 +119,10 @@ func (req *Request) SetTimeout(t time.Duration) *Request {
req.timeout = t
return req
}
func (req *Request) SetRetry(count uint) *Request {
req.retryCount = count
return req
}
// SetContentType set ContentType
func (req *Request) SetContentType(a string) *Request {
......@@ -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)
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)
if err != nil {
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.StatusCode = response.StatusCode
for k, v := range response.Header {
......@@ -331,16 +339,46 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
return xerror.New(err.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))
}
}
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 {
err = xerror.New(err.Error())
logger.Error("httpcli fail", logger.Any("err", err), xctx.CtxTraceIDField(ctx))
}
req.response = resp
req.err = err
resp.err = err
return resp, resp.err
}
......
......@@ -86,6 +86,10 @@ func (x ID) ToInt64() int64 {
return snowflake.ID(x).Int64()
}
func (x ID) ToInt() int {
return int(snowflake.ID(x).Int64())
}
func GenerateID() ID {
sid := Generate()
return ID(sid)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论