提交 5c90f99a authored 作者: mooncake9527's avatar mooncake9527

update

上级 5f7f8976
......@@ -106,6 +106,7 @@ type Config struct {
NacosRd NacosRd `yaml:"nacosRd" json:"nacosRd"`
Redis Redis `yaml:"redis" json:"redis"`
CronJobs Cron `yaml:"cron" json:"cron"`
Leaf Leaf `yaml:"leaf" json:"leaf"`
NacosConfClient *NacosClient `yaml:"-" json:"-"`
NacosNamingClient *nacos.NacosNamingClient `yaml:"-" json:"-"`
AliOss alioss.AliOssConfig `yaml:"oss" json:"oss"`
......@@ -364,6 +365,12 @@ type Cron struct {
Enable bool `yaml:"enable" json:"enable"`
}
type Leaf struct {
Addr string `yaml:"addr" json:"addr"` // eg http://leaf.qitu
ShowLog bool `yaml:"showLog" json:"showLog"`
RetryCount int `yaml:"retryCount" json:"retryCount"`
}
type CronJob struct {
Name string `yaml:"name" json:"name"`
TimeSpec string `yaml:"timeSpec" json:"timeSpec"`
......
......@@ -32,7 +32,7 @@ type Request struct {
reqBody []byte // Body data
reqBodyJSON interface{} // 传入对象,会自动json marshal
timeout time.Duration // Client timeout
retryCount uint
retryCount int
request *http.Request
response *Response
......@@ -143,7 +143,7 @@ func (x *Request) SetTimeout(t time.Duration) *Request {
x.timeout = t
return x
}
func (x *Request) SetRetry(count uint) *Request {
func (x *Request) SetRetry(count int) *Request {
x.retryCount = count
return x
}
......@@ -327,7 +327,7 @@ func (x *Request) send(ctx context.Context) {
x.pushDo(ctx)
return x.err
},
retry.RetryTimes(x.retryCount),
retry.RetryTimes(uint(x.retryCount)),
retry.RetryWithLinearBackoff(8*time.Second),
retry.Context(ctx),
)
......
......@@ -8,11 +8,15 @@ import (
"os"
"time"
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
)
var (
retryCount = 3
showLog = false
xsfAddr string = "http://leaf.qitu"
api = &client{
cli: httpcli.NewClient(
......@@ -24,7 +28,6 @@ var (
},
}),
}
ErrNoXsfAddr = errors.New("缺少环境变量XSF_ADDR")
)
type client struct {
......@@ -35,17 +38,32 @@ func init() {
if addr := os.Getenv("XSF_ADDR"); addr != "" {
xsfAddr = addr
}
if config.Cfg.Leaf.Addr != "" {
xsfAddr = config.Cfg.Leaf.Addr
}
config.Read(func(c *config.Config) {
if c.Leaf.ShowLog {
showLog = true
}
if c.Leaf.RetryCount > 0 {
retryCount = c.Leaf.RetryCount
}
})
}
func GenerateApiID(ctx context.Context, bizTag string) (xsf.ID, error) {
url := fmt.Sprintf(`%s/api/leaf?biz_tag=%s`, xsfAddr, bizTag)
var id xsf.ID
reply := &IDReply{
Data: &id,
}
if err := api.cli.NewRequest().OmitLog().SetRetry(3).
// if err := api.cli.NewRequest().SetRetry(3).
SetContentType("application/json").SetURL(url).GET(ctx).BindJSON(reply).Err(); err != nil {
return xsf.ID(-1), err
req := api.cli.NewRequest()
if !showLog {
req = req.OmitLog()
}
if err := req.SetRetry(retryCount).SetContentType("application/json").SetURL(url).GET(ctx).BindJSON(reply).Err(); err != nil {
logger.Error("调用leaf失败", logger.String("bizTag", bizTag), logger.Err(err))
return xsf.ID(-1), fmt.Errorf("调用leaf失败, %w", err)
}
if reply.Code == 1 {
return id, nil
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论