gate账号并发登录分布式锁
This commit is contained in:
1 parent
2cb9f89cae
commit
9b229bf70e
7 files changed
+147
-16
No files matched your search
@@ -18,5 +18,9 @@ track_thread = true
|
||||
[database]
|
||||
url = "mongodb://mongo:27017"
|
||||
|
||||
[redis]
|
||||
addr = "redis://redis:6379"
|
||||
password = ""
|
||||
|
||||
[mq]
|
||||
nats_url = "nats://nats:4222"
|
||||
@@ -197,12 +197,10 @@ func (c *Controller) queryCurRegion(ctx *gin.Context) {
|
||||
c.dispatchReqErrorRsp(ctx, 2)
|
||||
return
|
||||
}
|
||||
var gateServerAddr *api.GateServerAddr = nil
|
||||
var err error = nil
|
||||
now := time.Now().Unix()
|
||||
inst, exist := c.gateServerMap.Load(versionStr)
|
||||
if !exist || (exist && now-inst.(*GateServerInfo).timestamp > 60) {
|
||||
gateServerAddr, err = c.discoveryClient.GetGateServerAddr(ctx.Request.Context(), &api.GetGateServerAddrReq{
|
||||
gateServerInfo, exist := c.gateServerMap.Load(versionStr)
|
||||
if !exist {
|
||||
addr, err := c.discoveryClient.GetGateServerAddr(ctx.Request.Context(), &api.GetGateServerAddrReq{
|
||||
GameVersion: versionStr,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -210,14 +208,29 @@ func (c *Controller) queryCurRegion(ctx *gin.Context) {
|
||||
c.dispatchReqErrorRsp(ctx, 2)
|
||||
return
|
||||
}
|
||||
c.gateServerMap.Store(versionStr, &GateServerInfo{
|
||||
addr: gateServerAddr,
|
||||
gateServerInfo = &GateServerInfo{
|
||||
addr: addr,
|
||||
timestamp: now,
|
||||
})
|
||||
} else {
|
||||
gateServerAddr = inst.(*GateServerInfo).addr
|
||||
}
|
||||
c.gateServerMap.Store(versionStr, gateServerInfo)
|
||||
}
|
||||
if now-gateServerInfo.(*GateServerInfo).timestamp > 60 {
|
||||
addr, err := c.discoveryClient.GetGateServerAddr(ctx.Request.Context(), &api.GetGateServerAddrReq{
|
||||
GameVersion: versionStr,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("get gate server addr error: %v", err)
|
||||
} else {
|
||||
gateServerInfo = &GateServerInfo{
|
||||
addr: addr,
|
||||
timestamp: now,
|
||||
}
|
||||
c.gateServerMap.Store(versionStr, gateServerInfo)
|
||||
}
|
||||
}
|
||||
gateServerAddr := gateServerInfo.(*GateServerInfo).addr
|
||||
var regionCurr *proto.QueryCurrRegionHttpRsp = nil
|
||||
var err error = nil
|
||||
if !config.GetConfig().Hk4e.ForwardModeEnable {
|
||||
if !c.stopServerInfo.StopServer {
|
||||
regionCurr = GetRegionCurr(c.ec2b, gateServerAddr, &api.StopServerInfo{StopServer: false})
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"hk4e/pkg/logger"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RedisAccountKeyPrefix key前缀
|
||||
const RedisAccountKeyPrefix = "HK4E"
|
||||
|
||||
// GetRedisAccountLockKey 获取账号分布式锁key
|
||||
func (d *Dao) GetRedisAccountLockKey(openId string) string {
|
||||
return RedisAccountKeyPrefix + ":ACCOUNT_LOCK:" + openId
|
||||
}
|
||||
|
||||
// 基于redis的玩家离线数据分布式锁实现
|
||||
|
||||
const (
|
||||
MaxLockAliveTime = 10000 // 单个锁的最大存活时间 毫秒
|
||||
)
|
||||
|
||||
// DistLock 加锁并返回是否成功
|
||||
func (d *Dao) DistLock(openId string) bool {
|
||||
var result = false
|
||||
var err error = nil
|
||||
if d.redisCluster != nil {
|
||||
result, err = d.redisCluster.SetNX(context.TODO(),
|
||||
d.GetRedisAccountLockKey(openId),
|
||||
time.Now().UnixMilli(),
|
||||
time.Millisecond*time.Duration(MaxLockAliveTime)).Result()
|
||||
} else {
|
||||
result, err = d.redis.SetNX(context.TODO(),
|
||||
d.GetRedisAccountLockKey(openId),
|
||||
time.Now().UnixMilli(),
|
||||
time.Millisecond*time.Duration(MaxLockAliveTime)).Result()
|
||||
}
|
||||
if err != nil {
|
||||
logger.Error("redis lock setnx error: %v", err)
|
||||
return false
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// DistUnlock 解锁
|
||||
func (d *Dao) DistUnlock(openId string) {
|
||||
var result int64 = 0
|
||||
var err error = nil
|
||||
if d.redisCluster != nil {
|
||||
result, err = d.redisCluster.Del(context.TODO(), d.GetRedisAccountLockKey(openId)).Result()
|
||||
} else {
|
||||
result, err = d.redis.Del(context.TODO(), d.GetRedisAccountLockKey(openId)).Result()
|
||||
}
|
||||
if err != nil {
|
||||
logger.Error("redis lock del error: %v", err)
|
||||
return
|
||||
}
|
||||
if result == 0 {
|
||||
logger.Error("redis lock del result is fail")
|
||||
return
|
||||
}
|
||||
}
|
||||
+44
-2
@@ -2,18 +2,22 @@ package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/pkg/logger"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
)
|
||||
|
||||
type Dao struct {
|
||||
mongo *mongo.Client
|
||||
db *mongo.Database
|
||||
mongo *mongo.Client
|
||||
db *mongo.Database
|
||||
redis *redis.Client
|
||||
redisCluster *redis.ClusterClient
|
||||
}
|
||||
|
||||
func NewDao() (*Dao, error) {
|
||||
@@ -33,6 +37,36 @@ func NewDao() (*Dao, error) {
|
||||
r.mongo = client
|
||||
r.db = client.Database("gate_hk4e")
|
||||
|
||||
r.redis = nil
|
||||
r.redisCluster = nil
|
||||
redisAddr := strings.ReplaceAll(config.GetConfig().Redis.Addr, "redis://", "")
|
||||
if strings.Contains(redisAddr, ",") {
|
||||
redisAddrList := strings.Split(redisAddr, ",")
|
||||
r.redisCluster = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: redisAddrList,
|
||||
Password: config.GetConfig().Redis.Password,
|
||||
PoolSize: 10,
|
||||
MinIdleConns: 1,
|
||||
})
|
||||
} else {
|
||||
r.redis = redis.NewClient(&redis.Options{
|
||||
Addr: redisAddr,
|
||||
Password: config.GetConfig().Redis.Password,
|
||||
DB: 0,
|
||||
PoolSize: 10,
|
||||
MinIdleConns: 1,
|
||||
})
|
||||
}
|
||||
if r.redisCluster != nil {
|
||||
err = r.redisCluster.Ping(context.TODO()).Err()
|
||||
} else {
|
||||
err = r.redis.Ping(context.TODO()).Err()
|
||||
}
|
||||
if err != nil {
|
||||
logger.Error("redis ping error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
@@ -41,4 +75,12 @@ func (d *Dao) CloseDao() {
|
||||
if err != nil {
|
||||
logger.Error("mongo close error: %v", err)
|
||||
}
|
||||
if d.redisCluster != nil {
|
||||
err = d.redisCluster.Close()
|
||||
} else {
|
||||
err = d.redis.Close()
|
||||
}
|
||||
if err != nil {
|
||||
logger.Error("redis close error: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -698,7 +698,6 @@ func (k *KcpConnManager) syncMinLoadServerAppid() {
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("get gs server appid error: %v", err)
|
||||
k.minLoadGsServerAppId = ""
|
||||
} else {
|
||||
k.minLoadGsServerAppId = gsServerAppId.AppId
|
||||
}
|
||||
|
||||
+13
-1
@@ -252,6 +252,7 @@ func (k *KcpConnManager) connCtrlMsgHandle(
|
||||
return
|
||||
}
|
||||
k.closeKcpConnBySessionId(sessionId, connCtrlMsg.KickReason)
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,6 +312,7 @@ func (k *KcpConnManager) serverMsgHandle(
|
||||
kickFinishNotifyChan <- true
|
||||
delete(reLoginRemoteKickRegMap, serverMsg.UserId)
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,7 +421,9 @@ func (k *KcpConnManager) forwardRobotMsgToClientHandle(session *Session) {
|
||||
switch netMsg.EventId {
|
||||
case mq.ServerForwardModeServerCloseNotify:
|
||||
session.conn.Close()
|
||||
default:
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -479,6 +483,14 @@ func (k *KcpConnManager) doGateLogin(req *proto.GetPlayerTokenReq, session *Sess
|
||||
logger.Error("verify token error, openId: %v", req.AccountUid)
|
||||
return k.loginFailRsp(0, proto.Retcode_RET_ACCOUNT_VEIRFY_ERROR, false, 0)
|
||||
}
|
||||
ok := k.db.DistLock(req.AccountUid)
|
||||
if !ok {
|
||||
logger.Error("account lock fail, openId: %v", req.AccountUid)
|
||||
return k.loginFailRsp(0, proto.Retcode_RET_ANOTHER_LOGIN, false, 0)
|
||||
}
|
||||
defer func() {
|
||||
k.db.DistUnlock(req.AccountUid)
|
||||
}()
|
||||
account, err := k.db.QueryAccountByOpenId(req.AccountUid)
|
||||
if err != nil {
|
||||
logger.Error("query account error: %v, openId: %v", err, req.AccountUid)
|
||||
@@ -595,7 +607,7 @@ func (k *KcpConnManager) doGateLogin(req *proto.GetPlayerTokenReq, session *Sess
|
||||
// 密钥交换
|
||||
session.keyId = req.KeyId
|
||||
session.clientRandKey = req.ClientRandKey
|
||||
ok := k.keyExchange(session, uid, rsp)
|
||||
ok = k.keyExchange(session, uid, rsp)
|
||||
if !ok {
|
||||
logger.Error("key exchange error, uid: %v", uid)
|
||||
return k.loginFailRsp(0, proto.Retcode_RET_SVR_ERROR, false, 0)
|
||||
|
||||
+1
-2
@@ -2,10 +2,9 @@ package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/pkg/logger"
|
||||
"strings"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
Reference in new issue
Block a user