Skip to content

Commit

Permalink
修复Redis锁问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hsyhhssyy committed May 25, 2024
1 parent a72e5f6 commit fef9b3c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions AmiyaBotPlayerRatingServer.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Iddict/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=JMES/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Kutt/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Minigame/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Schulte/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public async Task<IActionResult> GenerateShortenUrl(string gameId)
// HTTP Access
var kuttUrl = _configuration["Kutt:Url"];
var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://"+ kuttUrl + "/api/v2/links");
var request = new HttpRequestMessage(HttpMethod.Post, "https://"+kuttUrl+"/api/v2/links");
request.Content = new StringContent(JsonConvert.SerializeObject(new
{
target = shortenUrl,
Expand Down
18 changes: 14 additions & 4 deletions AmiyaBotPlayerRatingServer/GameLogic/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ private string SerializeGame(Game game)
return JsonConvert.SerializeObject(game);
}

private string GenerateLockName(string gameId)
{
return "AmiyaBot-Minigame-Game-Lock-" + gameId;
}

public async Task<Game?> GetGameAsync(string gameId, bool readOnly = true)
{
//从数据库中获取查id
Expand All @@ -130,7 +135,7 @@ private string SerializeGame(Game game)
else
{
// 获取锁
var redisLock = await _redLockFactory.CreateLockAsync("AmiyaBot-Minigame-Game-Lock-", TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
var redisLock = await _redLockFactory.CreateLockAsync(GenerateLockName(gameInfo.Id), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));

if (redisLock.IsAcquired)
{
Expand Down Expand Up @@ -200,7 +205,7 @@ public async Task<List<Game>> GetGameByPlayerIdAsync(string playerId, bool readO
else
{
// 获取锁
var redisLock = await _redLockFactory.CreateLockAsync("AmiyaBot-Minigame-Game-Lock-", TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
var redisLock = await _redLockFactory.CreateLockAsync(GenerateLockName(info.Id), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));

if (redisLock.IsAcquired)
{
Expand All @@ -225,12 +230,17 @@ public async Task<List<Game>> GetGameByPlayerIdAsync(string playerId, bool readO

}

public async Task<Game> GetGameByJoinCodeAsync(string joinCode, bool readOnly = true)
public async Task<Game?> GetGameByJoinCodeAsync(string joinCode, bool readOnly = true)
{
//从数据库中获取查id

var info = await _dbContext.GameInfos.Where(x => x.JoinCode == joinCode.ToString()).FirstOrDefaultAsync();

if (info == null)
{
return null;
}

if (readOnly)
{
// 不需要获取锁,直接获取数据
Expand All @@ -243,7 +253,7 @@ public async Task<Game> GetGameByJoinCodeAsync(string joinCode, bool readOnly =
else
{
// 获取锁
var redisLock = await _redLockFactory.CreateLockAsync("AmiyaBot-Minigame-Game-Lock-",
var redisLock = await _redLockFactory.CreateLockAsync(GenerateLockName(info.Id),
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));

if (redisLock.IsAcquired)
Expand Down
2 changes: 1 addition & 1 deletion AmiyaBotPlayerRatingServer/RealtimeHubs/GameHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ await Clients.Group(gameId).SendAsync("PlayerLeft", JsonConvert.SerializeObject(
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public async Task CloseGame(string gameId)
{
var game = await ValidateGame(gameId,false);
await using var game = await ValidateGame(gameId,false);
var appUser = await ValidateUser();
var manager = await ValidateManager(game.GameType);

Expand Down

0 comments on commit fef9b3c

Please sign in to comment.