|
@@ -5,11 +5,12 @@ import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
"github.com/go-redis/redis/v8" |
|
|
"github.com/go-redis/redis/v8" |
|
|
"strconv" |
|
|
"strconv" |
|
|
|
|
|
"strings" |
|
|
"sync" |
|
|
"sync" |
|
|
"time" |
|
|
"time" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var _client *redis.Client |
|
|
|
|
|
|
|
|
var _client redis.UniversalClient |
|
|
var _ctx = context.Background() |
|
|
var _ctx = context.Background() |
|
|
var _workerIdLock sync.Mutex |
|
|
var _workerIdLock sync.Mutex |
|
|
|
|
|
|
|
@@ -25,11 +26,22 @@ var _MaxWorkerId int32 = 0 // 最大WorkerId值,超过此值从0开 |
|
|
|
|
|
|
|
|
var _RedisConnString = "" |
|
|
var _RedisConnString = "" |
|
|
var _RedisPassword = "" |
|
|
var _RedisPassword = "" |
|
|
|
|
|
var _RedisDB = 0 |
|
|
|
|
|
var _RedisMasterName = "" |
|
|
|
|
|
|
|
|
const _WorkerIdIndexKey string = "IdGen:WorkerId:Index" // redis 中的key |
|
|
const _WorkerIdIndexKey string = "IdGen:WorkerId:Index" // redis 中的key |
|
|
const _WorkerIdValueKeyPrefix string = "IdGen:WorkerId:Value:" // redis 中的key |
|
|
const _WorkerIdValueKeyPrefix string = "IdGen:WorkerId:Value:" // redis 中的key |
|
|
const _WorkerIdFlag = "Y" // IdGen:WorkerId:Value:xx 的值(将来可用 _token 替代) |
|
|
const _WorkerIdFlag = "Y" // IdGen:WorkerId:Value:xx 的值(将来可用 _token 替代) |
|
|
const _Log = false // 是否输出日志 |
|
|
|
|
|
|
|
|
const _Log = false // 是否输出日志 |
|
|
|
|
|
|
|
|
|
|
|
type RegisterConf struct { |
|
|
|
|
|
Address string // 注意:哨兵模式下,这里传入的是 Sentinel 节点,不是 Redis 节点 |
|
|
|
|
|
Password string |
|
|
|
|
|
DB int |
|
|
|
|
|
MasterName string // 注意:哨兵模式下,这里必须传入 Sentinel 服务名称 |
|
|
|
|
|
MaxWorkerId int32 |
|
|
|
|
|
TotalCount int32 // 注意:仅对 RegisterMany 生效 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func Validate(workerId int32) int32 { |
|
|
func Validate(workerId int32) int32 { |
|
|
for _, value := range _workerIdList { |
|
|
for _, value := range _workerIdList { |
|
@@ -68,20 +80,24 @@ func autoUnRegister() { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RegisterMany(ip string, port int32, password string, maxWorkerId int32, totalCount int32) []int32 { |
|
|
|
|
|
if maxWorkerId < 0 { |
|
|
|
|
|
|
|
|
func RegisterMany(conf RegisterConf) []int32 { |
|
|
|
|
|
if conf.MaxWorkerId < 0 { |
|
|
return []int32{-2} |
|
|
return []int32{-2} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if totalCount < 1 { |
|
|
|
|
|
|
|
|
if conf.TotalCount < 1 { |
|
|
return []int32{-1} |
|
|
return []int32{-1} |
|
|
|
|
|
} else if conf.TotalCount == 0 { |
|
|
|
|
|
conf.TotalCount = 1 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
autoUnRegister() |
|
|
autoUnRegister() |
|
|
|
|
|
|
|
|
_MaxWorkerId = maxWorkerId |
|
|
|
|
|
_RedisConnString = ip + ":" + strconv.Itoa(int(port)) |
|
|
|
|
|
_RedisPassword = password |
|
|
|
|
|
|
|
|
_MaxWorkerId = conf.MaxWorkerId |
|
|
|
|
|
_RedisConnString = conf.Address |
|
|
|
|
|
_RedisPassword = conf.Password |
|
|
|
|
|
_RedisDB = conf.DB |
|
|
|
|
|
_RedisMasterName = conf.MasterName |
|
|
_client = newRedisClient() |
|
|
_client = newRedisClient() |
|
|
if _client == nil { |
|
|
if _client == nil { |
|
|
return []int32{-1} |
|
|
return []int32{-1} |
|
@@ -102,7 +118,7 @@ func RegisterMany(ip string, port int32, password string, maxWorkerId int32, tot |
|
|
//} |
|
|
//} |
|
|
|
|
|
|
|
|
_lifeIndex++ |
|
|
_lifeIndex++ |
|
|
_workerIdList = make([]int32, totalCount) |
|
|
|
|
|
|
|
|
_workerIdList = make([]int32, conf.TotalCount) |
|
|
for key := range _workerIdList { |
|
|
for key := range _workerIdList { |
|
|
_workerIdList[key] = -1 // 全部初始化-1 |
|
|
_workerIdList[key] = -1 // 全部初始化-1 |
|
|
} |
|
|
} |
|
@@ -125,16 +141,18 @@ func RegisterMany(ip string, port int32, password string, maxWorkerId int32, tot |
|
|
return _workerIdList |
|
|
return _workerIdList |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RegisterOne(ip string, port int32, password string, maxWorkerId int32) int32 { |
|
|
|
|
|
if maxWorkerId < 0 { |
|
|
|
|
|
|
|
|
func RegisterOne(conf RegisterConf) int32 { |
|
|
|
|
|
if conf.MaxWorkerId < 0 { |
|
|
return -2 |
|
|
return -2 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
autoUnRegister() |
|
|
autoUnRegister() |
|
|
|
|
|
|
|
|
_MaxWorkerId = maxWorkerId |
|
|
|
|
|
_RedisConnString = ip + ":" + strconv.Itoa(int(port)) |
|
|
|
|
|
_RedisPassword = password |
|
|
|
|
|
|
|
|
_MaxWorkerId = conf.MaxWorkerId |
|
|
|
|
|
_RedisConnString = conf.Address |
|
|
|
|
|
_RedisPassword = conf.Password |
|
|
|
|
|
_RedisDB = conf.DB |
|
|
|
|
|
_RedisMasterName = conf.MasterName |
|
|
_loopCount = 0 |
|
|
_loopCount = 0 |
|
|
_client = newRedisClient() |
|
|
_client = newRedisClient() |
|
|
if _client == nil { |
|
|
if _client == nil { |
|
@@ -170,16 +188,18 @@ func register(lifeTime int) int32 { |
|
|
return getNextWorkerId(lifeTime) |
|
|
return getNextWorkerId(lifeTime) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func newRedisClient() *redis.Client { |
|
|
|
|
|
return redis.NewClient(&redis.Options{ |
|
|
|
|
|
Addr: _RedisConnString, |
|
|
|
|
|
Password: _RedisPassword, |
|
|
|
|
|
DB: 0, |
|
|
|
|
|
|
|
|
func newRedisClient() redis.UniversalClient { |
|
|
|
|
|
client := redis.NewUniversalClient(&redis.UniversalOptions{ |
|
|
|
|
|
Addrs: strings.Split(_RedisConnString, ","), |
|
|
|
|
|
Password: _RedisPassword, |
|
|
|
|
|
DB: _RedisDB, |
|
|
|
|
|
MasterName: _RedisMasterName, |
|
|
//PoolSize: 1000, |
|
|
//PoolSize: 1000, |
|
|
//ReadTimeout: time.Millisecond * time.Duration(100), |
|
|
//ReadTimeout: time.Millisecond * time.Duration(100), |
|
|
//WriteTimeout: time.Millisecond * time.Duration(100), |
|
|
//WriteTimeout: time.Millisecond * time.Duration(100), |
|
|
//IdleTimeout: time.Second * time.Duration(60), |
|
|
//IdleTimeout: time.Second * time.Duration(60), |
|
|
}) |
|
|
}) |
|
|
|
|
|
return client |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func getNextWorkerId(lifeTime int) int32 { |
|
|
func getNextWorkerId(lifeTime int) int32 { |
|
|