mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 14:12:27 +08:00
完善文档
This commit is contained in:
31
README.md
31
README.md
@@ -6,18 +6,31 @@ hk4e game server
|
||||
|
||||
* Go >= 1.18
|
||||
|
||||
1. 首次需要安装工具 `make dev_tool`
|
||||
2. 生成协议 `make gen`
|
||||
> 1. 首次需要安装工具 `make dev_tool`
|
||||
> 2. 生成协议 `make gen`
|
||||
|
||||
## 快速运行
|
||||
|
||||
#### 第三方组件
|
||||
|
||||
* mongodb
|
||||
* nats-server
|
||||
* redis
|
||||
|
||||
1. 启动节点服务器(仅单节点) `cmd/node && go run .`
|
||||
2. 启动http登录服务器(可多节点) `cmd/dispatch && go run .`
|
||||
3. 启动网关服务器(可多节点) `cd cmd/gate && go run .`
|
||||
4. 启动战斗服务器(可多节点) `cmd/fight && go run .`
|
||||
5. 启动寻路服务器(可多节点) `cmd/pathfinding && go run .`
|
||||
6. 启动游戏服务器(可多节点) `cd cmd/gs && go run .`
|
||||
7. 启动游戏管理服务器(仅单节点) `cmd/gm && go run .`
|
||||
#### 启动顺序
|
||||
|
||||
> 1. 启动节点服务器(仅单节点 有状态) `cmd/node && go run .`
|
||||
> 2. 启动http登录服务器(可多节点 无状态) `cmd/dispatch && go run .`
|
||||
> 3. 启动网关服务器(可多节点 有状态) `cd cmd/gate && go run .`
|
||||
> 4. 启动战斗服务器(可多节点 有状态) `cmd/fight && go run .`
|
||||
> 5. 启动寻路服务器(可多节点 无状态) `cmd/pathfinding && go run .`
|
||||
> 6. 启动游戏服务器(可多节点 有状态) `cd cmd/gs && go run .`
|
||||
> 7. 启动游戏管理服务器(仅单节点 无状态) `cmd/gm && go run .`
|
||||
|
||||
#### 其它
|
||||
|
||||
* 配置运行时环境变量
|
||||
|
||||
```shell
|
||||
GOLANG_PROTOBUF_REGISTRATION_CONFLICT=ignore
|
||||
```
|
||||
|
||||
@@ -18,10 +18,7 @@ var (
|
||||
func main() {
|
||||
flag.Parse()
|
||||
go func() {
|
||||
err := statsviz_serve.Serve("0.0.0.0:2345")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_ = statsviz_serve.Serve("0.0.0.0:2345")
|
||||
}()
|
||||
err := app.Run(context.TODO(), *config)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
[hk4e]
|
||||
kcp_addr = "127.0.0.1"
|
||||
kcp_addr = "127.0.0.1" # 该地址只用来注册到节点服务器 并非网关本地监听地址 本地监听为0.0.0.0
|
||||
kcp_port = 22103
|
||||
client_proto_proxy_enable = false
|
||||
version = "320"
|
||||
|
||||
[logger]
|
||||
level = "DEBUG"
|
||||
|
||||
@@ -38,12 +38,13 @@ type Redis struct {
|
||||
|
||||
// Hk4e 原神相关
|
||||
type Hk4e struct {
|
||||
KcpPort int32 `toml:"kcp_port"`
|
||||
KcpPort int32 `toml:"kcp_port"` // 该地址只用来注册到节点服务器 并非网关本地监听地址 本地监听为0.0.0.0
|
||||
KcpAddr string `toml:"kcp_addr"`
|
||||
ResourcePath string `toml:"resource_path"`
|
||||
GameDataConfigPath string `toml:"game_data_config_path"`
|
||||
GachaHistoryServer string `toml:"gacha_history_server"`
|
||||
ClientProtoProxyEnable bool `toml:"client_proto_proxy_enable"`
|
||||
Version string `toml:"version"`
|
||||
}
|
||||
|
||||
// MQ 消息队列
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
)
|
||||
|
||||
func (c *Controller) query_security_file(context *gin.Context) {
|
||||
// 很早以前2.6.0版本的时候抓包为了完美还原写的 不清楚有没有副作用暂时不要了
|
||||
return
|
||||
file, err := os.ReadFile("static/security_file")
|
||||
if err != nil {
|
||||
@@ -35,22 +36,11 @@ func (c *Controller) query_region_list(context *gin.Context) {
|
||||
_, _ = context.Writer.WriteString(regionListBase64)
|
||||
}
|
||||
|
||||
func (c *Controller) query_cur_region(context *gin.Context) {
|
||||
versionName := context.Query("version")
|
||||
response := "CAESGE5vdCBGb3VuZCB2ZXJzaW9uIGNvbmZpZw=="
|
||||
if len(context.Request.URL.RawQuery) > 0 {
|
||||
addr, err := c.discovery.GetGateServerAddr(context.Request.Context(), &api.NullMsg{})
|
||||
if err != nil {
|
||||
logger.Error("get gate server addr error: %v", err)
|
||||
return
|
||||
}
|
||||
regionCurrBase64 := region.GetRegionCurrBase64(addr.IpAddr, int32(addr.Port), c.ec2b)
|
||||
response = regionCurrBase64
|
||||
}
|
||||
func (c *Controller) getClientVersionByName(versionName string) (int, string) {
|
||||
reg, err := regexp.Compile("[0-9]+")
|
||||
if err != nil {
|
||||
logger.Error("compile regexp error: %v", err)
|
||||
return
|
||||
return 0, ""
|
||||
}
|
||||
versionSlice := reg.FindAllString(versionName, -1)
|
||||
version := 0
|
||||
@@ -58,7 +48,7 @@ func (c *Controller) query_cur_region(context *gin.Context) {
|
||||
v, err := strconv.Atoi(versionSlice[index])
|
||||
if err != nil {
|
||||
logger.Error("parse client version error: %v", err)
|
||||
return
|
||||
return 0, ""
|
||||
}
|
||||
for i := 0; i < len(versionSlice)-1-index; i++ {
|
||||
v *= 10
|
||||
@@ -69,88 +59,126 @@ func (c *Controller) query_cur_region(context *gin.Context) {
|
||||
// 测试版本
|
||||
version /= 10
|
||||
}
|
||||
if version >= 275 {
|
||||
logger.Debug("do hk4e 2.8 rsa logic")
|
||||
if context.Query("dispatchSeed") == "" {
|
||||
rsp := &httpapi.QueryCurRegionRspJson{
|
||||
Content: response,
|
||||
Sign: "TW9yZSBsb3ZlIGZvciBVQSBQYXRjaCBwbGF5ZXJz",
|
||||
}
|
||||
context.JSON(http.StatusOK, rsp)
|
||||
return
|
||||
}
|
||||
keyId := context.Query("key_id")
|
||||
encPubPrivKey, exist := c.encRsaKeyMap[keyId]
|
||||
if !exist {
|
||||
logger.Error("can not found key id: %v", keyId)
|
||||
return
|
||||
}
|
||||
regionInfo, err := base64.StdEncoding.DecodeString(response)
|
||||
if err != nil {
|
||||
logger.Error("decode region info error: %v", err)
|
||||
return
|
||||
}
|
||||
chunkSize := 256 - 11
|
||||
regionInfoLength := len(regionInfo)
|
||||
numChunks := int(math.Ceil(float64(regionInfoLength) / float64(chunkSize)))
|
||||
encryptedRegionInfo := make([]byte, 0)
|
||||
for i := 0; i < numChunks; i++ {
|
||||
from := i * chunkSize
|
||||
to := int(math.Min(float64((i+1)*chunkSize), float64(regionInfoLength)))
|
||||
chunk := regionInfo[from:to]
|
||||
pubKey, err := endec.RsaParsePubKeyByPrivKey(encPubPrivKey)
|
||||
if err != nil {
|
||||
logger.Error("parse rsa pub key error: %v", err)
|
||||
return
|
||||
}
|
||||
privKey, err := endec.RsaParsePrivKey(encPubPrivKey)
|
||||
if err != nil {
|
||||
logger.Error("parse rsa priv key error: %v", err)
|
||||
return
|
||||
}
|
||||
encrypt, err := endec.RsaEncrypt(chunk, pubKey)
|
||||
if err != nil {
|
||||
logger.Error("rsa enc error: %v", err)
|
||||
return
|
||||
}
|
||||
decrypt, err := endec.RsaDecrypt(encrypt, privKey)
|
||||
if err != nil {
|
||||
logger.Error("rsa dec error: %v", err)
|
||||
return
|
||||
}
|
||||
if bytes.Compare(decrypt, chunk) != 0 {
|
||||
logger.Error("rsa dec test fail")
|
||||
return
|
||||
}
|
||||
encryptedRegionInfo = append(encryptedRegionInfo, encrypt...)
|
||||
}
|
||||
signPrivkey, err := endec.RsaParsePrivKey(c.signRsaKey)
|
||||
if err != nil {
|
||||
logger.Error("parse rsa priv key error: %v", err)
|
||||
return
|
||||
}
|
||||
signData, err := endec.RsaSign(regionInfo, signPrivkey)
|
||||
if err != nil {
|
||||
logger.Error("rsa sign error: %v", err)
|
||||
return
|
||||
}
|
||||
ok, err := endec.RsaVerify(regionInfo, signData, &signPrivkey.PublicKey)
|
||||
if err != nil {
|
||||
logger.Error("rsa verify error: %v", err)
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
logger.Error("rsa verify test fail")
|
||||
return
|
||||
}
|
||||
return version, strconv.Itoa(version)
|
||||
}
|
||||
|
||||
func (c *Controller) query_cur_region(context *gin.Context) {
|
||||
rspError := func() {
|
||||
rspContentError := "CAESGE5vdCBGb3VuZCB2ZXJzaW9uIGNvbmZpZw=="
|
||||
rspSignError := "TW9yZSBsb3ZlIGZvciBVQSBQYXRjaCBwbGF5ZXJz"
|
||||
rsp := &httpapi.QueryCurRegionRspJson{
|
||||
Content: base64.StdEncoding.EncodeToString(encryptedRegionInfo),
|
||||
Sign: base64.StdEncoding.EncodeToString(signData),
|
||||
Content: rspContentError,
|
||||
Sign: rspSignError,
|
||||
}
|
||||
context.JSON(http.StatusOK, rsp)
|
||||
return
|
||||
} else {
|
||||
context.Header("Content-type", "text/html; charset=UTF-8")
|
||||
_, _ = context.Writer.WriteString(response)
|
||||
}
|
||||
versionName := context.Query("version")
|
||||
if versionName == "" {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
version, versionStr := c.getClientVersionByName(versionName)
|
||||
if version == 0 {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
addr, err := c.discovery.GetGateServerAddr(context.Request.Context(), &api.GetGateServerAddrReq{
|
||||
Version: versionStr,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("get gate server addr error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
regionCurrBase64 := region.GetRegionCurrBase64(addr.IpAddr, int32(addr.Port), c.ec2b)
|
||||
if version < 275 {
|
||||
context.Header("Content-type", "text/html; charset=UTF-8")
|
||||
_, _ = context.Writer.WriteString(regionCurrBase64)
|
||||
return
|
||||
}
|
||||
logger.Debug("do hk4e 2.8 rsa logic")
|
||||
if context.Query("dispatchSeed") == "" {
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
keyId := context.Query("key_id")
|
||||
encPubPrivKey, exist := c.encRsaKeyMap[keyId]
|
||||
if !exist {
|
||||
logger.Error("can not found key id: %v", keyId)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
regionInfo, err := base64.StdEncoding.DecodeString(regionCurrBase64)
|
||||
if err != nil {
|
||||
logger.Error("decode region info error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
chunkSize := 256 - 11
|
||||
regionInfoLength := len(regionInfo)
|
||||
numChunks := int(math.Ceil(float64(regionInfoLength) / float64(chunkSize)))
|
||||
encryptedRegionInfo := make([]byte, 0)
|
||||
for i := 0; i < numChunks; i++ {
|
||||
from := i * chunkSize
|
||||
to := int(math.Min(float64((i+1)*chunkSize), float64(regionInfoLength)))
|
||||
chunk := regionInfo[from:to]
|
||||
pubKey, err := endec.RsaParsePubKeyByPrivKey(encPubPrivKey)
|
||||
if err != nil {
|
||||
logger.Error("parse rsa pub key error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
privKey, err := endec.RsaParsePrivKey(encPubPrivKey)
|
||||
if err != nil {
|
||||
logger.Error("parse rsa priv key error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
encrypt, err := endec.RsaEncrypt(chunk, pubKey)
|
||||
if err != nil {
|
||||
logger.Error("rsa enc error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
decrypt, err := endec.RsaDecrypt(encrypt, privKey)
|
||||
if err != nil {
|
||||
logger.Error("rsa dec error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
if bytes.Compare(decrypt, chunk) != 0 {
|
||||
logger.Error("rsa dec test fail")
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
encryptedRegionInfo = append(encryptedRegionInfo, encrypt...)
|
||||
}
|
||||
signPrivkey, err := endec.RsaParsePrivKey(c.signRsaKey)
|
||||
if err != nil {
|
||||
logger.Error("parse rsa priv key error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
signData, err := endec.RsaSign(regionInfo, signPrivkey)
|
||||
if err != nil {
|
||||
logger.Error("rsa sign error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
ok, err := endec.RsaVerify(regionInfo, signData, &signPrivkey.PublicKey)
|
||||
if err != nil {
|
||||
logger.Error("rsa verify error: %v", err)
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
logger.Error("rsa verify test fail")
|
||||
rspError()
|
||||
return
|
||||
}
|
||||
rsp := &httpapi.QueryCurRegionRspJson{
|
||||
Content: base64.StdEncoding.EncodeToString(encryptedRegionInfo),
|
||||
Sign: base64.StdEncoding.EncodeToString(signData),
|
||||
}
|
||||
context.JSON(http.StatusOK, rsp)
|
||||
}
|
||||
|
||||
BIN
doc/HK4E登录流程.jpg
Normal file
BIN
doc/HK4E登录流程.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 224 KiB |
1
doc/README.md
Normal file
1
doc/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# 关于框架的一些小说明
|
||||
@@ -36,6 +36,16 @@ func Run(ctx context.Context, configFile string) error {
|
||||
return err
|
||||
}
|
||||
APPID = rsp.GetAppId()
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Second * 15)
|
||||
for {
|
||||
<-ticker.C
|
||||
_, _ = client.Discovery.KeepaliveServer(context.TODO(), &api.KeepaliveServerReq{
|
||||
ServerType: api.FIGHT,
|
||||
AppId: APPID,
|
||||
})
|
||||
}
|
||||
}()
|
||||
defer func() {
|
||||
_, _ = client.Discovery.CancelServer(context.TODO(), &api.CancelServerReq{
|
||||
ServerType: api.FIGHT,
|
||||
|
||||
@@ -238,6 +238,10 @@ func (f *FightRoutine) attackHandle(gameMsg *mq.GameMsg) {
|
||||
continue
|
||||
}
|
||||
attackResult := hitInfo.AttackResult
|
||||
if attackResult == nil {
|
||||
logger.Error("attackResult is nil")
|
||||
continue
|
||||
}
|
||||
// logger.Debug("run attack handler, attackResult: %v", attackResult)
|
||||
target := f.entityMap[attackResult.DefenseId]
|
||||
if target == nil {
|
||||
|
||||
@@ -34,11 +34,22 @@ func Run(ctx context.Context, configFile string) error {
|
||||
IpAddr: config.CONF.Hk4e.KcpAddr,
|
||||
Port: uint32(config.CONF.Hk4e.KcpPort),
|
||||
},
|
||||
Version: config.CONF.Hk4e.Version,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
APPID = rsp.GetAppId()
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Second * 15)
|
||||
for {
|
||||
<-ticker.C
|
||||
_, _ = client.Discovery.KeepaliveServer(context.TODO(), &api.KeepaliveServerReq{
|
||||
ServerType: api.GATE,
|
||||
AppId: APPID,
|
||||
})
|
||||
}
|
||||
}()
|
||||
defer func() {
|
||||
_, _ = client.Discovery.CancelServer(context.TODO(), &api.CancelServerReq{
|
||||
ServerType: api.GATE,
|
||||
|
||||
@@ -2,15 +2,14 @@
|
||||
|
||||
## 功能介绍
|
||||
|
||||
### 开启本功能后,网关服务器以及游戏服务器等其他服务器,将预先对客户端上行和服务器下行的协议数据做前置转换,采用任意版本的协议文件(必要字段名必须与现有的协议保持一致)均可,避免了因协议序号混淆等频繁变动,而造成游戏服务器代码不必要的频繁改动
|
||||
#### 开启本功能后,网关服务器以及游戏服务器等其他服务器,将预先对客户端上行和服务器下行的协议数据做前置转换,采用任意版本的协议文件(必要字段名必须与现有的协议保持一致)均可,避免了因协议序号混淆等频繁变动,而造成游戏服务器代码不必要的频繁改动
|
||||
|
||||
## 使用方法
|
||||
|
||||
1. 在此目录下建立bin目录和proto目录
|
||||
2. 将client_cmd.csv文件放到bin目录下
|
||||
3. 将对应版本的proto协议文件复制到proto目录下并编译成pb.go
|
||||
4. 将client_proto_gen_test.go的TestClientProtoGen方法添加运行配置
|
||||
5. 将运行配置输出目录和工作目录都设置为bin目录
|
||||
6. 运行并生成client_proto_gen.go
|
||||
7. 将client_cmd.csv放入gate和gs和fight服务器的运行目录下
|
||||
8. 将gate和gs和fight服务器的配置文件中开启client_proto_proxy_enable客户端协议代理功能
|
||||
> 1. 在此目录下建立bin目录和proto目录
|
||||
> 2. 将对应版本的proto协议文件复制到proto目录下并编译成pb.go
|
||||
> 3. 将client_proto_gen_test.go的TestClientProtoGen方法添加运行配置
|
||||
> 4. 将运行配置输出目录和工作目录都设置为bin目录
|
||||
> 5. 运行并生成client_proto_gen.go
|
||||
> 6. 将client_cmd.csv放入gate和gs和fight服务器的运行目录下
|
||||
> 7. 将gate和gs和fight服务器的配置文件中开启client_proto_proxy_enable客户端协议代理功能
|
||||
|
||||
@@ -14,9 +14,8 @@ func TestClientProtoGen(t *testing.T) {
|
||||
nameList := make([]string, 0)
|
||||
for _, entry := range dir {
|
||||
split := strings.Split(entry.Name(), ".")
|
||||
if len(split) != 2 {
|
||||
if len(split) != 2 || split[1] != "proto" {
|
||||
continue
|
||||
//panic("file name error")
|
||||
}
|
||||
nameList = append(nameList, split[0])
|
||||
}
|
||||
|
||||
@@ -295,7 +295,7 @@ func (k *KcpConnectManager) getPlayerToken(req *proto.GetPlayerTokenReq, session
|
||||
}
|
||||
oldSession := k.GetSessionByUserId(tokenVerifyRsp.PlayerID)
|
||||
if oldSession != nil {
|
||||
// 顶号
|
||||
// 本地顶号
|
||||
kickFinishNotifyChan := make(chan bool)
|
||||
k.kcpEventInput <- &KcpEvent{
|
||||
ConvId: oldSession.conn.GetConv(),
|
||||
@@ -303,6 +303,18 @@ func (k *KcpConnectManager) getPlayerToken(req *proto.GetPlayerTokenReq, session
|
||||
EventMessage: kickFinishNotifyChan,
|
||||
}
|
||||
<-kickFinishNotifyChan
|
||||
} else {
|
||||
// 远程全局顶号
|
||||
connCtrlMsg := new(mq.ConnCtrlMsg)
|
||||
connCtrlMsg.KickUserId = tokenVerifyRsp.PlayerID
|
||||
connCtrlMsg.KickReason = kcp.EnetServerRelogin
|
||||
k.messageQueue.SendToAll(&mq.NetMsg{
|
||||
MsgType: mq.MsgTypeConnCtrl,
|
||||
EventId: mq.KickPlayerNotify,
|
||||
ConnCtrlMsg: connCtrlMsg,
|
||||
})
|
||||
// TODO 确保旧连接已下线 已通知GS已保存好数据
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
// 关联玩家uid和连接信息
|
||||
session.userId = tokenVerifyRsp.PlayerID
|
||||
|
||||
@@ -83,7 +83,7 @@ func (k *KcpConnectManager) Start() {
|
||||
k.dispatchKey = regionEc2b.XorKey()
|
||||
// kcp
|
||||
port := strconv.Itoa(int(config.CONF.Hk4e.KcpPort))
|
||||
listener, err := kcp.ListenWithOptions(config.CONF.Hk4e.KcpAddr+":"+port, nil, 0, 0)
|
||||
listener, err := kcp.ListenWithOptions("0.0.0.0:"+port, nil, 0, 0)
|
||||
if err != nil {
|
||||
logger.Error("listen kcp err: %v", err)
|
||||
return
|
||||
|
||||
@@ -43,6 +43,16 @@ func Run(ctx context.Context, configFile string) error {
|
||||
return err
|
||||
}
|
||||
APPID = rsp.GetAppId()
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Second * 15)
|
||||
for {
|
||||
<-ticker.C
|
||||
_, _ = client.Discovery.KeepaliveServer(context.TODO(), &api.KeepaliveServerReq{
|
||||
ServerType: api.GS,
|
||||
AppId: APPID,
|
||||
})
|
||||
}
|
||||
}()
|
||||
GSID = rsp.GetGsId()
|
||||
defer func() {
|
||||
_, _ = client.Discovery.CancelServer(context.TODO(), &api.CancelServerReq{
|
||||
|
||||
@@ -263,6 +263,9 @@ func (g *GameManager) ClientAbilityChangeNotify(player *model.Player, payloadMsg
|
||||
if worldAvatar == nil {
|
||||
continue
|
||||
}
|
||||
if abilityMetaAddAbility.Ability == nil {
|
||||
continue
|
||||
}
|
||||
worldAvatar.abilityList = append(worldAvatar.abilityList, abilityMetaAddAbility.Ability)
|
||||
case proto.AbilityInvokeArgument_ABILITY_INVOKE_ARGUMENT_META_MODIFIER_CHANGE:
|
||||
abilityMetaModifierChange := new(proto.AbilityMetaModifierChange)
|
||||
|
||||
@@ -174,6 +174,7 @@ func (u *UserManager) OfflineUser(player *model.Player, changeGsInfo *ChangeGsIn
|
||||
logger.Error("deep copy player error: %v", err)
|
||||
return
|
||||
}
|
||||
playerCopy.DbState = player.DbState
|
||||
go func() {
|
||||
u.SaveUserToDbSync(playerCopy)
|
||||
u.SaveUserToRedisSync(playerCopy)
|
||||
@@ -342,6 +343,7 @@ func (u *UserManager) SaveTempOfflineUser(player *model.Player) {
|
||||
logger.Error("deep copy player error: %v", err)
|
||||
return
|
||||
}
|
||||
playerCopy.DbState = player.DbState
|
||||
go func() {
|
||||
u.SaveUserToDbSync(playerCopy)
|
||||
}()
|
||||
@@ -384,10 +386,20 @@ func (u *UserManager) LoadUserFromDbSync(userId uint32) *model.Player {
|
||||
}
|
||||
|
||||
func (u *UserManager) SaveUserToDbSync(player *model.Player) {
|
||||
err := u.dao.UpdatePlayer(player)
|
||||
if err != nil {
|
||||
logger.Error("update player error: %v", err)
|
||||
return
|
||||
if player.DbState == model.DbInsert {
|
||||
err := u.dao.InsertPlayer(player)
|
||||
if err != nil {
|
||||
logger.Error("insert player error: %v", err)
|
||||
return
|
||||
}
|
||||
} else if player.DbState == model.DbNormal {
|
||||
err := u.dao.UpdatePlayer(player)
|
||||
if err != nil {
|
||||
logger.Error("update player error: %v", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
logger.Error("invalid player db state: %v", player.DbState)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ type DiscoveryNATSRPCServer interface {
|
||||
// 获取区服密钥信息
|
||||
GetRegionEc2B(ctx context.Context, req *NullMsg) (*RegionEc2B, error)
|
||||
// 获取负载最小的网关服务器的地址和端口
|
||||
GetGateServerAddr(ctx context.Context, req *NullMsg) (*GateServerAddr, error)
|
||||
GetGateServerAddr(ctx context.Context, req *GetGateServerAddrReq) (*GateServerAddr, error)
|
||||
}
|
||||
|
||||
// RegisterDiscoveryNATSRPCServer register Discovery service
|
||||
@@ -53,7 +53,7 @@ type DiscoveryNATSRPCClient interface {
|
||||
// 获取区服密钥信息
|
||||
GetRegionEc2B(ctx context.Context, req *NullMsg, opt ...natsrpc.CallOption) (*RegionEc2B, error)
|
||||
// 获取负载最小的网关服务器的地址和端口
|
||||
GetGateServerAddr(ctx context.Context, req *NullMsg, opt ...natsrpc.CallOption) (*GateServerAddr, error)
|
||||
GetGateServerAddr(ctx context.Context, req *GetGateServerAddrReq, opt ...natsrpc.CallOption) (*GateServerAddr, error)
|
||||
}
|
||||
|
||||
type _DiscoveryNATSRPCClient struct {
|
||||
@@ -96,7 +96,7 @@ func (c *_DiscoveryNATSRPCClient) GetRegionEc2B(ctx context.Context, req *NullMs
|
||||
err := c.c.Request(ctx, "GetRegionEc2B", req, rep, opt...)
|
||||
return rep, err
|
||||
}
|
||||
func (c *_DiscoveryNATSRPCClient) GetGateServerAddr(ctx context.Context, req *NullMsg, opt ...natsrpc.CallOption) (*GateServerAddr, error) {
|
||||
func (c *_DiscoveryNATSRPCClient) GetGateServerAddr(ctx context.Context, req *GetGateServerAddrReq, opt ...natsrpc.CallOption) (*GateServerAddr, error) {
|
||||
rep := &GateServerAddr{}
|
||||
err := c.c.Request(ctx, "GetGateServerAddr", req, rep, opt...)
|
||||
return rep, err
|
||||
|
||||
@@ -159,6 +159,7 @@ type RegisterServerReq struct {
|
||||
|
||||
ServerType string `protobuf:"bytes,1,opt,name=server_type,json=serverType,proto3" json:"server_type,omitempty"`
|
||||
GateServerAddr *GateServerAddr `protobuf:"bytes,2,opt,name=gate_server_addr,json=gateServerAddr,proto3" json:"gate_server_addr,omitempty"`
|
||||
Version string `protobuf:"bytes,3,opt,name=version,proto3" json:"version,omitempty"`
|
||||
}
|
||||
|
||||
func (x *RegisterServerReq) Reset() {
|
||||
@@ -207,6 +208,13 @@ func (x *RegisterServerReq) GetGateServerAddr() *GateServerAddr {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *RegisterServerReq) GetVersion() string {
|
||||
if x != nil {
|
||||
return x.Version
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type RegisterServerRsp struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
@@ -372,6 +380,53 @@ func (x *KeepaliveServerReq) GetAppId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetGateServerAddrReq struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Version string `protobuf:"bytes,1,opt,name=version,proto3" json:"version,omitempty"`
|
||||
}
|
||||
|
||||
func (x *GetGateServerAddrReq) Reset() {
|
||||
*x = GetGateServerAddrReq{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_api_proto_msgTypes[7]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *GetGateServerAddrReq) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetGateServerAddrReq) ProtoMessage() {}
|
||||
|
||||
func (x *GetGateServerAddrReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_api_proto_msgTypes[7]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetGateServerAddrReq.ProtoReflect.Descriptor instead.
|
||||
func (*GetGateServerAddrReq) Descriptor() ([]byte, []int) {
|
||||
return file_api_proto_rawDescGZIP(), []int{7}
|
||||
}
|
||||
|
||||
func (x *GetGateServerAddrReq) GetVersion() string {
|
||||
if x != nil {
|
||||
return x.Version
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type RegionEc2B struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
@@ -383,7 +438,7 @@ type RegionEc2B struct {
|
||||
func (x *RegionEc2B) Reset() {
|
||||
*x = RegionEc2B{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_api_proto_msgTypes[7]
|
||||
mi := &file_api_proto_msgTypes[8]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@@ -396,7 +451,7 @@ func (x *RegionEc2B) String() string {
|
||||
func (*RegionEc2B) ProtoMessage() {}
|
||||
|
||||
func (x *RegionEc2B) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_api_proto_msgTypes[7]
|
||||
mi := &file_api_proto_msgTypes[8]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@@ -409,7 +464,7 @@ func (x *RegionEc2B) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use RegionEc2B.ProtoReflect.Descriptor instead.
|
||||
func (*RegionEc2B) Descriptor() ([]byte, []int) {
|
||||
return file_api_proto_rawDescGZIP(), []int{7}
|
||||
return file_api_proto_rawDescGZIP(), []int{8}
|
||||
}
|
||||
|
||||
func (x *RegionEc2B) GetData() []byte {
|
||||
@@ -431,7 +486,7 @@ type GateServerAddr struct {
|
||||
func (x *GateServerAddr) Reset() {
|
||||
*x = GateServerAddr{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_api_proto_msgTypes[8]
|
||||
mi := &file_api_proto_msgTypes[9]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@@ -444,7 +499,7 @@ func (x *GateServerAddr) String() string {
|
||||
func (*GateServerAddr) ProtoMessage() {}
|
||||
|
||||
func (x *GateServerAddr) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_api_proto_msgTypes[8]
|
||||
mi := &file_api_proto_msgTypes[9]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@@ -457,7 +512,7 @@ func (x *GateServerAddr) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use GateServerAddr.ProtoReflect.Descriptor instead.
|
||||
func (*GateServerAddr) Descriptor() ([]byte, []int) {
|
||||
return file_api_proto_rawDescGZIP(), []int{8}
|
||||
return file_api_proto_rawDescGZIP(), []int{9}
|
||||
}
|
||||
|
||||
func (x *GateServerAddr) GetIpAddr() string {
|
||||
@@ -485,63 +540,68 @@ var file_api_proto_rawDesc = []byte{
|
||||
0x65, 0x72, 0x54, 0x79, 0x70, 0x65, 0x22, 0x2a, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x53, 0x65, 0x72,
|
||||
0x76, 0x65, 0x72, 0x41, 0x70, 0x70, 0x49, 0x64, 0x52, 0x73, 0x70, 0x12, 0x15, 0x0a, 0x06, 0x61,
|
||||
0x70, 0x70, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x61, 0x70, 0x70,
|
||||
0x49, 0x64, 0x22, 0x78, 0x0a, 0x11, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x53, 0x65,
|
||||
0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x65, 0x72, 0x76, 0x65,
|
||||
0x72, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x65,
|
||||
0x72, 0x76, 0x65, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12, 0x42, 0x0a, 0x10, 0x67, 0x61, 0x74, 0x65,
|
||||
0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x18, 0x02, 0x20, 0x01,
|
||||
0x28, 0x0b, 0x32, 0x18, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x47, 0x61,
|
||||
0x74, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x52, 0x0e, 0x67, 0x61,
|
||||
0x74, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x22, 0x3f, 0x0a, 0x11,
|
||||
0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x73,
|
||||
0x70, 0x12, 0x15, 0x0a, 0x06, 0x61, 0x70, 0x70, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x05, 0x61, 0x70, 0x70, 0x49, 0x64, 0x12, 0x13, 0x0a, 0x05, 0x67, 0x73, 0x5f, 0x69,
|
||||
0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x67, 0x73, 0x49, 0x64, 0x22, 0x49, 0x0a,
|
||||
0x0f, 0x43, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71,
|
||||
0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18,
|
||||
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x54, 0x79, 0x70,
|
||||
0x65, 0x12, 0x15, 0x0a, 0x06, 0x61, 0x70, 0x70, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x05, 0x61, 0x70, 0x70, 0x49, 0x64, 0x22, 0x4c, 0x0a, 0x12, 0x4b, 0x65, 0x65, 0x70,
|
||||
0x61, 0x6c, 0x69, 0x76, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x1f,
|
||||
0x0a, 0x0b, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12,
|
||||
0x15, 0x0a, 0x06, 0x61, 0x70, 0x70, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x05, 0x61, 0x70, 0x70, 0x49, 0x64, 0x22, 0x20, 0x0a, 0x0a, 0x52, 0x65, 0x67, 0x69, 0x6f, 0x6e,
|
||||
0x45, 0x63, 0x32, 0x62, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01,
|
||||
0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3d, 0x0a, 0x0e, 0x47, 0x61, 0x74, 0x65,
|
||||
0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x12, 0x17, 0x0a, 0x07, 0x69, 0x70,
|
||||
0x5f, 0x61, 0x64, 0x64, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x69, 0x70, 0x41,
|
||||
0x64, 0x64, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28,
|
||||
0x0d, 0x52, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x32, 0xad, 0x03, 0x0a, 0x09, 0x44, 0x69, 0x73, 0x63,
|
||||
0x6f, 0x76, 0x65, 0x72, 0x79, 0x12, 0x4c, 0x0a, 0x0e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65,
|
||||
0x72, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x12, 0x1b, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61,
|
||||
0x70, 0x69, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x65,
|
||||
0x72, 0x52, 0x65, 0x71, 0x1a, 0x1b, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e,
|
||||
0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x73,
|
||||
0x70, 0x22, 0x00, 0x12, 0x3e, 0x0a, 0x0c, 0x43, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x53, 0x65, 0x72,
|
||||
0x76, 0x65, 0x72, 0x12, 0x19, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43,
|
||||
0x61, 0x6e, 0x63, 0x65, 0x6c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x1a, 0x11,
|
||||
0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4e, 0x75, 0x6c, 0x6c, 0x4d, 0x73,
|
||||
0x67, 0x22, 0x00, 0x12, 0x44, 0x0a, 0x0f, 0x4b, 0x65, 0x65, 0x70, 0x61, 0x6c, 0x69, 0x76, 0x65,
|
||||
0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x12, 0x1c, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70,
|
||||
0x69, 0x2e, 0x4b, 0x65, 0x65, 0x70, 0x61, 0x6c, 0x69, 0x76, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65,
|
||||
0x72, 0x52, 0x65, 0x71, 0x1a, 0x11, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e,
|
||||
0x4e, 0x75, 0x6c, 0x6c, 0x4d, 0x73, 0x67, 0x22, 0x00, 0x12, 0x4c, 0x0a, 0x0e, 0x47, 0x65, 0x74,
|
||||
0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x70, 0x70, 0x49, 0x64, 0x12, 0x1b, 0x2e, 0x6e, 0x6f,
|
||||
0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x47, 0x65, 0x74, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
|
||||
0x41, 0x70, 0x70, 0x49, 0x64, 0x52, 0x65, 0x71, 0x1a, 0x1b, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e,
|
||||
0x49, 0x64, 0x22, 0x92, 0x01, 0x0a, 0x11, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x53,
|
||||
0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x65, 0x72, 0x76,
|
||||
0x65, 0x72, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x73,
|
||||
0x65, 0x72, 0x76, 0x65, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12, 0x42, 0x0a, 0x10, 0x67, 0x61, 0x74,
|
||||
0x65, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x18, 0x02, 0x20,
|
||||
0x01, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x47,
|
||||
0x61, 0x74, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x52, 0x0e, 0x67,
|
||||
0x61, 0x74, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x12, 0x18, 0x0a,
|
||||
0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07,
|
||||
0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3f, 0x0a, 0x11, 0x52, 0x65, 0x67, 0x69, 0x73,
|
||||
0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x73, 0x70, 0x12, 0x15, 0x0a, 0x06,
|
||||
0x61, 0x70, 0x70, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x61, 0x70,
|
||||
0x70, 0x49, 0x64, 0x12, 0x13, 0x0a, 0x05, 0x67, 0x73, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01,
|
||||
0x28, 0x0d, 0x52, 0x04, 0x67, 0x73, 0x49, 0x64, 0x22, 0x49, 0x0a, 0x0f, 0x43, 0x61, 0x6e, 0x63,
|
||||
0x65, 0x6c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x1f, 0x0a, 0x0b, 0x73,
|
||||
0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x0a, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12, 0x15, 0x0a, 0x06,
|
||||
0x61, 0x70, 0x70, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x61, 0x70,
|
||||
0x70, 0x49, 0x64, 0x22, 0x4c, 0x0a, 0x12, 0x4b, 0x65, 0x65, 0x70, 0x61, 0x6c, 0x69, 0x76, 0x65,
|
||||
0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x65, 0x72,
|
||||
0x76, 0x65, 0x72, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a,
|
||||
0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12, 0x15, 0x0a, 0x06, 0x61, 0x70,
|
||||
0x70, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x61, 0x70, 0x70, 0x49,
|
||||
0x64, 0x22, 0x30, 0x0a, 0x14, 0x47, 0x65, 0x74, 0x47, 0x61, 0x74, 0x65, 0x53, 0x65, 0x72, 0x76,
|
||||
0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x52, 0x65, 0x71, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x65, 0x72,
|
||||
0x73, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76, 0x65, 0x72, 0x73,
|
||||
0x69, 0x6f, 0x6e, 0x22, 0x20, 0x0a, 0x0a, 0x52, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x45, 0x63, 0x32,
|
||||
0x62, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52,
|
||||
0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3d, 0x0a, 0x0e, 0x47, 0x61, 0x74, 0x65, 0x53, 0x65, 0x72,
|
||||
0x76, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x12, 0x17, 0x0a, 0x07, 0x69, 0x70, 0x5f, 0x61, 0x64,
|
||||
0x64, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x69, 0x70, 0x41, 0x64, 0x64, 0x72,
|
||||
0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04,
|
||||
0x70, 0x6f, 0x72, 0x74, 0x32, 0xba, 0x03, 0x0a, 0x09, 0x44, 0x69, 0x73, 0x63, 0x6f, 0x76, 0x65,
|
||||
0x72, 0x79, 0x12, 0x4c, 0x0a, 0x0e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x53, 0x65,
|
||||
0x72, 0x76, 0x65, 0x72, 0x12, 0x1b, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e,
|
||||
0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65,
|
||||
0x71, 0x1a, 0x1b, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x65, 0x67,
|
||||
0x69, 0x73, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x73, 0x70, 0x22, 0x00,
|
||||
0x12, 0x3e, 0x0a, 0x0c, 0x43, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
|
||||
0x12, 0x19, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x61, 0x6e, 0x63,
|
||||
0x65, 0x6c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x1a, 0x11, 0x2e, 0x6e, 0x6f,
|
||||
0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4e, 0x75, 0x6c, 0x6c, 0x4d, 0x73, 0x67, 0x22, 0x00,
|
||||
0x12, 0x44, 0x0a, 0x0f, 0x4b, 0x65, 0x65, 0x70, 0x61, 0x6c, 0x69, 0x76, 0x65, 0x53, 0x65, 0x72,
|
||||
0x76, 0x65, 0x72, 0x12, 0x1c, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4b,
|
||||
0x65, 0x65, 0x70, 0x61, 0x6c, 0x69, 0x76, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65,
|
||||
0x71, 0x1a, 0x11, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4e, 0x75, 0x6c,
|
||||
0x6c, 0x4d, 0x73, 0x67, 0x22, 0x00, 0x12, 0x4c, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x53, 0x65, 0x72,
|
||||
0x76, 0x65, 0x72, 0x41, 0x70, 0x70, 0x49, 0x64, 0x12, 0x1b, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e,
|
||||
0x61, 0x70, 0x69, 0x2e, 0x47, 0x65, 0x74, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x70, 0x70,
|
||||
0x49, 0x64, 0x52, 0x73, 0x70, 0x22, 0x00, 0x12, 0x3a, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x52, 0x65,
|
||||
0x67, 0x69, 0x6f, 0x6e, 0x45, 0x63, 0x32, 0x62, 0x12, 0x11, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e,
|
||||
0x61, 0x70, 0x69, 0x2e, 0x4e, 0x75, 0x6c, 0x6c, 0x4d, 0x73, 0x67, 0x1a, 0x14, 0x2e, 0x6e, 0x6f,
|
||||
0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x45, 0x63, 0x32,
|
||||
0x62, 0x22, 0x00, 0x12, 0x42, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x47, 0x61, 0x74, 0x65, 0x53, 0x65,
|
||||
0x72, 0x76, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x12, 0x11, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e,
|
||||
0x61, 0x70, 0x69, 0x2e, 0x4e, 0x75, 0x6c, 0x6c, 0x4d, 0x73, 0x67, 0x1a, 0x18, 0x2e, 0x6e, 0x6f,
|
||||
0x64, 0x65, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x47, 0x61, 0x74, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65,
|
||||
0x72, 0x41, 0x64, 0x64, 0x72, 0x22, 0x00, 0x42, 0x13, 0x5a, 0x11, 0x68, 0x6b, 0x34, 0x65, 0x2f,
|
||||
0x6e, 0x6f, 0x64, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x3b, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x33,
|
||||
0x49, 0x64, 0x52, 0x65, 0x71, 0x1a, 0x1b, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69,
|
||||
0x2e, 0x47, 0x65, 0x74, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x70, 0x70, 0x49, 0x64, 0x52,
|
||||
0x73, 0x70, 0x22, 0x00, 0x12, 0x3a, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x52, 0x65, 0x67, 0x69, 0x6f,
|
||||
0x6e, 0x45, 0x63, 0x32, 0x62, 0x12, 0x11, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69,
|
||||
0x2e, 0x4e, 0x75, 0x6c, 0x6c, 0x4d, 0x73, 0x67, 0x1a, 0x14, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e,
|
||||
0x61, 0x70, 0x69, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x45, 0x63, 0x32, 0x62, 0x22, 0x00,
|
||||
0x12, 0x4f, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x47, 0x61, 0x74, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65,
|
||||
0x72, 0x41, 0x64, 0x64, 0x72, 0x12, 0x1e, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69,
|
||||
0x2e, 0x47, 0x65, 0x74, 0x47, 0x61, 0x74, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x64,
|
||||
0x64, 0x72, 0x52, 0x65, 0x71, 0x1a, 0x18, 0x2e, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x61, 0x70, 0x69,
|
||||
0x2e, 0x47, 0x61, 0x74, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x22,
|
||||
0x00, 0x42, 0x13, 0x5a, 0x11, 0x68, 0x6b, 0x34, 0x65, 0x2f, 0x6e, 0x6f, 0x64, 0x65, 0x2f, 0x61,
|
||||
0x70, 0x69, 0x3b, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -556,32 +616,33 @@ func file_api_proto_rawDescGZIP() []byte {
|
||||
return file_api_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_api_proto_msgTypes = make([]protoimpl.MessageInfo, 9)
|
||||
var file_api_proto_msgTypes = make([]protoimpl.MessageInfo, 10)
|
||||
var file_api_proto_goTypes = []interface{}{
|
||||
(*NullMsg)(nil), // 0: node.api.NullMsg
|
||||
(*GetServerAppIdReq)(nil), // 1: node.api.GetServerAppIdReq
|
||||
(*GetServerAppIdRsp)(nil), // 2: node.api.GetServerAppIdRsp
|
||||
(*RegisterServerReq)(nil), // 3: node.api.RegisterServerReq
|
||||
(*RegisterServerRsp)(nil), // 4: node.api.RegisterServerRsp
|
||||
(*CancelServerReq)(nil), // 5: node.api.CancelServerReq
|
||||
(*KeepaliveServerReq)(nil), // 6: node.api.KeepaliveServerReq
|
||||
(*RegionEc2B)(nil), // 7: node.api.RegionEc2b
|
||||
(*GateServerAddr)(nil), // 8: node.api.GateServerAddr
|
||||
(*NullMsg)(nil), // 0: node.api.NullMsg
|
||||
(*GetServerAppIdReq)(nil), // 1: node.api.GetServerAppIdReq
|
||||
(*GetServerAppIdRsp)(nil), // 2: node.api.GetServerAppIdRsp
|
||||
(*RegisterServerReq)(nil), // 3: node.api.RegisterServerReq
|
||||
(*RegisterServerRsp)(nil), // 4: node.api.RegisterServerRsp
|
||||
(*CancelServerReq)(nil), // 5: node.api.CancelServerReq
|
||||
(*KeepaliveServerReq)(nil), // 6: node.api.KeepaliveServerReq
|
||||
(*GetGateServerAddrReq)(nil), // 7: node.api.GetGateServerAddrReq
|
||||
(*RegionEc2B)(nil), // 8: node.api.RegionEc2b
|
||||
(*GateServerAddr)(nil), // 9: node.api.GateServerAddr
|
||||
}
|
||||
var file_api_proto_depIdxs = []int32{
|
||||
8, // 0: node.api.RegisterServerReq.gate_server_addr:type_name -> node.api.GateServerAddr
|
||||
9, // 0: node.api.RegisterServerReq.gate_server_addr:type_name -> node.api.GateServerAddr
|
||||
3, // 1: node.api.Discovery.RegisterServer:input_type -> node.api.RegisterServerReq
|
||||
5, // 2: node.api.Discovery.CancelServer:input_type -> node.api.CancelServerReq
|
||||
6, // 3: node.api.Discovery.KeepaliveServer:input_type -> node.api.KeepaliveServerReq
|
||||
1, // 4: node.api.Discovery.GetServerAppId:input_type -> node.api.GetServerAppIdReq
|
||||
0, // 5: node.api.Discovery.GetRegionEc2b:input_type -> node.api.NullMsg
|
||||
0, // 6: node.api.Discovery.GetGateServerAddr:input_type -> node.api.NullMsg
|
||||
7, // 6: node.api.Discovery.GetGateServerAddr:input_type -> node.api.GetGateServerAddrReq
|
||||
4, // 7: node.api.Discovery.RegisterServer:output_type -> node.api.RegisterServerRsp
|
||||
0, // 8: node.api.Discovery.CancelServer:output_type -> node.api.NullMsg
|
||||
0, // 9: node.api.Discovery.KeepaliveServer:output_type -> node.api.NullMsg
|
||||
2, // 10: node.api.Discovery.GetServerAppId:output_type -> node.api.GetServerAppIdRsp
|
||||
7, // 11: node.api.Discovery.GetRegionEc2b:output_type -> node.api.RegionEc2b
|
||||
8, // 12: node.api.Discovery.GetGateServerAddr:output_type -> node.api.GateServerAddr
|
||||
8, // 11: node.api.Discovery.GetRegionEc2b:output_type -> node.api.RegionEc2b
|
||||
9, // 12: node.api.Discovery.GetGateServerAddr:output_type -> node.api.GateServerAddr
|
||||
7, // [7:13] is the sub-list for method output_type
|
||||
1, // [1:7] is the sub-list for method input_type
|
||||
1, // [1:1] is the sub-list for extension type_name
|
||||
@@ -680,7 +741,7 @@ func file_api_proto_init() {
|
||||
}
|
||||
}
|
||||
file_api_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*RegionEc2B); i {
|
||||
switch v := v.(*GetGateServerAddrReq); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
@@ -692,6 +753,18 @@ func file_api_proto_init() {
|
||||
}
|
||||
}
|
||||
file_api_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*RegionEc2B); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_api_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*GateServerAddr); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
@@ -710,7 +783,7 @@ func file_api_proto_init() {
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_api_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 9,
|
||||
NumMessages: 10,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
|
||||
@@ -16,7 +16,7 @@ service Discovery {
|
||||
// 获取区服密钥信息
|
||||
rpc GetRegionEc2b (NullMsg) returns (RegionEc2b) {}
|
||||
// 获取负载最小的网关服务器的地址和端口
|
||||
rpc GetGateServerAddr (NullMsg) returns (GateServerAddr) {}
|
||||
rpc GetGateServerAddr (GetGateServerAddrReq) returns (GateServerAddr) {}
|
||||
}
|
||||
|
||||
message NullMsg {
|
||||
@@ -33,6 +33,7 @@ message GetServerAppIdRsp {
|
||||
message RegisterServerReq {
|
||||
string server_type = 1;
|
||||
GateServerAddr gate_server_addr = 2;
|
||||
string version = 3;
|
||||
}
|
||||
|
||||
message RegisterServerRsp {
|
||||
@@ -50,6 +51,10 @@ message KeepaliveServerReq {
|
||||
string app_id = 2;
|
||||
}
|
||||
|
||||
message GetGateServerAddrReq {
|
||||
string version = 1;
|
||||
}
|
||||
|
||||
message RegionEc2b {
|
||||
bytes data = 1;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@ import (
|
||||
"context"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"hk4e/common/region"
|
||||
"hk4e/node/api"
|
||||
@@ -35,28 +37,29 @@ type ServerInstance struct {
|
||||
appId string
|
||||
gateServerIpAddr string
|
||||
gateServerPort uint32
|
||||
version string
|
||||
lastAliveTime int64
|
||||
}
|
||||
|
||||
type DiscoveryService struct {
|
||||
regionEc2b *random.Ec2b
|
||||
// TODO 加锁
|
||||
serverInstanceMap map[string]map[string]*ServerInstance
|
||||
serverAppIdMap map[string]bool
|
||||
gsIdCounter uint32
|
||||
regionEc2b *random.Ec2b // 全局区服密钥信息
|
||||
serverInstanceMap map[string]*sync.Map // 全部服务器实例集合 key:服务器类型 value:服务器实例集合 -> key:appid value:服务器实例
|
||||
serverAppIdMap map[string]bool // 服务器appid集合 key:appid value:是否存在
|
||||
gsIdCounter uint32 // GSID计数器
|
||||
}
|
||||
|
||||
func NewDiscoveryService() *DiscoveryService {
|
||||
r := new(DiscoveryService)
|
||||
r.regionEc2b = region.NewRegionEc2b()
|
||||
logger.Info("region ec2b create ok, seed: %v", r.regionEc2b.Seed())
|
||||
r.serverInstanceMap = map[string]map[string]*ServerInstance{
|
||||
api.GATE: make(map[string]*ServerInstance),
|
||||
api.GS: make(map[string]*ServerInstance),
|
||||
api.FIGHT: make(map[string]*ServerInstance),
|
||||
api.PATHFINDING: make(map[string]*ServerInstance),
|
||||
}
|
||||
r.serverInstanceMap = make(map[string]*sync.Map)
|
||||
r.serverInstanceMap[api.GATE] = &sync.Map{}
|
||||
r.serverInstanceMap[api.GS] = &sync.Map{}
|
||||
r.serverInstanceMap[api.FIGHT] = &sync.Map{}
|
||||
r.serverInstanceMap[api.PATHFINDING] = &sync.Map{}
|
||||
r.serverAppIdMap = make(map[string]bool)
|
||||
r.gsIdCounter = 0
|
||||
go r.removeDeadServer()
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -84,8 +87,9 @@ func (s *DiscoveryService) RegisterServer(ctx context.Context, req *api.Register
|
||||
logger.Info("register new gate server, ip: %v, port: %v", req.GateServerAddr.IpAddr, req.GateServerAddr.Port)
|
||||
inst.gateServerIpAddr = req.GateServerAddr.IpAddr
|
||||
inst.gateServerPort = req.GateServerAddr.Port
|
||||
inst.version = req.Version
|
||||
}
|
||||
instMap[appId] = inst
|
||||
instMap.Store(appId, inst)
|
||||
logger.Info("new server appid is: %v", appId)
|
||||
rsp := &api.RegisterServerRsp{
|
||||
AppId: appId,
|
||||
@@ -103,11 +107,11 @@ func (s *DiscoveryService) CancelServer(ctx context.Context, req *api.CancelServ
|
||||
if !exist {
|
||||
return nil, errors.New("server type not exist")
|
||||
}
|
||||
_, exist = instMap[req.AppId]
|
||||
_, exist = instMap.Load(req.AppId)
|
||||
if !exist {
|
||||
return nil, errors.New("server not exist")
|
||||
}
|
||||
delete(instMap, req.AppId)
|
||||
instMap.Delete(req.AppId)
|
||||
return &api.NullMsg{}, nil
|
||||
}
|
||||
|
||||
@@ -117,12 +121,12 @@ func (s *DiscoveryService) KeepaliveServer(ctx context.Context, req *api.Keepali
|
||||
if !exist {
|
||||
return nil, errors.New("server type not exist")
|
||||
}
|
||||
inst, exist := instMap[req.AppId]
|
||||
inst, exist := instMap.Load(req.AppId)
|
||||
if !exist {
|
||||
return nil, errors.New("server not exist")
|
||||
}
|
||||
// TODO
|
||||
_ = inst
|
||||
serverInstance := inst.(*ServerInstance)
|
||||
serverInstance.lastAliveTime = time.Now().Unix()
|
||||
return &api.NullMsg{}, nil
|
||||
}
|
||||
|
||||
@@ -133,7 +137,7 @@ func (s *DiscoveryService) GetServerAppId(ctx context.Context, req *api.GetServe
|
||||
if !exist {
|
||||
return nil, errors.New("server type not exist")
|
||||
}
|
||||
if len(instMap) == 0 {
|
||||
if s.getServerInstanceMapLen(instMap) == 0 {
|
||||
return nil, errors.New("no server found")
|
||||
}
|
||||
inst := s.getRandomServerInstance(instMap)
|
||||
@@ -152,16 +156,28 @@ func (s *DiscoveryService) GetRegionEc2B(ctx context.Context, req *api.NullMsg)
|
||||
}
|
||||
|
||||
// GetGateServerAddr 获取负载最小的网关服务器的地址和端口
|
||||
func (s *DiscoveryService) GetGateServerAddr(ctx context.Context, req *api.NullMsg) (*api.GateServerAddr, error) {
|
||||
func (s *DiscoveryService) GetGateServerAddr(ctx context.Context, req *api.GetGateServerAddrReq) (*api.GateServerAddr, error) {
|
||||
logger.Debug("get gate server addr")
|
||||
instMap, exist := s.serverInstanceMap[api.GATE]
|
||||
if !exist {
|
||||
return nil, errors.New("gate server not exist")
|
||||
}
|
||||
if len(instMap) == 0 {
|
||||
if s.getServerInstanceMapLen(instMap) == 0 {
|
||||
return nil, errors.New("no gate server found")
|
||||
}
|
||||
inst := s.getRandomServerInstance(instMap)
|
||||
versionInstMap := sync.Map{}
|
||||
instMap.Range(func(key, value any) bool {
|
||||
serverInstance := value.(*ServerInstance)
|
||||
if serverInstance.version != req.Version {
|
||||
return true
|
||||
}
|
||||
versionInstMap.Store(key, serverInstance)
|
||||
return true
|
||||
})
|
||||
if s.getServerInstanceMapLen(&versionInstMap) == 0 {
|
||||
return nil, errors.New("no gate server found")
|
||||
}
|
||||
inst := s.getRandomServerInstance(&versionInstMap)
|
||||
logger.Debug("get gate server addr is, ip: %v, port: %v", inst.gateServerIpAddr, inst.gateServerPort)
|
||||
return &api.GateServerAddr{
|
||||
IpAddr: inst.gateServerIpAddr,
|
||||
@@ -169,13 +185,42 @@ func (s *DiscoveryService) GetGateServerAddr(ctx context.Context, req *api.NullM
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *DiscoveryService) getRandomServerInstance(instMap map[string]*ServerInstance) *ServerInstance {
|
||||
func (s *DiscoveryService) getRandomServerInstance(instMap *sync.Map) *ServerInstance {
|
||||
instList := make(ServerInstanceSortList, 0)
|
||||
for _, v := range instMap {
|
||||
instList = append(instList, v)
|
||||
}
|
||||
instMap.Range(func(key, value any) bool {
|
||||
instList = append(instList, value.(*ServerInstance))
|
||||
return true
|
||||
})
|
||||
sort.Stable(instList)
|
||||
index := random.GetRandomInt32(0, int32(len(instList)-1))
|
||||
inst := instList[index]
|
||||
return inst
|
||||
}
|
||||
|
||||
func (s *DiscoveryService) getServerInstanceMapLen(instMap *sync.Map) int {
|
||||
count := 0
|
||||
instMap.Range(func(key, value any) bool {
|
||||
count++
|
||||
return true
|
||||
})
|
||||
return count
|
||||
}
|
||||
|
||||
// 定时移除掉线服务器
|
||||
func (s *DiscoveryService) removeDeadServer() {
|
||||
ticker := time.NewTicker(time.Second * 60)
|
||||
for {
|
||||
<-ticker.C
|
||||
nowTime := time.Now().Unix()
|
||||
for _, instMap := range s.serverInstanceMap {
|
||||
instMap.Range(func(key, value any) bool {
|
||||
serverInstance := value.(*ServerInstance)
|
||||
if nowTime-serverInstance.lastAliveTime > 60 {
|
||||
logger.Warn("remove dead server, server type: %v, appid: %v", serverInstance.serverType, serverInstance.appId)
|
||||
instMap.Delete(key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,16 @@ func Run(ctx context.Context, configFile string) error {
|
||||
return err
|
||||
}
|
||||
APPID = rsp.GetAppId()
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Second * 15)
|
||||
for {
|
||||
<-ticker.C
|
||||
_, _ = client.Discovery.KeepaliveServer(context.TODO(), &api.KeepaliveServerReq{
|
||||
ServerType: api.PATHFINDING,
|
||||
AppId: APPID,
|
||||
})
|
||||
}
|
||||
}()
|
||||
defer func() {
|
||||
_, _ = client.Discovery.CancelServer(context.TODO(), &api.CancelServerReq{
|
||||
ServerType: api.PATHFINDING,
|
||||
|
||||
Reference in New Issue
Block a user