diff --git a/go.sum b/go.sum index c00e7127..d104eaed 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,6 @@ github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQ github.com/aws/aws-sdk-go v1.25.3/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go v1.42.27/go.mod h1:OGr6lGMAKGlG9CVrYnWYDKIyb829c6EVBRjxqjmPepc= -github.com/aws/aws-sdk-go v1.44.41 h1:FNW3Tb8vKvXLZ7lzGlg/dCAXhK4RC5fyFewD11oJhUM= -github.com/aws/aws-sdk-go v1.44.41/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= github.com/aws/aws-sdk-go v1.44.42 h1:sPkafCTLh2diZtDojetwbhU7QWQljYvc3PRjnrgKFlE= github.com/aws/aws-sdk-go v1.44.42/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= @@ -722,8 +720,6 @@ golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.0.0-20220622184535-263ec571b305 h1:dAgbJ2SP4jD6XYfMNLVj0BF21jo2PjChrtGaAvF5M3I= -golang.org/x/net v0.0.0-20220622184535-263ec571b305/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ= golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -782,8 +778,6 @@ golang.org/x/sys v0.0.0-20210902050250-f475640dd07b/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664 h1:wEZYwx+kK+KlZ0hpvP2Ls1Xr4+RWnlzGFwPP0aiDjIU= -golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJIkPFVM6oDtMFYmgws0= golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= @@ -845,8 +839,6 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98 google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0= -google.golang.org/genproto v0.0.0-20220623142657-077d458a5694 h1:itnFmgk4Ls5nT+mYO2ZK6F0DpKsGZLhB5BB9y5ZL2HA= -google.golang.org/genproto v0.0.0-20220623142657-077d458a5694/go.mod h1:KEWEmljWE5zPzLBa/oHl6DaEt9LmfH6WtH1OHIvleBA= google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f h1:hJ/Y5SqPXbarffmAsApliUlcvMU+wScNGfyop4bZm8o= google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f/go.mod h1:KEWEmljWE5zPzLBa/oHl6DaEt9LmfH6WtH1OHIvleBA= google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= diff --git a/utils/gojobs/grpc_worker.go b/utils/gojobs/grpc_worker.go index 5778b9ee..07923dcb 100644 --- a/utils/gojobs/grpc_worker.go +++ b/utils/gojobs/grpc_worker.go @@ -3,7 +3,7 @@ package gojobs import ( "context" "go.dtapp.net/library/utils/gojobs/pb" - "go.dtapp.net/library/utils/gouuid" + "go.dtapp.net/library/utils/only" "google.golang.org/grpc" ) @@ -52,7 +52,7 @@ func NewWorker(config *WorkerConfig) *Worker { // SubscribeCron 订阅服务 func (w *Worker) SubscribeCron() pb.PubSub_SubscribeClient { stream, err := w.Pub.Subscribe(context.Background(), &pb.SubscribeRequest{ - Id: gouuid.GetUuId(), + Id: only.GetUuId(), Value: prefix, Ip: w.ClientIp, }) @@ -65,7 +65,7 @@ func (w *Worker) SubscribeCron() pb.PubSub_SubscribeClient { // StartCron 启动任务 func (w *Worker) StartCron() pb.PubSub_SubscribeClient { stream, err := w.Pub.Subscribe(context.Background(), &pb.SubscribeRequest{ - Id: gouuid.GetUuId(), + Id: only.GetUuId(), Value: prefixSprintf(w.ClientIp), Ip: w.ClientIp, }) diff --git a/utils/gojobs/jobs_gorm.go b/utils/gojobs/jobs_gorm.go index c1bf4b39..f7f0e0b3 100644 --- a/utils/gojobs/jobs_gorm.go +++ b/utils/gojobs/jobs_gorm.go @@ -8,7 +8,7 @@ import ( "go.dtapp.net/library/utils/goarray" "go.dtapp.net/library/utils/goip" "go.dtapp.net/library/utils/gojobs/jobs_gorm_model" - "go.dtapp.net/library/utils/gouuid" + "go.dtapp.net/library/utils/only" "gorm.io/gorm" "log" "runtime" @@ -105,7 +105,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) { statusEdit := j.EditTask(j.db, info.Id). Select("run_id"). Updates(jobs_gorm_model.Task{ - RunId: gouuid.GetUuId(), + RunId: only.GetUuId(), }) if statusEdit.RowsAffected == 0 { log.Println("statusEdit", statusEdit.Error) @@ -120,7 +120,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) { Updates(jobs_gorm_model.Task{ StatusDesc: "执行成功", Number: info.Number + 1, - RunId: gouuid.GetUuId(), + RunId: only.GetUuId(), UpdatedIp: j.config.OutsideIp, Result: desc, }) @@ -150,7 +150,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) { Updates(jobs_gorm_model.Task{ StatusDesc: "执行失败", Number: info.Number + 1, - RunId: gouuid.GetUuId(), + RunId: only.GetUuId(), UpdatedIp: j.config.OutsideIp, Result: desc, }) @@ -206,7 +206,7 @@ func (j *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error { Params: config.Params, StatusDesc: "首次添加任务", Frequency: config.Frequency, - RunId: gouuid.GetUuId(), + RunId: only.GetUuId(), CustomId: config.CustomId, CustomSequence: config.CustomSequence, Type: config.Type, @@ -242,7 +242,7 @@ func (j *JobsGorm) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) erro Params: config.Params, StatusDesc: "首次添加任务", Frequency: config.Frequency, - RunId: gouuid.GetUuId(), + RunId: only.GetUuId(), CustomId: config.CustomId, CustomSequence: config.CustomSequence, Type: config.Type, @@ -276,7 +276,7 @@ func (j *JobsGorm) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNu StatusDesc: "首次添加任务", Frequency: config.Frequency, MaxNumber: config.MaxNumber, - RunId: gouuid.GetUuId(), + RunId: only.GetUuId(), CustomId: config.CustomId, CustomSequence: config.CustomSequence, Type: config.Type, @@ -314,7 +314,7 @@ func (j *JobsGorm) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdM StatusDesc: "首次添加任务", Frequency: config.Frequency, MaxNumber: config.MaxNumber, - RunId: gouuid.GetUuId(), + RunId: only.GetUuId(), CustomId: config.CustomId, CustomSequence: config.CustomSequence, Type: config.Type, diff --git a/utils/gosnowflake/gosnowflake.go b/utils/gosnowflake/gosnowflake.go new file mode 100644 index 00000000..ec2ac46e --- /dev/null +++ b/utils/gosnowflake/gosnowflake.go @@ -0,0 +1,55 @@ +package gosnowflake + +import ( + "errors" + "sync" + "time" +) + +const ( + workerBits uint8 = 10 + numberBits uint8 = 12 + workerMax int64 = -1 ^ (-1 << workerBits) + numberMax int64 = -1 ^ (-1 << numberBits) + timeShift uint8 = workerBits + numberBits + workerShift uint8 = numberBits + startTime int64 = 1525705533000 // 如果在程序跑了一段时间修改了epoch这个值 可能会导致生成相同的ID +) + +type Worker struct { + mu sync.Mutex + timestamp int64 + workerId int64 + number int64 +} + +func NewWorker(workerId int64) (*Worker, error) { + if workerId < 0 || workerId > workerMax { + return nil, errors.New("worker ID excess of quantity") + } + // 生成一个新节点 + return &Worker{ + timestamp: 0, + workerId: workerId, + number: 0, + }, nil +} + +func (w *Worker) GetId() int64 { + w.mu.Lock() + defer w.mu.Unlock() + now := time.Now().UnixNano() / 1e6 + if w.timestamp == now { + w.number++ + if w.number > numberMax { + for now <= w.timestamp { + now = time.Now().UnixNano() / 1e6 + } + } + } else { + w.number = 0 + w.timestamp = now + } + ID := (now-startTime)< dataLength { + return "", errors.New("超出长度") + } else if dateFormatLength+customIdLength == dataLength { + return "", errors.New("无法使用时间方法") + } else if randomLength < 3 { + return "", errors.New("自定义的数据过长") + } + return fmt.Sprintf("%v%s%s", customId, gotime.Current().SetFormat(dateFormat), gorandom.Numeric(randomLength)), nil +} + +// 生成18位时间[年月日时]唯一编号 +func generateIdTwo(customId string) (string, error) { + + var ( + randomLength = 4 // 随机数据长度 + customIdLength = len(customId) // 自定义长度 + ) + + const ( + dataLength = 18 // 默认数据长度 + dateFormat = "2006010215" // 时间格式 + dateFormatLength = len(dateFormat) // 时间格式长度 + ) + + // 重新计算随机数据长度 + randomLength = dataLength - (dateFormatLength + customIdLength) + + if dateFormatLength+customIdLength > dataLength { + return "", errors.New("超出长度") + } else if dateFormatLength+customIdLength == dataLength { + return "", errors.New("无法使用时间方法") + } else if randomLength < 3 { + return "", errors.New("自定义的数据过长") + } + return fmt.Sprintf("%v%s%s", customId, gotime.Current().SetFormat(dateFormat), gorandom.Numeric(randomLength)), nil +} + +// 生成18位时间[年月日]唯一编号 +func generateIdThree(customId string) (string, error) { + + var ( + randomLength = 4 // 随机数据长度 + customIdLength = len(customId) // 自定义长度 + ) + + const ( + dataLength = 18 // 默认数据长度 + dateFormat = "20060102" // 时间格式 + dateFormatLength = len(dateFormat) // 时间格式长度 + ) + + // 重新计算随机数据长度 + randomLength = dataLength - (dateFormatLength + customIdLength) + + if dateFormatLength+customIdLength > dataLength { + return "", errors.New("超出长度") + } else if dateFormatLength+customIdLength == dataLength { + return "", errors.New("无法使用时间方法") + } else if randomLength < 3 { + return "", errors.New("自定义的数据过长") + } + return fmt.Sprintf("%v%s%s", customId, gotime.Current().SetFormat(dateFormat), gorandom.Numeric(randomLength)), nil +} + +// 生成18位随机唯一编号 +func generateIdFour(customId string) (string, error) { + + var ( + randomLength = 4 // 随机数据长度 + customIdLength = len(customId) // 自定义长度 + ) + + const ( + dataLength = 18 // 默认数据长度 + ) + + // 重新计算随机数据长度 + randomLength = dataLength - customIdLength + + if customIdLength >= dataLength { + return "", errors.New("超出长度") + } else if randomLength < 3 { + return "", errors.New("自定义的数据过长") + } + return fmt.Sprintf("%v%s", customId, gorandom.Numeric(randomLength)), nil +} diff --git a/utils/only/only.go b/utils/only/only.go new file mode 100644 index 00000000..aad79c4e --- /dev/null +++ b/utils/only/only.go @@ -0,0 +1,8 @@ +package only + +type Only struct { +} + +func NewOnly() *Only { + return &Only{} +} diff --git a/utils/gouuid/gouuid.go b/utils/only/uuid.go similarity index 76% rename from utils/gouuid/gouuid.go rename to utils/only/uuid.go index 754fee6b..40d57b8c 100644 --- a/utils/gouuid/gouuid.go +++ b/utils/only/uuid.go @@ -1,4 +1,4 @@ -package gouuid +package only import ( "crypto/rand" @@ -6,7 +6,7 @@ import ( "time" ) -// GetUuId 获取唯一ID +// GetUuId 由 32 个十六进制数字组成,以 6 个组显示,由连字符 - 分隔 func GetUuId() string { unix32bits := uint32(time.Now().UTC().Unix()) buff := make([]byte, 12)