提交 6a544217 authored 作者: mooncake9527's avatar mooncake9527

snowflake as default

上级 3b84dccb
...@@ -14,11 +14,11 @@ import ( ...@@ -14,11 +14,11 @@ import (
) )
var ( var (
XSF_TYPE = "SF" idGenerateType = "snowflake" // 默认snowflake方式生成
retryCount = 3 leafRetryCount = 3
showLog = false leafShowLog = false
addr string = "http://leaf.qitu" leafAddr string = "http://leaf.qitu"
api = &client{ leafApi = &client{
cli: httpcli.NewClient( cli: httpcli.NewClient(
&http.Client{ &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
...@@ -35,22 +35,26 @@ type client struct { ...@@ -35,22 +35,26 @@ type client struct {
} }
func Init() error { func Init() error {
if config.Cfg.App.IDGeneration.Leaf.Addr != "" { if config.Cfg.App.IDGeneration.Type != "" {
addr = config.Cfg.App.IDGeneration.Leaf.Addr idGenerateType = config.Cfg.App.IDGeneration.Type
} if idGenerateType == "leaf" {
config.Read(func(c *config.Config) { if config.Cfg.App.IDGeneration.Leaf.Addr != "" {
if c.App.IDGeneration.Leaf.ShowLog { leafAddr = config.Cfg.App.IDGeneration.Leaf.Addr
showLog = true }
} if config.Cfg.App.IDGeneration.Leaf.ShowLog {
if c.App.IDGeneration.Leaf.RetryCount > 0 { leafShowLog = true
retryCount = c.App.IDGeneration.Leaf.RetryCount }
if config.Cfg.App.IDGeneration.Leaf.RetryCount > 0 {
leafRetryCount = config.Cfg.App.IDGeneration.Leaf.RetryCount
}
} }
}) }
return nil return nil
} }
func GenerateID(ctx context.Context, bizTag string) (xsf.ID, error) { func GenerateID(ctx context.Context, bizTag string) (xsf.ID, error) {
if XSF_TYPE == "snowflake" { if idGenerateType == "snowflake" {
return GenerateSFID(ctx, bizTag) return GenerateSFID(ctx, bizTag)
} else { } else {
return GenerateApiID(ctx, bizTag) return GenerateApiID(ctx, bizTag)
...@@ -62,16 +66,16 @@ func GenerateSFID(ctx context.Context, bizTag string) (xsf.ID, error) { ...@@ -62,16 +66,16 @@ func GenerateSFID(ctx context.Context, bizTag string) (xsf.ID, error) {
} }
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`, addr, bizTag) url := fmt.Sprintf(`%s/api/leaf?biz_tag=%s`, leafAddr, bizTag)
var id xsf.ID var id xsf.ID
reply := &IDReply{ reply := &IDReply{
Data: &id, Data: &id,
} }
req := api.cli.NewRequest() req := leafApi.cli.NewRequest()
if !showLog { if !leafShowLog {
req = req.OmitLog() req = req.OmitLog()
} }
if err := req.SetRetry(retryCount).SetContentType("application/json").SetURL(url).GET(ctx).BindJSON(reply).Err(); err != nil { if err := req.SetRetry(leafRetryCount).SetContentType("application/json").SetURL(url).GET(ctx).BindJSON(reply).Err(); err != nil {
logger.Error("调用leaf失败", logger.String("bizTag", bizTag), logger.Err(err)) logger.Error("调用leaf失败", logger.String("bizTag", bizTag), logger.Err(err))
return xsf.ID(-1), fmt.Errorf("调用leaf失败, %w", err) return xsf.ID(-1), fmt.Errorf("调用leaf失败, %w", err)
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论