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

snowflake as default

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