Kaynağa Gözat

负载同步

zhengtao 7 ay önce
ebeveyn
işleme
711eb3be4b

+ 1 - 2
components/mongo/component.go

@@ -98,10 +98,9 @@ func CreateDatabase(uri, dbName string, timeout ...time.Duration) (*mongo.Databa
 	//	PreferServerCipherSuites: true,
 	//}
 	//o := options.Client().ApplyURI(uri).SetTLSConfig(tlsConfig)
-	o := options.Client().ApplyURI(uri).SetMaxPoolSize(500). //最大连接
+	o := options.Client().ApplyURI(uri).SetMaxPoolSize(200). //最大连接
 									SetMinPoolSize(20).                  //最小连接
 									SetMaxConnIdleTime(3 * time.Minute). //连接空闲时间
-									SetMaxConnIdleTime(5 * time.Minute). //连接生命周期
 									SetConnectTimeout(10 * time.Second). //连接超时时间
 									SetSocketTimeout(10 * time.Second)   //套接字超时时间
 	if err := o.Validate(); err != nil {

+ 17 - 7
game/game_cluster/internal/mdb/models/account.go

@@ -119,26 +119,36 @@ func (ac *Account) AccountRegisterOrLogin(req *param.LoginReq) (*Account, int32)
 		JoinIp:    req.IP,
 		JoinTime:  mhayaTime.Now().Unix(),
 	}
-
-	_, err = mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable)
+	player := NewPlayer(token.New(req.Pid, req.OpenID, devAccountTable.UserName, "", "", ""))
+	jsonData, err := jsoniter.Marshal(&player)
+	if err != nil {
+		clog.Errorf("Failed to AccountRegisterOrLogin request: %v  err = %v", devAccountTable, err.Error())
+		return nil, code.LoginError
+	}
+	err = mdb.RDB.Set(ctx, fmt.Sprintf("%v:%v", constant.CNamePlayer, req.OpenID), jsonData, time.Hour*3*24).Err()
 	if err != nil {
 		clog.Errorf("Failed to AccountRegisterOrLogin request: %v  err = %v", devAccountTable, err.Error())
 		return nil, code.LoginError
 	}
-	player := NewPlayer(token.New(req.Pid, req.OpenID, devAccountTable.UserName, "", "", ""))
-	jsonData, _ := jsoniter.Marshal(&devAccountTable)
-	err = mdb.RDB.Set(context.Background(), fmt.Sprintf("%v:%v", constant.CNameAccount, devAccountTable.OpenId), jsonData, time.Hour*3*24).Err()
 
-	jsonData, err = jsoniter.Marshal(&player)
+	jsonData, err = jsoniter.Marshal(&devAccountTable)
+	if err != nil {
+		clog.Errorf("Failed to AccountRegisterOrLogin  request: %v  err = %v", devAccountTable, err.Error())
+		return nil, code.LoginError
+	}
+
+	err = mdb.RDB.Set(context.Background(), fmt.Sprintf("%v:%v", constant.CNameAccount, devAccountTable.OpenId), jsonData, time.Hour*3*24).Err()
 	if err != nil {
 		clog.Errorf("Failed to AccountRegisterOrLogin request: %v  err = %v", devAccountTable, err.Error())
 		return nil, code.LoginError
 	}
-	err = mdb.RDB.Set(ctx, fmt.Sprintf("%v:%v", constant.CNamePlayer, req.OpenID), jsonData, time.Hour*3*24).Err()
+
+	_, err = mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable)
 	if err != nil {
 		clog.Errorf("Failed to AccountRegisterOrLogin request: %v  err = %v", devAccountTable, err.Error())
 		return nil, code.LoginError
 	}
+
 	//统计新注册
 	SetDailyRecordNewUserRegisterHash(req.Platform, req.Channel, devAccountTable.UserName, req.IP, DailyRecordNewRegistered)
 

+ 9 - 11
game/game_cluster/nodes/game/module/player/actor_player.go

@@ -24,6 +24,7 @@ import (
 	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/mongo/options"
 	"io"
+	"math/rand"
 	"net/http"
 	//"net/url"
 	"time"
@@ -51,7 +52,7 @@ type (
 )
 
 func (p *actorPlayer) OnInit() {
-	p.exitTime = time.Minute * 10
+	p.exitTime = time.Duration(rand.Int63n(1200)) + time.Minute*5
 	clog.Debugf("[actorPlayer] path = %s init!", p.PathString())
 
 	// 注册 session关闭的remote函数(网关触发连接断开后,会调用RPC发送该消息)
@@ -102,7 +103,7 @@ func (p *actorPlayer) OnInit() {
 	//p.Timer().Add(10*time.Second, p.refreshRoll)
 	p.Timer().Add(60*time.Second, p.SetRewardToDB)
 	p.Timer().Add(1*time.Minute, p.setRedis)
-	p.Timer().Add(60*time.Second, p.setDb)
+	p.Timer().Add(30*time.Second+time.Duration(rand.Int63n(60)), p.setDb)
 	//p.Timer().Add(30*time.Second, p.setInviteReward)
 
 	//p.Timer().Add(1*time.Second, p.addRoll)
@@ -236,20 +237,17 @@ func (p *actorPlayer) setRedisAll() {
 			return
 		}
 		mdb.RDB.Set(ctx, fmt.Sprintf("%v:%v", constant.CNamePlayer, p.Player.OpenId), jsonData, time.Hour*3*24)
-		p.dirty = false
 	}
 }
 
 func (p *actorPlayer) setDb() {
-	go func() {
-		if p.isOnline && p.dirty {
-			ret, err := mdb.MDB.Collection(constant.CNamePlayer).UpdateOne(context.Background(), bson.M{"openId": p.Player.OpenId}, bson.M{"$set": p.Player}, options.Update().SetUpsert(true))
-			if err != nil {
-				clog.Warnf("[actorPlayer] setDb ret=%v, err = %s exit!", ret, err)
-			}
-			p.dirty = false
+	if p.isOnline && p.dirty {
+		ret, err := mdb.MDB.Collection(constant.CNamePlayer).UpdateOne(context.Background(), bson.M{"openId": p.Player.OpenId}, bson.M{"$set": p.Player}, options.Update().SetUpsert(true))
+		if err != nil {
+			clog.Warnf("[actorPlayer] setDb ret=%v, err = %s exit!", ret, err)
 		}
-	}()
+		p.dirty = false
+	}
 }
 
 // 获取和更新头像

+ 1 - 1
game/game_cluster/robot_client/main.go

@@ -17,7 +17,7 @@ import (
 func main() {
 
 	// 定义命令行标志
-	userOffset := flag.Int("userOffset", 100001, "用户偏移量")
+	userOffset := flag.Int("userOffset", 700001, "用户偏移量")
 	count := flag.Int("count", 100000, "请求总数")
 	qps := flag.Int("qps", 500, "最大并发数")
 	url := flag.String("url", "http://127.0.0.1:20000", "请求url")