Commit 019264c811a521d3309c811345caadfeb536c916
1 parent
352e430c
Exists in
master
and in
1 other branch
feat✨:房间排行活动:数据操作添加分布式锁
Showing
3 changed files
with
25 additions
and
0 deletions
Show diff stats
service/roomrank/consts.go
| 1 | package roomrank | 1 | package roomrank |
| 2 | 2 | ||
| 3 | +import "time" | ||
| 4 | + | ||
| 3 | const ( | 5 | const ( |
| 4 | UserClassMin = 1 // 最小评级 | 6 | UserClassMin = 1 // 最小评级 |
| 5 | UserClassMax = 5 // 最大评级 | 7 | UserClassMax = 5 // 最大评级 |
| 6 | 8 | ||
| 7 | RoomCloseSecond = 180 // 房间关闭时间 | 9 | RoomCloseSecond = 180 // 房间关闭时间 |
| 8 | RoomResetRobotSecond = 300 // 改变机器人分数并重新排序的间隔时间 | 10 | RoomResetRobotSecond = 300 // 改变机器人分数并重新排序的间隔时间 |
| 11 | + | ||
| 12 | + RoomLockMillisecond = 3000 * time.Millisecond // 分布式锁时长 3秒 | ||
| 9 | ) | 13 | ) |
service/roomrank/handle.go
| @@ -5,6 +5,7 @@ import ( | @@ -5,6 +5,7 @@ import ( | ||
| 5 | "apigame/models" | 5 | "apigame/models" |
| 6 | "apigame/service-common/svmysql" | 6 | "apigame/service-common/svmysql" |
| 7 | "apigame/service/code-msg" | 7 | "apigame/service/code-msg" |
| 8 | + "apigame/util/zredislock" | ||
| 8 | "fmt" | 9 | "fmt" |
| 9 | ) | 10 | ) |
| 10 | 11 | ||
| @@ -53,6 +54,11 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan | @@ -53,6 +54,11 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan | ||
| 53 | player.Name = req.PlayerName | 54 | player.Name = req.PlayerName |
| 54 | player.Icon = req.PlayerIcon | 55 | player.Icon = req.PlayerIcon |
| 55 | 56 | ||
| 57 | + // 采用分布式锁 | ||
| 58 | + lockKey := getLockKey(gameId, config.Id) | ||
| 59 | + lock := zredislock.Obtain(lockKey, RoomLockMillisecond, nil) | ||
| 60 | + defer lock.Release() | ||
| 61 | + | ||
| 56 | // 尝试判断结算 | 62 | // 尝试判断结算 |
| 57 | hasSettleChange := TrySettle(gameId, player, config) | 63 | hasSettleChange := TrySettle(gameId, player, config) |
| 58 | 64 | ||
| @@ -107,6 +113,11 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r | @@ -107,6 +113,11 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r | ||
| 107 | player.Name = req.PlayerName | 113 | player.Name = req.PlayerName |
| 108 | player.Icon = req.PlayerIcon | 114 | player.Icon = req.PlayerIcon |
| 109 | 115 | ||
| 116 | + // 采用分布式锁 | ||
| 117 | + lockKey := getLockKey(gameId, config.Id) | ||
| 118 | + lock := zredislock.Obtain(lockKey, RoomLockMillisecond, nil) | ||
| 119 | + defer lock.Release() | ||
| 120 | + | ||
| 110 | // 尝试判断结算 | 121 | // 尝试判断结算 |
| 111 | _ = TrySettle(gameId, player, config) | 122 | _ = TrySettle(gameId, player, config) |
| 112 | if !player.SettleHas { | 123 | if !player.SettleHas { |
| @@ -151,6 +162,11 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs | @@ -151,6 +162,11 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs | ||
| 151 | player.Name = req.PlayerName | 162 | player.Name = req.PlayerName |
| 152 | player.Icon = req.PlayerIcon | 163 | player.Icon = req.PlayerIcon |
| 153 | 164 | ||
| 165 | + // 采用分布式锁 | ||
| 166 | + lockKey := getLockKey(gameId, config.Id) | ||
| 167 | + lock := zredislock.Obtain(lockKey, RoomLockMillisecond, nil) | ||
| 168 | + defer lock.Release() | ||
| 169 | + | ||
| 154 | // 尝试判断结算 | 170 | // 尝试判断结算 |
| 155 | hasSettleChange := TrySettle(gameId, player, config) | 171 | hasSettleChange := TrySettle(gameId, player, config) |
| 156 | if hasSettleChange { | 172 | if hasSettleChange { |
service/roomrank/logic.go
| @@ -2,11 +2,16 @@ package roomrank | @@ -2,11 +2,16 @@ package roomrank | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "apigame/configs/confroomrank" | 4 | "apigame/configs/confroomrank" |
| 5 | + "apigame/service-common/svconst" | ||
| 5 | "apigame/util/utstring" | 6 | "apigame/util/utstring" |
| 6 | "apigame/util/zjson" | 7 | "apigame/util/zjson" |
| 7 | "fmt" | 8 | "fmt" |
| 8 | ) | 9 | ) |
| 9 | 10 | ||
| 11 | +func getLockKey(gameId string, activityId int64) string { | ||
| 12 | + return fmt.Sprintf("%s:lock:roomrank:%s:%d", svconst.REDIS_CACHEP_REFIX, gameId, activityId) | ||
| 13 | +} | ||
| 14 | + | ||
| 10 | // TrySettle 尝试判断结算 | 15 | // TrySettle 尝试判断结算 |
| 11 | func TrySettle(gameId string, player *Player, config *confroomrank.ActivityConfig) (hasChange bool) { | 16 | func TrySettle(gameId string, player *Player, config *confroomrank.ActivityConfig) (hasChange bool) { |
| 12 | hasChange = false | 17 | hasChange = false |