feat sn 改成 int64

This commit is contained in:
2026-01-30 11:53:20 +08:00
parent 427fca7ed1
commit 5dc5391b07
20 changed files with 189 additions and 182 deletions

View File

@@ -6,7 +6,7 @@ import (
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
)
func GatewayNewClient(sid ...string) (grpc_pb.GatewayClient, error) {
func GatewayNewClient(sid ...int64) (grpc_pb.GatewayClient, error) {
c, err := discover.FindServer(common.KeyDiscoverGateway, sid...)
if err != nil {
return nil, err
@@ -14,8 +14,8 @@ func GatewayNewClient(sid ...string) (grpc_pb.GatewayClient, error) {
return grpc_pb.NewGatewayClient(c), nil
}
func GatewayNewBroadcastClient() map[string]grpc_pb.GatewayClient {
clientM := make(map[string]grpc_pb.GatewayClient)
func GatewayNewBroadcastClient() map[int64]grpc_pb.GatewayClient {
clientM := make(map[int64]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/rs/grpc_pb"
)
func QgdzsNewClient(sid ...string) (grpc_pb.QgdzsClient, error) {
func QgdzsNewClient(sid ...int64) (grpc_pb.QgdzsClient, error) {
c, err := discover.FindServer(common.KeyDiscoverQgdzs, sid...)
if err != nil {
return nil, err

View File

@@ -7,7 +7,7 @@ import (
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
)
func SceneNewClient(sid ...string) (grpc_pb.SceneClient, error) {
func SceneNewClient(sid ...int64) (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[string]grpc_pb.SceneClient {
clientM := make(map[string]grpc_pb.SceneClient)
func SceneNewBroadcastClient() map[int64]grpc_pb.SceneClient {
clientM := make(map[int64]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/rs/grpc_pb"
)
func UserNewClient(sid ...string) (grpc_pb.UserClient, error) {
func UserNewClient(sid ...int64) (grpc_pb.UserClient, error) {
c, err := discover.FindServer(common.KeyDiscoverUser, sid...)
if err != nil {
return nil, err

View File

@@ -2,10 +2,10 @@ package grpc_client
import (
"context"
"fmt"
"git.hlsq.asia/mmorpg/service-common/log"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"strconv"
"sync"
)
@@ -22,7 +22,7 @@ type gatewayStream struct {
stream grpc.ClientStream
}
func findGatewayBySID(sid string, fun GatewayFun) (*gatewayStream, error) {
func findGatewayBySID(sid int64, fun GatewayFun) (*gatewayStream, error) {
key := gatewayKey(sid, fun)
if v, ok := gatewayServer.Load(key); ok {
@@ -53,7 +53,7 @@ func findGatewayBySID(sid string, fun GatewayFun) (*gatewayStream, error) {
return ss, nil
}
func SendMessageToGateway(sid string, fun GatewayFun, msg proto.Message, re ...bool) error {
func SendMessageToGateway(sid int64, fun GatewayFun, msg proto.Message, re ...bool) error {
ss, err := findGatewayBySID(sid, fun)
if err != nil {
return err
@@ -78,6 +78,6 @@ func SendMessageToGateway(sid string, fun GatewayFun, msg proto.Message, re ...b
return nil
}
func gatewayKey(sid string, fun GatewayFun) string {
return sid + "-" + strconv.Itoa(int(fun))
func gatewayKey(sid int64, fun GatewayFun) string {
return fmt.Sprintf("%v-%v", sid, fun)
}

View File

@@ -2,10 +2,10 @@ package grpc_client
import (
"context"
"fmt"
"git.hlsq.asia/mmorpg/service-common/log"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"strconv"
"sync"
)
@@ -22,7 +22,7 @@ type sceneStream struct {
stream grpc.ClientStream
}
func findSceneBySID(sid string, fun SceneFun) (*sceneStream, error) {
func findSceneBySID(sid int64, fun SceneFun) (*sceneStream, error) {
key := sceneKey(sid, fun)
if v, ok := sceneServer.Load(key); ok {
@@ -53,7 +53,7 @@ func findSceneBySID(sid string, fun SceneFun) (*sceneStream, error) {
return ss, nil
}
func SendMessageToScene(sid string, fun SceneFun, msg proto.Message, re ...bool) error {
func SendMessageToScene(sid int64, fun SceneFun, msg proto.Message, re ...bool) error {
ss, err := findSceneBySID(sid, fun)
if err != nil {
return err
@@ -78,6 +78,6 @@ func SendMessageToScene(sid string, fun SceneFun, msg proto.Message, re ...bool)
return nil
}
func sceneKey(sid string, fun SceneFun) string {
return sid + "-" + strconv.Itoa(int(fun))
func sceneKey(sid int64, fun SceneFun) string {
return fmt.Sprintf("%v-%v", sid, fun)
}

View File

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

View File

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

View File

@@ -25,7 +25,7 @@ type IService interface {
type Base struct {
Target string
ServiceName string
SID string
SID int64
Serve *grpc.Server
Cfg *config.GrpcConfig
OnCustomGrpcServerOption func() []grpc.ServerOption
@@ -39,7 +39,7 @@ type Base struct {
func (s *Base) Init(ready *sync.WaitGroup) {
s.wg = &sync.WaitGroup{}
s.wg.Add(1)
s.SID = utils.SnowflakeInstance().Generate().String()
s.SID = utils.SnowflakeInstance().Generate().Int64()
go func() {
defer s.wg.Done()
defer s.OnClose()