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

update

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