Redis reads prefer replica

This commit is contained in:
2025-09-15 00:48:15 +01:00
parent a741f25bde
commit 919ab864ce
7 changed files with 15 additions and 11 deletions

View File

@@ -16,6 +16,7 @@ using StackExchange.Redis.Extensions.Core.Abstractions;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using StackExchange.Redis;
namespace MareSynchronosAuthService.Controllers;
@@ -112,7 +113,7 @@ public class JwtController : Controller
return Unauthorized("You are permanently banned.");
}
var existingIdent = await _redis.GetAsync<string>("UID:" + authResult.Uid);
var existingIdent = await _redis.GetAsync<string>("UID:" + authResult.Uid, CommandFlags.PreferReplica);
if (!string.IsNullOrEmpty(existingIdent)) return Unauthorized("Already logged in to this account. Reconnect in 60 seconds. If you keep seeing this issue, restart your game.");
var token = CreateToken(new List<Claim>()

View File

@@ -46,7 +46,7 @@ public class AccountRegistrationService
return reply;
}
var registrationsByIp = await _redis.GetAsync<int>("IPREG:" + ip).ConfigureAwait(false);
var registrationsByIp = await _redis.GetAsync<int>("IPREG:" + ip, CommandFlags.PreferReplica).ConfigureAwait(false);
if (registrationsByIp >= _configurationService.GetValueOrDefault(nameof(AuthServiceConfiguration.RegisterIpLimit), 3))
{
reply.ErrorMessage = "Too many registrations from this IP. Please try again later.";

View File

@@ -6,6 +6,7 @@ using MareSynchronos.API.Data;
using MareSynchronos.API.Dto.Group;
using MareSynchronosShared.Metrics;
using Microsoft.AspNetCore.SignalR;
using StackExchange.Redis;
namespace MareSynchronosServer.Hubs;
@@ -118,14 +119,14 @@ public partial class MareHub
private async Task<Dictionary<string, string>> GetOnlineUsers(List<string> uids)
{
var result = await _redis.GetAllAsync<string>(uids.Select(u => "UID:" + u).ToHashSet(StringComparer.Ordinal)).ConfigureAwait(false);
var result = await _redis.GetAllAsync<string>(uids.Select(u => "UID:" + u).ToHashSet(StringComparer.Ordinal), CommandFlags.PreferReplica).ConfigureAwait(false);
return uids.Where(u => result.TryGetValue("UID:" + u, out var ident) && !string.IsNullOrEmpty(ident)).ToDictionary(u => u, u => result["UID:" + u], StringComparer.Ordinal);
}
private async Task<string> GetUserIdent(string uid)
{
if (string.IsNullOrEmpty(uid)) return string.Empty;
return await _redis.GetAsync<string>("UID:" + uid).ConfigureAwait(false);
return await _redis.GetAsync<string>("UID:" + uid, CommandFlags.PreferReplica).ConfigureAwait(false);
}
private async Task RemoveUserFromRedis()

View File

@@ -6,6 +6,7 @@ using MareSynchronosShared.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using StackExchange.Redis;
namespace MareSynchronosServer.Hubs;
@@ -13,12 +14,12 @@ public partial class MareHub
{
private async Task<string?> GetUserGposeLobby()
{
return await _redis.GetAsync<string>(GposeLobbyUser).ConfigureAwait(false);
return await _redis.GetAsync<string>(GposeLobbyUser, CommandFlags.PreferReplica).ConfigureAwait(false);
}
private async Task<List<string>> GetUsersInLobby(string lobbyId, bool includeSelf = false)
{
var users = await _redis.GetAsync<List<string>>($"GposeLobby:{lobbyId}").ConfigureAwait(false);
var users = await _redis.GetAsync<List<string>>($"GposeLobby:{lobbyId}", CommandFlags.PreferReplica).ConfigureAwait(false);
return users?.Where(u => includeSelf || !string.Equals(u, UserUID, StringComparison.Ordinal)).ToList() ?? [];
}
@@ -68,7 +69,7 @@ public partial class MareHub
while (string.IsNullOrEmpty(lobbyId))
{
lobbyId = StringUtils.GenerateRandomString(30, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
var result = await _redis.GetAsync<List<string>>($"GposeLobby:{lobbyId}").ConfigureAwait(false);
var result = await _redis.GetAsync<List<string>>($"GposeLobby:{lobbyId}", CommandFlags.PreferReplica).ConfigureAwait(false);
if (result != null)
lobbyId = string.Empty;
}

View File

@@ -3,6 +3,7 @@ using MareSynchronos.API.SignalR;
using MareSynchronosServer.Hubs;
using Microsoft.AspNetCore.SignalR;
using StackExchange.Redis.Extensions.Core.Abstractions;
using StackExchange.Redis;
namespace MareSynchronosServer.Services;
@@ -153,7 +154,7 @@ public sealed class GPoseLobbyDistributionService : IHostedService, IDisposable
if (!lobbyId.Value.Values.Any())
continue;
var gposeLobbyUsers = await _redisDb.GetAsync<List<string>>($"GposeLobby:{lobbyId.Key}").ConfigureAwait(false);
var gposeLobbyUsers = await _redisDb.GetAsync<List<string>>($"GposeLobby:{lobbyId.Key}", CommandFlags.PreferReplica).ConfigureAwait(false);
if (gposeLobbyUsers == null)
continue;
@@ -200,7 +201,7 @@ public sealed class GPoseLobbyDistributionService : IHostedService, IDisposable
if (!lobbyId.Value.Values.Any())
continue;
var gposeLobbyUsers = await _redisDb.GetAsync<List<string>>($"GposeLobby:{lobbyId.Key}").ConfigureAwait(false);
var gposeLobbyUsers = await _redisDb.GetAsync<List<string>>($"GposeLobby:{lobbyId.Key}", CommandFlags.PreferReplica).ConfigureAwait(false);
if (gposeLobbyUsers == null)
continue;

View File

@@ -646,7 +646,7 @@ public class MareModule : InteractionModuleBase
var auth = await db.Auth.Include(u => u.PrimaryUser).SingleOrDefaultAsync(u => u.UserUID == dbUser.UID).ConfigureAwait(false);
var groups = await db.Groups.Where(g => g.OwnerUID == dbUser.UID).ToListAsync().ConfigureAwait(false);
var groupsJoined = await db.GroupPairs.Where(g => g.GroupUserUID == dbUser.UID).ToListAsync().ConfigureAwait(false);
var identity = await _connectionMultiplexer.GetDatabase().StringGetAsync("UID:" + dbUser.UID).ConfigureAwait(false);
var identity = await _connectionMultiplexer.GetDatabase().StringGetAsync("UID:" + dbUser.UID, CommandFlags.PreferReplica).ConfigureAwait(false);
eb.WithTitle("User Information");
eb.WithDescription("This is the user information for Discord User <@" + userToCheckForDiscordId + ">" + Environment.NewLine + Environment.NewLine

View File

@@ -30,7 +30,7 @@ public class UserRequirementHandler : AuthorizationHandler<UserRequirement, HubI
if ((requirement.Requirements & UserRequirements.Identified) is UserRequirements.Identified)
{
var ident = await _redis.GetAsync<string>("UID:" + uid).ConfigureAwait(false);
var ident = await _redis.GetAsync<string>("UID:" + uid, CommandFlags.PreferReplica).ConfigureAwait(false);
if (ident == RedisValue.EmptyString) context.Fail();
}