diff --git a/go.mod b/go.mod index a6163c3..455c524 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.hlsq.asia/mmorpg/service-scene go 1.23.1 require ( - git.hlsq.asia/mmorpg/service-common v0.0.0-20260112082258-b1e7d33940d7 + git.hlsq.asia/mmorpg/service-common v0.0.0-20260113014617-7812a3c669d7 github.com/judwhite/go-svc v1.2.1 go.uber.org/zap v1.27.0 google.golang.org/grpc v1.71.1 diff --git a/go.sum b/go.sum index 301cf39..a8fc9ab 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260112082258-b1e7d33940d7 h1:C3quCA54dyFgmlCVgJXx+0rNqa+JZgGggdotbvHAsnA= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260112082258-b1e7d33940d7/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc= +git.hlsq.asia/mmorpg/service-common v0.0.0-20260113014617-7812a3c669d7 h1:sNZWEsAy4G5HEigdnnIHQ4eqmifN3rDPPApeTG4c4h4= +git.hlsq.asia/mmorpg/service-common v0.0.0-20260113014617-7812a3c669d7/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc= github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= diff --git a/internal/grpc_server/stream_client/gateway.go b/internal/grpc_server/stream_client/gateway.go deleted file mode 100644 index c43e22b..0000000 --- a/internal/grpc_server/stream_client/gateway.go +++ /dev/null @@ -1,84 +0,0 @@ -package stream_client - -import ( - "context" - "git.hlsq.asia/mmorpg/service-common/log" - "git.hlsq.asia/mmorpg/service-common/net/grpc/service" - "google.golang.org/grpc" - "google.golang.org/protobuf/proto" - "strconv" - "sync" -) - -type GatewayFun int - -const ( - FunToClient GatewayFun = iota -) - -var gatewayServer sync.Map // map[string]*gatewayStream - -type gatewayStream struct { - mu sync.Mutex - stream grpc.ClientStream -} - -func findGatewayBySID(sid string, fun GatewayFun) (*gatewayStream, error) { - key := gatewayKey(sid, fun) - - if v, ok := gatewayServer.Load(key); ok { - return v.(*gatewayStream), nil - } - - client, err := service.GatewayNewClient(sid) - if err != nil { - log.Errorf("findGatewayBySID cannot find client: %v", err) - return nil, err - } - var stream grpc.ClientStream - switch fun { - case FunToClient: - stream, err = client.ToClient(context.Background()) - } - if err != nil { - log.Errorf("findGatewayBySID %v err: %v, sid: %v", fun, err, sid) - return nil, err - } - - ss := &gatewayStream{stream: stream} - if actual, loaded := gatewayServer.LoadOrStore(key, ss); loaded { - go func() { _ = stream.CloseSend() }() - return actual.(*gatewayStream), nil - } - - return ss, nil -} - -func SendMessageToGateway(sid string, fun GatewayFun, msg proto.Message, re ...bool) error { - ss, err := findGatewayBySID(sid, fun) - if err != nil { - return err - } - - ss.mu.Lock() - err = ss.stream.SendMsg(msg) - ss.mu.Unlock() - - if err != nil { - key := gatewayKey(sid, fun) - if v, ok := gatewayServer.Load(key); ok && v == ss { - gatewayServer.Delete(key) - _ = ss.stream.CloseSend() - } - // 如果没有标识本次是重试的,就重试一次(默认重试) - if re == nil || !re[0] { - return SendMessageToGateway(sid, fun, msg, true) - } - return err - } - return nil -} - -func gatewayKey(sid string, fun GatewayFun) string { - return sid + "-" + strconv.Itoa(int(fun)) -} diff --git a/internal/instance/instance.go b/internal/instance/instance.go index 7616b22..7e8f7a9 100644 --- a/internal/instance/instance.go +++ b/internal/instance/instance.go @@ -5,10 +5,10 @@ import ( "fmt" "git.hlsq.asia/mmorpg/service-common/discover" "git.hlsq.asia/mmorpg/service-common/log" + "git.hlsq.asia/mmorpg/service-common/net/grpc/stream_client" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb" "git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb" "git.hlsq.asia/mmorpg/service-common/utils" - "git.hlsq.asia/mmorpg/service-scene/internal/grpc_server/stream_client" "git.hlsq.asia/mmorpg/service-scene/internal/npc" "go.uber.org/zap" "google.golang.org/protobuf/proto"