feat usn sid 改成string
This commit is contained in:
@@ -6,15 +6,14 @@ import (
|
||||
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 大量读少量写的情况下,读写锁比同步Map更高效
|
||||
var (
|
||||
instanceMU = sync.RWMutex{}
|
||||
instanceM = make(map[int64]string) // [uniqueNo]sid
|
||||
instanceLeaseM = make(map[int64]clientv3.LeaseID) // [uniqueNo]
|
||||
instanceM = make(map[string]string) // [uniqueNo]sid
|
||||
instanceLeaseM = make(map[string]clientv3.LeaseID) // [uniqueNo]
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -23,7 +22,7 @@ func init() {
|
||||
}
|
||||
|
||||
// FindInstanceByUniqueNo 根据唯一标识查询副本
|
||||
func FindInstanceByUniqueNo(uniqueNO int64) (sid string) {
|
||||
func FindInstanceByUniqueNo(uniqueNO string) (sid string) {
|
||||
instanceMU.RLock()
|
||||
defer instanceMU.RUnlock()
|
||||
if c, ok := instanceM[uniqueNO]; ok {
|
||||
@@ -33,7 +32,7 @@ func FindInstanceByUniqueNo(uniqueNO int64) (sid string) {
|
||||
}
|
||||
|
||||
// RegisterInstance 注册副本
|
||||
func RegisterInstance(sid int64, instanceID int32, uniqueNo, ttl int64) error {
|
||||
func RegisterInstance(sid string, instanceID int32, uniqueNo string, ttl int64) error {
|
||||
serverMU.Lock()
|
||||
defer serverMU.Unlock()
|
||||
leaseID, err := common.NewLeaseAndKeepAlive(ttl)
|
||||
@@ -41,7 +40,7 @@ func RegisterInstance(sid int64, instanceID int32, uniqueNo, ttl int64) error {
|
||||
return err
|
||||
}
|
||||
key := fmt.Sprintf("%v/%v/%v", common.KeyDiscoverInstance, instanceID, uniqueNo)
|
||||
_, err = etcd.GetClient().Put(key, strconv.Itoa(int(sid)), clientv3.WithLease(leaseID))
|
||||
_, err = etcd.GetClient().Put(key, sid, clientv3.WithLease(leaseID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -50,7 +49,7 @@ func RegisterInstance(sid int64, instanceID int32, uniqueNo, ttl int64) error {
|
||||
}
|
||||
|
||||
// UnRegisterInstance 解注册副本
|
||||
func UnRegisterInstance(uniqueNo int64) {
|
||||
func UnRegisterInstance(uniqueNo string) {
|
||||
serverMU.Lock()
|
||||
defer serverMU.Unlock()
|
||||
if leaseID, ok := instanceLeaseM[uniqueNo]; ok {
|
||||
|
||||
Reference in New Issue
Block a user