private readonly ConcurrentDictionary<string, SemaphoreSlim> entityLocks = new();
SemaphoreSlim semaphore = entityLocks.GetOrAdd(entity.Id, new SemaphoreSlim(1));
await semaphore.WaitAsync(TimeSpan.FromMilliseconds(config.LockTimeoutMilliseconds)).ConfigureAwait(false);
try
{
// do stuff
}
finally
{
semaphore.Release();
entityLocks.TryRemove(entity.Id, out _);
}
private readonly ConcurrentDictionary<string, SemaphoreSlim> entityLocks = new();
SemaphoreSlim semaphore = entityLocks.GetOrAdd(entity.Id, new SemaphoreSlim(1));
await semaphore.WaitAsync(TimeSpan.FromMilliseconds(config.LockTimeoutMilliseconds)).ConfigureAwait(false);
try
{
// do stuff
}
finally
{
semaphore.Release();
entityLocks.TryRemove(entity.Id, out _);
}