3 Commits
v0.0.20 ... dev

Author SHA1 Message Date
6097209c48 ci/cd调整 2026-06-10 15:01:13 +08:00
1835faddc0 ci/cd调整 2026-06-10 14:54:26 +08:00
qhd
0ddc2f17b9 fix: 修复snowflake节点并发初始化问题 2026-06-06 14:34:33 +08:00
16 changed files with 58 additions and 47 deletions

View File

@@ -7,10 +7,11 @@ import (
"fmt"
"regexp"
"strings"
"sync"
"time"
"gitea.com/red-future/common/beans"
"gitea.com/red-future/common/utils"
"gitea.redpowerfuture.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/bwmarrin/snowflake"
"github.com/gogf/gf/v2/crypto/gmd5"
"github.com/gogf/gf/v2/database/gdb"
@@ -29,8 +30,28 @@ import (
var (
localCache *gcache.Cache
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 获取本地缓存实例
func getLocalCache() *gcache.Cache {
if localCache == nil {
@@ -166,17 +187,8 @@ func insertHook(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result
return nil, err
}
// 懒加载初始化全局snowflake节点只创建一次
if snowflakeNode == 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
if g.IsEmpty(snowflakeNode) {
return nil, fmt.Errorf("snowflakeNode is nil")
}
for i := range in.Data {

View File

@@ -16,7 +16,7 @@ import (
"syscall"
"time"
"gitea.com/red-future/common/log/consts"
"gitea.redpowerfuture.com/red-future/common/log/consts"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"

View File

@@ -11,12 +11,12 @@ import (
"fmt"
"time"
"gitea.com/red-future/common/log/consts"
"gitea.redpowerfuture.com/red-future/common/log/consts"
"go.mongodb.org/mongo-driver/v2/event"
"gitea.com/red-future/common/beans"
"gitea.com/red-future/common/log/model/entity"
"gitea.com/red-future/common/utils"
"gitea.redpowerfuture.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/log/model/entity"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"

View File

@@ -10,8 +10,8 @@ import (
"fmt"
"time"
"gitea.com/red-future/common/beans"
"gitea.com/red-future/common/utils"
"gitea.redpowerfuture.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"

3
go.mod
View File

@@ -1,5 +1,4 @@
module gitea.com/red-future/common
module gitea.redpowerfuture.com/red-future/common
go 1.26.0
require (

View File

@@ -9,9 +9,9 @@ import (
"regexp"
"strings"
_ "gitea.com/red-future/common/consul"
"gitea.com/red-future/common/jaeger"
"gitea.com/red-future/common/utils"
_ "gitea.redpowerfuture.com/red-future/common/consul"
"gitea.redpowerfuture.com/red-future/common/jaeger"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
"github.com/gogf/gf/v2/net/ghttp"

View File

@@ -3,8 +3,8 @@ package controller
import (
"context"
"gitea.com/red-future/common/log/model/dto"
"gitea.com/red-future/common/log/service"
"gitea.redpowerfuture.com/red-future/common/log/model/dto"
"gitea.redpowerfuture.com/red-future/common/log/service"
)
type operationLog struct{}

View File

@@ -3,15 +3,15 @@ package dao
import (
"context"
"gitea.com/red-future/common/beans"
"gitea.com/red-future/common/db/mongo"
"gitea.redpowerfuture.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/db/mongo"
"strings"
"time"
"gitea.com/red-future/common/log/consts"
"gitea.com/red-future/common/log/model/dto"
"gitea.com/red-future/common/log/model/entity"
"gitea.redpowerfuture.com/red-future/common/log/consts"
"gitea.redpowerfuture.com/red-future/common/log/model/dto"
"gitea.redpowerfuture.com/red-future/common/log/model/entity"
"go.mongodb.org/mongo-driver/v2/bson"
)

View File

@@ -1,7 +1,7 @@
package dto
import (
"gitea.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/beans"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)

View File

@@ -1,7 +1,7 @@
package entity
import (
"gitea.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/beans"
)
// OperationLog 操作日志实体 - 用于记录数据增删改操作行为

View File

@@ -2,11 +2,11 @@ package service
import (
"context"
"gitea.com/red-future/common/beans"
"gitea.com/red-future/common/log/dao"
"gitea.com/red-future/common/log/model/dto"
logEntity "gitea.com/red-future/common/log/model/entity"
"gitea.com/red-future/common/utils"
"gitea.redpowerfuture.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/log/dao"
"gitea.redpowerfuture.com/red-future/common/log/model/dto"
logEntity "gitea.redpowerfuture.com/red-future/common/log/model/entity"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/gogf/gf/v2/util/gconv"
)

View File

@@ -9,7 +9,7 @@ import (
"sync/atomic"
"time"
"gitea.com/red-future/common/utils"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/alibaba/sentinel-golang/api"
"github.com/alibaba/sentinel-golang/core/circuitbreaker"
"github.com/gogf/gf/v2/frame/g"

View File

@@ -4,9 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"gitea.com/red-future/common/beans"
commonHttp "gitea.com/red-future/common/http"
"gitea.com/red-future/common/utils"
"gitea.redpowerfuture.com/red-future/common/beans"
commonHttp "gitea.redpowerfuture.com/red-future/common/http"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"gitea.com/red-future/common/utils"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/util/gconv"

View File

@@ -2,8 +2,8 @@ package swagger
import (
"fmt"
"gitea.com/red-future/common/consul"
"gitea.com/red-future/common/http"
"gitea.redpowerfuture.com/red-future/common/consul"
"gitea.redpowerfuture.com/red-future/common/http"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/util/gconv"

View File

@@ -13,7 +13,7 @@ import (
"sync/atomic"
"time"
"gitea.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/beans"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/errors/gcode"