Skip to content

Commit

Permalink
fix(slimdata): one semaphore per key
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Sep 4, 2024
1 parent 6871ee0 commit 8f13eab
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/SlimData/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,23 @@ public static Task ListRightPop(HttpContext context)
await context.Response.Body.WriteAsync(bin, context.RequestAborted);
});
}

private static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
private static readonly IDictionary<string,SemaphoreSlim> SemaphoreSlims = new Dictionary<string, SemaphoreSlim>();
public static async Task<ListString> ListRightPopCommand(SlimPersistentState provider, string key, int count, IRaftCluster cluster,
CancellationTokenSource source)
{
var values = new ListString();
values.Items = new List<byte[]>();
await semaphoreSlim.WaitAsync();

if(SemaphoreSlims.TryGetValue(key, out var semaphoreSlim))
{
await semaphoreSlim.WaitAsync();
}
else
{
SemaphoreSlims[key] = new SemaphoreSlim(1, 1);
await SemaphoreSlims[key].WaitAsync();
}
try
{
while (cluster.TryGetLeaseToken(out var leaseToken) && leaseToken.IsCancellationRequested)
Expand All @@ -133,7 +142,7 @@ public static async Task<ListString> ListRightPopCommand(SlimPersistentState pro
}
finally
{
semaphoreSlim.Release();
SemaphoreSlims[key].Release();
}
return values;

Expand Down

0 comments on commit 8f13eab

Please sign in to comment.