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
service/roomrank/handle.go
| ... | ... | @@ -5,6 +5,7 @@ import ( |
| 5 | 5 | "apigame/models" |
| 6 | 6 | "apigame/service-common/svmysql" |
| 7 | 7 | "apigame/service/code-msg" |
| 8 | + "apigame/util/zredislock" | |
| 8 | 9 | "fmt" |
| 9 | 10 | ) |
| 10 | 11 | |
| ... | ... | @@ -53,6 +54,11 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan |
| 53 | 54 | player.Name = req.PlayerName |
| 54 | 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 | 63 | hasSettleChange := TrySettle(gameId, player, config) |
| 58 | 64 | |
| ... | ... | @@ -107,6 +113,11 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r |
| 107 | 113 | player.Name = req.PlayerName |
| 108 | 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 | 122 | _ = TrySettle(gameId, player, config) |
| 112 | 123 | if !player.SettleHas { |
| ... | ... | @@ -151,6 +162,11 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs |
| 151 | 162 | player.Name = req.PlayerName |
| 152 | 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 | 171 | hasSettleChange := TrySettle(gameId, player, config) |
| 156 | 172 | if hasSettleChange { | ... | ... |
service/roomrank/logic.go
| ... | ... | @@ -2,11 +2,16 @@ package roomrank |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | 4 | "apigame/configs/confroomrank" |
| 5 | + "apigame/service-common/svconst" | |
| 5 | 6 | "apigame/util/utstring" |
| 6 | 7 | "apigame/util/zjson" |
| 7 | 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 | 15 | // TrySettle 尝试判断结算 |
| 11 | 16 | func TrySettle(gameId string, player *Player, config *confroomrank.ActivityConfig) (hasChange bool) { |
| 12 | 17 | hasChange = false | ... | ... |