Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0ddc2f17b9 |
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.com/red-future/common/beans"
|
||||||
@@ -29,8 +30,28 @@ import (
|
|||||||
var (
|
var (
|
||||||
localCache *gcache.Cache
|
localCache *gcache.Cache
|
||||||
snowflakeNode *snowflake.Node
|
snowflakeNode *snowflake.Node
|
||||||
|
snowflakeOnce sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
ctx := context.Background()
|
||||||
|
snowflakeOnce.Do(func() {
|
||||||
|
nodeId := genv.Get("APP_NODE", 1).Int64()
|
||||||
|
// 安全范围 0~1023
|
||||||
|
if nodeId < 0 || nodeId > 1023 {
|
||||||
|
nodeId = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
node, err := snowflake.NewNode(nodeId)
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Errorf(ctx, "snowflake init failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
snowflakeNode = node
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// getLocalCache 获取本地缓存实例
|
// getLocalCache 获取本地缓存实例
|
||||||
func getLocalCache() *gcache.Cache {
|
func getLocalCache() *gcache.Cache {
|
||||||
if localCache == nil {
|
if localCache == nil {
|
||||||
@@ -166,17 +187,8 @@ func insertHook(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 懒加载初始化全局snowflake节点,只创建一次
|
if g.IsEmpty(snowflakeNode) {
|
||||||
if snowflakeNode == nil {
|
return nil, fmt.Errorf("snowflakeNode is nil")
|
||||||
nodeId := genv.Get("APP_NODE", "").Int64()
|
|
||||||
if g.IsEmpty(nodeId) {
|
|
||||||
nodeId = 1
|
|
||||||
}
|
|
||||||
node, err := snowflake.NewNode(nodeId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
snowflakeNode = node
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range in.Data {
|
for i := range in.Data {
|
||||||
|
|||||||
Reference in New Issue
Block a user