diff --git a/regworkerid/reghelper.go b/regworkerid/reghelper.go index 2dafe25..0380f25 100644 --- a/regworkerid/reghelper.go +++ b/regworkerid/reghelper.go @@ -211,6 +211,81 @@ func RegisterOne(conf RegisterConf) int32 { return id } +func RegisterOneWithClient(conf RegisterConf, redisClient redis.UniversalClient) int32 { + if conf.MaxWorkerId < 0 || conf.MinWorkerId > conf.MaxWorkerId { + return -2 + } + + _MaxWorkerId = conf.MaxWorkerId + _MinWorkerId = conf.MinWorkerId + _RedisConnString = conf.Address + _RedisPassword = conf.Password + _RedisDB = conf.DB + _RedisMasterName = conf.MasterName + _WorkerIdLifeTimeSeconds = conf.LifeTimeSeconds + _loopCount = 0 + + _client = redisClient + + autoUnRegister() + + _lifeIndex++ + var id = register(_lifeIndex) + if id > -1 { + _workerIdList = []int32{id} + go extendLifeTime(_lifeIndex) + } + + return id +} + +func RegisterManyWithClient(conf RegisterConf, redisClient redis.UniversalClient) []int32 { + if conf.MaxWorkerId < 0 || conf.MinWorkerId > conf.MaxWorkerId { + return []int32{-2} + } + + if conf.TotalCount < 1 { + return []int32{-1} + } else if conf.TotalCount == 0 { + conf.TotalCount = 1 + } + + _MaxWorkerId = conf.MaxWorkerId + _MinWorkerId = conf.MinWorkerId + _RedisConnString = conf.Address + _RedisPassword = conf.Password + _RedisDB = conf.DB + _RedisMasterName = conf.MasterName + _WorkerIdLifeTimeSeconds = conf.LifeTimeSeconds + + _client = redisClient + + autoUnRegister() + + _lifeIndex++ + _workerIdList = make([]int32, conf.TotalCount) + for key := range _workerIdList { + _workerIdList[key] = -1 // 全部初始化-1 + } + + useExtendFunc := false + for key := range _workerIdList { + id := register(_lifeIndex) + if id > -1 { + useExtendFunc = true + _workerIdList[key] = id //= append(_workerIdList, id) + } else { + break + } + } + + if useExtendFunc { + go extendLifeTime(_lifeIndex) + } + + return _workerIdList +} + func register(lifeTime int32) int32 { _loopCount = 0 return getNextWorkerId(lifeTime) @@ -234,6 +309,7 @@ func getNextWorkerId(lifeTime int32) int32 { // 获取当前 WorkerIdIndex r, err := _client.Incr(_ctx, _WorkerIdIndexKey).Result() if err != nil { + fmt.Println(err.Error()) return -1 } @@ -376,6 +452,7 @@ func extendWorkerIdLifeTime(lifeIndex int32, workerId int32) { func get(key string) (string, bool) { r, err := _client.Get(_ctx, key).Result() if err != nil { + fmt.Println(err.Error()) return "", false } return r, true @@ -384,6 +461,7 @@ func get(key string) (string, bool) { func del(key string) (int64, bool) { r, err := _client.Del(_ctx, key).Result() if err != nil { + fmt.Println(err.Error()) return 0, false } return r, true @@ -418,6 +496,7 @@ func extendWorkerIdFlag(workerId int32) { func canReset() bool { r, err := _client.Incr(_ctx, _WorkerIdValueKeyPrefix+"Edit").Result() if err != nil { + fmt.Println(err.Error()) return false } @@ -436,6 +515,7 @@ func endReset() { func getWorkerIdFlag(workerId int32) (string, bool) { r, err := _client.Get(_ctx, _WorkerIdValueKeyPrefix+strconv.Itoa(int(workerId))).Result() if err != nil { + fmt.Println(err.Error()) return "", false } return r, true