🐛 Fixes distributed lock
This commit is contained in:
parent
916d9500a2
commit
b4c26f2d55
@ -143,8 +143,21 @@ public class AccountEventService(
|
|||||||
{
|
{
|
||||||
var lockKey = $"{CheckInLockKey}{user.Id}";
|
var lockKey = $"{CheckInLockKey}{user.Id}";
|
||||||
|
|
||||||
await using var lk = await cache.AcquireLockAsync(lockKey, TimeSpan.FromMinutes(10), TimeSpan.Zero);
|
try
|
||||||
if (lk is null) throw new InvalidOperationException("Check-in was in progress.");
|
{
|
||||||
|
var lk = await cache.AcquireLockAsync(lockKey, TimeSpan.FromMinutes(1), TimeSpan.FromMilliseconds(100));
|
||||||
|
|
||||||
|
if (lk != null)
|
||||||
|
await lk.ReleaseAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Ignore errors from this pre-check
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now try to acquire the lock properly
|
||||||
|
await using var lockObj = await cache.AcquireLockAsync(lockKey, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(5));
|
||||||
|
if (lockObj is null) throw new InvalidOperationException("Check-in was in progress.");
|
||||||
|
|
||||||
var cultureInfo = new CultureInfo(user.Language, false);
|
var cultureInfo = new CultureInfo(user.Language, false);
|
||||||
CultureInfo.CurrentCulture = cultureInfo;
|
CultureInfo.CurrentCulture = cultureInfo;
|
||||||
@ -205,6 +218,7 @@ public class AccountEventService(
|
|||||||
s.SetProperty(b => b.Experience, b => b.Experience + result.RewardExperience)
|
s.SetProperty(b => b.Experience, b => b.Experience + result.RewardExperience)
|
||||||
);
|
);
|
||||||
db.AccountCheckInResults.Add(result);
|
db.AccountCheckInResults.Add(result);
|
||||||
|
await db.SaveChangesAsync(); // Don't forget to save changes to the database
|
||||||
|
|
||||||
await act.CreateActivity(
|
await act.CreateActivity(
|
||||||
user,
|
user,
|
||||||
|
@ -117,8 +117,6 @@ public class RedisDistributedLock : IDistributedLock
|
|||||||
private readonly IDatabase _database;
|
private readonly IDatabase _database;
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
|
|
||||||
private const string LockKeyPrefix = "Lock_";
|
|
||||||
|
|
||||||
public string Resource { get; }
|
public string Resource { get; }
|
||||||
public string LockId { get; }
|
public string LockId { get; }
|
||||||
|
|
||||||
@ -144,7 +142,7 @@ public class RedisDistributedLock : IDistributedLock
|
|||||||
|
|
||||||
var result = await _database.ScriptEvaluateAsync(
|
var result = await _database.ScriptEvaluateAsync(
|
||||||
script,
|
script,
|
||||||
[$"{LockKeyPrefix}{Resource}"],
|
[$"{CacheServiceRedis.LockKeyPrefix}{Resource}"],
|
||||||
[LockId, (long)timeSpan.TotalMilliseconds]
|
[LockId, (long)timeSpan.TotalMilliseconds]
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -166,7 +164,7 @@ public class RedisDistributedLock : IDistributedLock
|
|||||||
|
|
||||||
await _database.ScriptEvaluateAsync(
|
await _database.ScriptEvaluateAsync(
|
||||||
script,
|
script,
|
||||||
[$"{LockKeyPrefix}{Resource}"],
|
[$"{CacheServiceRedis.LockKeyPrefix}{Resource}"],
|
||||||
[LockId]
|
[LockId]
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -186,11 +184,11 @@ public class CacheServiceRedis : ICacheService
|
|||||||
private readonly JsonSerializerSettings _serializerSettings;
|
private readonly JsonSerializerSettings _serializerSettings;
|
||||||
|
|
||||||
// Global prefix for all cache keys
|
// Global prefix for all cache keys
|
||||||
private const string GlobalKeyPrefix = "dyson:";
|
public const string GlobalKeyPrefix = "dyson:";
|
||||||
|
|
||||||
// Using prefixes for different types of keys
|
// Using prefixes for different types of keys
|
||||||
private const string GroupKeyPrefix = GlobalKeyPrefix + "cg:";
|
public const string GroupKeyPrefix = GlobalKeyPrefix + "cg:";
|
||||||
private const string LockKeyPrefix = GlobalKeyPrefix + "lock:";
|
public const string LockKeyPrefix = GlobalKeyPrefix + "lock:";
|
||||||
|
|
||||||
public CacheServiceRedis(IConnectionMultiplexer redis)
|
public CacheServiceRedis(IConnectionMultiplexer redis)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user