feat usn sid 改成string

This commit is contained in:
2026-01-10 16:02:23 +08:00
parent e209ec64af
commit 8c6614578b
11 changed files with 43 additions and 49 deletions

View File

@@ -30,13 +30,13 @@ var (
// ServiceProvider 服务提供者
type ServiceProvider struct {
Target string
SID int64
SID string
Addr string
}
// InstanceProvider 副本提供者
type InstanceProvider struct {
InstanceID int // 副本ID
UniqueNo int64 // 副本唯一编号
UniqueNo string // 副本唯一编号
SID string
}

View File

@@ -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 {

View File

@@ -6,7 +6,6 @@ import (
"git.hlsq.asia/mmorpg/service-common/db/etcd"
"git.hlsq.asia/mmorpg/service-common/discover/common"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/utils"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
"strconv"
@@ -88,13 +87,13 @@ func onServerChange(t mvccpb.Event_EventType, key, value string) {
case clientv3.EventTypePut:
onCBByType(common.ListenerTypeNewServer, &common.ServiceProvider{
Target: common.KeyDiscoverService + "/" + split[2],
SID: utils.StringToInt64(split[3]),
SID: split[3],
Addr: value,
})
case clientv3.EventTypeDelete:
onCBByType(common.ListenerTypeCloseServer, &common.ServiceProvider{
Target: common.KeyDiscoverService + "/" + split[2],
SID: utils.StringToInt64(split[3]),
SID: split[3],
})
}
}
@@ -110,13 +109,13 @@ func onInstanceChange(t mvccpb.Event_EventType, key, value string, preKv *mvccpb
case clientv3.EventTypePut:
onCBByType(common.ListenerTypeNewInstance, &common.InstanceProvider{
InstanceID: instanceID,
UniqueNo: utils.StringToInt64(split[3]),
UniqueNo: split[3],
SID: value,
})
case clientv3.EventTypeDelete:
onCBByType(common.ListenerTypeCloseInstance, &common.InstanceProvider{
InstanceID: instanceID,
UniqueNo: utils.StringToInt64(split[3]),
UniqueNo: split[3],
SID: string(preKv.Value),
})
}

View File

@@ -15,7 +15,7 @@ import (
var (
serverMU = sync.RWMutex{}
conn = make(map[string]*grpc_conn.GrpcConnectionMgr)
serverLeaseM = make(map[int64]clientv3.LeaseID)
serverLeaseM = make(map[string]clientv3.LeaseID)
)
func init() {
@@ -24,7 +24,7 @@ func init() {
}
// FindServer 根据SID或随机查找服务
func FindServer(target string, sid ...int64) (*grpc.ClientConn, error) {
func FindServer(target string, sid ...string) (*grpc.ClientConn, error) {
serverMU.RLock()
defer serverMU.RUnlock()
if v, ok := conn[target]; ok {
@@ -33,17 +33,17 @@ func FindServer(target string, sid ...int64) (*grpc.ClientConn, error) {
return nil, fmt.Errorf("cannot find server")
}
func FindServerAll(target string) map[int64]*grpc.ClientConn {
func FindServerAll(target string) map[string]*grpc.ClientConn {
serverMU.RLock()
defer serverMU.RUnlock()
if v, ok := conn[target]; ok {
return v.LoadAll()
}
return make(map[int64]*grpc.ClientConn)
return make(map[string]*grpc.ClientConn)
}
// RegisterGrpcServer 注册服务提供者
func RegisterGrpcServer(target string, sid int64, addr string, ttl int64) error {
func RegisterGrpcServer(target string, sid string, addr string, ttl int64) error {
serverMU.Lock()
defer serverMU.Unlock()
leaseID, err := common.NewLeaseAndKeepAlive(ttl)
@@ -59,7 +59,7 @@ func RegisterGrpcServer(target string, sid int64, addr string, ttl int64) error
}
// UnRegisterGrpcServer 解注册服务提供者
func UnRegisterGrpcServer(sid int64) {
func UnRegisterGrpcServer(sid string) {
serverMU.Lock()
defer serverMU.Unlock()
if leaseID, ok := serverLeaseM[sid]; ok {

View File

@@ -9,11 +9,11 @@ import (
)
type GrpcConnection struct {
sid int64
sid string
conn *grpc.ClientConn
}
func NewGrpcConnection(sid int64, address string) (*GrpcConnection, error) {
func NewGrpcConnection(sid string, address string) (*GrpcConnection, error) {
p := &GrpcConnection{
sid: sid,
}

View File

@@ -8,18 +8,18 @@ import (
)
type GrpcConnectionMgr struct {
poolM map[int64]*GrpcConnection
poolM map[string]*GrpcConnection
poolS []*GrpcConnection
}
func NewGrpcConnectionMgr() *GrpcConnectionMgr {
return &GrpcConnectionMgr{
poolM: make(map[int64]*GrpcConnection),
poolM: make(map[string]*GrpcConnection),
poolS: make([]*GrpcConnection, 0),
}
}
func (p *GrpcConnectionMgr) Store(sid int64, addr string) {
func (p *GrpcConnectionMgr) Store(sid string, addr string) {
pool, err := NewGrpcConnection(sid, addr)
if err != nil {
log.Errorf("create grpc err: %v, sid: %v, addr: %v", err, sid, addr)
@@ -29,7 +29,7 @@ func (p *GrpcConnectionMgr) Store(sid int64, addr string) {
p.poolS = append(p.poolS, pool)
}
func (p *GrpcConnectionMgr) Delete(sid int64) int {
func (p *GrpcConnectionMgr) Delete(sid string) int {
delete(p.poolM, sid)
for i, pool := range p.poolS {
if pool.sid == sid {
@@ -40,9 +40,9 @@ func (p *GrpcConnectionMgr) Delete(sid int64) int {
return len(p.poolS)
}
func (p *GrpcConnectionMgr) Load(sid ...int64) (*grpc.ClientConn, error) {
func (p *GrpcConnectionMgr) Load(sid ...string) (*grpc.ClientConn, error) {
var pool *GrpcConnection
if len(sid) > 0 && sid[0] > 0 {
if len(sid) > 0 && sid[0] != "" {
pool = p.poolM[sid[0]]
} else {
pool = p.poolS[rand.Intn(len(p.poolS))]
@@ -53,8 +53,8 @@ func (p *GrpcConnectionMgr) Load(sid ...int64) (*grpc.ClientConn, error) {
return pool.GetConnection(), nil
}
func (p *GrpcConnectionMgr) LoadAll() map[int64]*grpc.ClientConn {
sidM := make(map[int64]*grpc.ClientConn)
func (p *GrpcConnectionMgr) LoadAll() map[string]*grpc.ClientConn {
sidM := make(map[string]*grpc.ClientConn)
for sid, pool := range p.poolM {
sidM[sid] = pool.GetConnection()
}

View File

@@ -6,7 +6,7 @@ import (
"git.hlsq.asia/mmorpg/service-common/proto/ss/grpc_pb"
)
func GatewayNewClient(sid ...int64) (grpc_pb.GatewayClient, error) {
func GatewayNewClient(sid ...string) (grpc_pb.GatewayClient, error) {
c, err := discover.FindServer(common.KeyDiscoverGateway, sid...)
if err != nil {
return nil, err
@@ -14,8 +14,8 @@ func GatewayNewClient(sid ...int64) (grpc_pb.GatewayClient, error) {
return grpc_pb.NewGatewayClient(c), nil
}
func GatewayNewBroadcastClient() map[int64]grpc_pb.GatewayClient {
clientM := make(map[int64]grpc_pb.GatewayClient)
func GatewayNewBroadcastClient() map[string]grpc_pb.GatewayClient {
clientM := make(map[string]grpc_pb.GatewayClient)
connM := discover.FindServerAll(common.KeyDiscoverGateway)
for sid, conn := range connM {
clientM[sid] = grpc_pb.NewGatewayClient(conn)

View File

@@ -7,7 +7,7 @@ import (
"git.hlsq.asia/mmorpg/service-common/proto/ss/grpc_pb"
)
func SceneNewClient(sid ...int64) (grpc_pb.SceneClient, error) {
func SceneNewClient(sid ...string) (grpc_pb.SceneClient, error) {
c, err := discover.FindServer(common.KeyDiscoverScene, sid...)
if err != nil {
return nil, err
@@ -23,8 +23,8 @@ func SceneNewClientLB() (grpc_pb.SceneClient, error) {
return grpc_pb.NewSceneClient(c), nil
}
func SceneNewBroadcastClient() map[int64]grpc_pb.SceneClient {
clientM := make(map[int64]grpc_pb.SceneClient)
func SceneNewBroadcastClient() map[string]grpc_pb.SceneClient {
clientM := make(map[string]grpc_pb.SceneClient)
connM := discover.FindServerAll(common.KeyDiscoverScene)
for sid, conn := range connM {
clientM[sid] = grpc_pb.NewSceneClient(conn)

View File

@@ -7,7 +7,7 @@ import (
"git.hlsq.asia/mmorpg/service-common/proto/ss/grpc_pb"
)
func UserNewClient(sid ...int64) (grpc_pb.UserClient, error) {
func UserNewClient(sid ...string) (grpc_pb.UserClient, error) {
c, err := discover.FindServer(common.KeyDiscoverUser, sid...)
if err != nil {
return nil, err

View File

@@ -22,7 +22,7 @@ type IService interface {
type Base struct {
Target string
SID int64
SID string
Serve *grpc.Server
EtcdTTL int64
OnInit func(serve *grpc.Server)
@@ -34,7 +34,7 @@ type Base struct {
func (s *Base) Init(addr string, port int32) {
s.wg = &sync.WaitGroup{}
s.wg.Add(1)
s.SID = utils.SnowflakeInstance().Generate().Int64()
s.SID = utils.SnowflakeInstance().Generate().String()
go func() {
defer s.wg.Done()
defer s.OnClose()

View File

@@ -5,16 +5,15 @@ import (
"errors"
"github.com/golang-jwt/jwt/v5"
"google.golang.org/grpc/metadata"
"strconv"
"time"
)
type Claims struct {
USN int64 `json:"usn"`
USN string `json:"usn"`
jwt.RegisteredClaims
}
func GenToken(usn int64, secret string, expires time.Duration) (string, error) {
func GenToken(usn string, secret string, expires time.Duration) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, Claims{
USN: usn,
RegisteredClaims: jwt.RegisteredClaims{
@@ -41,16 +40,13 @@ func ParseToken(tokenString string, secret string) (*Claims, error) {
return claims, nil
}
func ShouldBindUsn(ctx context.Context, usn *int64) bool {
func ShouldBindUsn(ctx context.Context, usn *string) bool {
if md, ok := metadata.FromIncomingContext(ctx); ok {
usnArr := md.Get("X-Usn")
if len(usnArr) == 0 || usnArr[0] == "" {
return false
}
s, _ := strconv.Atoi(usnArr[0])
if s > 0 {
*usn = int64(s)
*usn = usnArr[0]
}
}
return *usn > 0
return *usn != ""
}