feat 奇怪的知识1.0.1
This commit is contained in:
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.23.1
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
bou.ke/monkey v1.0.2
|
bou.ke/monkey v1.0.2
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260112082258-b1e7d33940d7
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260113014617-7812a3c669d7
|
||||||
github.com/alicebob/miniredis/v2 v2.35.0
|
github.com/alicebob/miniredis/v2 v2.35.0
|
||||||
github.com/gin-contrib/cors v1.7.6
|
github.com/gin-contrib/cors v1.7.6
|
||||||
github.com/gin-gonic/gin v1.11.0
|
github.com/gin-gonic/gin v1.11.0
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -2,8 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
|
|||||||
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
|
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
|
||||||
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/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
|
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
|
||||||
|
|||||||
@@ -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 SceneFun int
|
|
||||||
|
|
||||||
const (
|
|
||||||
FunAction SceneFun = iota
|
|
||||||
)
|
|
||||||
|
|
||||||
var sceneServer sync.Map // map[string]*sceneStream
|
|
||||||
|
|
||||||
type sceneStream struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
stream grpc.ClientStream
|
|
||||||
}
|
|
||||||
|
|
||||||
func findSceneBySID(sid string, fun SceneFun) (*sceneStream, error) {
|
|
||||||
key := sceneKey(sid, fun)
|
|
||||||
|
|
||||||
if v, ok := sceneServer.Load(key); ok {
|
|
||||||
return v.(*sceneStream), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := service.SceneNewClient(sid)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("findSceneBySID cannot find client: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var stream grpc.ClientStream
|
|
||||||
switch fun {
|
|
||||||
case FunAction:
|
|
||||||
stream, err = client.Action(context.Background())
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("findSceneBySID %v err: %v, sid: %v", fun, err, sid)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ss := &sceneStream{stream: stream}
|
|
||||||
if actual, loaded := sceneServer.LoadOrStore(key, ss); loaded {
|
|
||||||
go func() { _ = stream.CloseSend() }()
|
|
||||||
return actual.(*sceneStream), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return ss, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func SendMessageToScene(sid string, fun SceneFun, msg proto.Message, re ...bool) error {
|
|
||||||
ss, err := findSceneBySID(sid, fun)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
ss.mu.Lock()
|
|
||||||
err = ss.stream.SendMsg(msg)
|
|
||||||
ss.mu.Unlock()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
key := sceneKey(sid, fun)
|
|
||||||
if v, ok := sceneServer.Load(key); ok && v == ss {
|
|
||||||
sceneServer.Delete(key)
|
|
||||||
_ = ss.stream.CloseSend()
|
|
||||||
}
|
|
||||||
// 如果没有标识本次是重试的,就重试一次(默认重试)
|
|
||||||
if re == nil || !re[0] {
|
|
||||||
return SendMessageToScene(sid, fun, msg, true)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sceneKey(sid string, fun SceneFun) string {
|
|
||||||
return sid + "-" + strconv.Itoa(int(fun))
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package stream_client
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
|
|
||||||
"github.com/stretchr/testify/suite"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SceneTestSuite struct {
|
|
||||||
testutil.TestSuite
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts *SceneTestSuite) TestSceneKey() {
|
|
||||||
r := sceneKey(1122, FunAction)
|
|
||||||
ts.Assert().Equal("1122-0", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoginTestSuite(t *testing.T) {
|
|
||||||
suite.Run(t, &SceneTestSuite{})
|
|
||||||
}
|
|
||||||
@@ -5,10 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
|
||||||
|
"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-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/grpc_server/stream_client"
|
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user