Skip to content

feat: add register with client #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions regworkerid/reghelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down