提交 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 { ...@@ -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 {
return body[:n-1] if isLastByteNewline(body) {
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()
......
...@@ -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,33 +319,66 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer) ...@@ -314,33 +319,66 @@ 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
response, err := client.Do(req.request) if req.retryCount > 0 {
if err != nil { err = retry.Retry(func() error {
logger.Info("httpcli fail", logger.Any("err", err), xctx.CtxTraceIDField(ctx)) response, err := client.Do(req.request)
return xerror.New(err.Error()) if err != nil {
} logger.Info("httpcli fail", logger.Any("err", err), xctx.CtxTraceIDField(ctx))
resp.Status = response.Status return xerror.Wrap(err, "client do")
resp.StatusCode = response.StatusCode }
for k, v := range response.Header { if response != nil {
resp.Header[k] = v resp.Status = response.Status
resp.StatusCode = response.StatusCode
for k, v := range response.Header {
resp.Header[k] = v
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
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.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())
} }
defer response.Body.Close() if response != nil {
body, err := io.ReadAll(response.Body) resp.Status = response.Status
if err != nil { resp.StatusCode = response.StatusCode
return xerror.New(err.Error()) 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))
}
} }
logger.Info("httpcli rsp", logger.Any("body", bytes.NewBuffer(body).String()), xctx.CtxTraceIDField(ctx)) }
resp.Body = io.NopCloser(bytes.NewBuffer(body))
return nil
}, retry.RetryWithLinearBackoff(time.Second), retry.Context(ctx))
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
} }
......
...@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论