feat 奇怪的知识1.0.1
This commit is contained in:
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module git.hlsq.asia/mmorpg/service-scene
|
|||||||
go 1.23.1
|
go 1.23.1
|
||||||
|
|
||||||
require (
|
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
|
github.com/judwhite/go-svc v1.2.1
|
||||||
go.uber.org/zap v1.27.0
|
go.uber.org/zap v1.27.0
|
||||||
google.golang.org/grpc v1.71.1
|
google.golang.org/grpc v1.71.1
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -1,7 +1,7 @@
|
|||||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
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-20260113014617-7812a3c669d7 h1:sNZWEsAy4G5HEigdnnIHQ4eqmifN3rDPPApeTG4c4h4=
|
||||||
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/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc=
|
||||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
|||||||
@@ -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))
|
|
||||||
}
|
|
||||||
@@ -5,10 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-common/discover"
|
"git.hlsq.asia/mmorpg/service-common/discover"
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"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/rs/grpc_pb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
"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"
|
"git.hlsq.asia/mmorpg/service-scene/internal/npc"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
|||||||
Reference in New Issue
Block a user