Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions HzMemoryCache/HzCacheMemoryLocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace HzCache
public class HzCacheMemoryLockerOptions
{
public int lockPoolSize { get; set; } = 7872;
public int maxLockRetries { get; set; } = 3;
}

public class HzCacheMemoryLocker
Expand Down Expand Up @@ -93,7 +94,21 @@ public async ValueTask<object> AcquireLockAsync(string cacheName, string cacheIn
var acquired = false;
using (var waitForSemaphore = HzActivities.Source.StartActivityWithCommonTags(HzActivities.Names.WaitForSemaphore, HzActivities.Area.HzCacheMemoryLocker, key: key, async: true))
{
acquired = await semaphore.WaitAsync(timeout, token).ConfigureAwait(false);
for (var attempt = 0; attempt <= options.maxLockRetries; attempt++)
{
try
{
acquired = await semaphore.WaitAsync(timeout, token).ConfigureAwait(false);
break;
}
catch (ObjectDisposedException) when (attempt < options.maxLockRetries)
Comment thread
JoelNygren-Norce marked this conversation as resolved.
{
// The MemoryCache evicted and disposed this semaphore between GetSemaphore and WaitAsync.
// Remove the stale entry and retry with a fresh semaphore.
lockCache.Remove(key);
semaphore = GetSemaphore(cacheName, cacheInstanceId, key, logger);
}
}
Comment thread
JoelNygren-Norce marked this conversation as resolved.
}

if (acquired)
Expand Down Expand Up @@ -136,7 +151,19 @@ public async ValueTask<object> AcquireLockAsync(string cacheName, string cacheIn
HzActivities.Source.StartActivityWithCommonTags(HzActivities.Names.WaitForSemaphore,
HzActivities.Area.HzCacheMemoryLocker, key: key))
{
acquired = semaphore.Wait(timeout, token);
for (var attempt = 0; attempt <= options.maxLockRetries; attempt++)
{
try
{
acquired = semaphore.Wait(timeout, token);
break;
}
catch (ObjectDisposedException) when (attempt < options.maxLockRetries)
{
lockCache.Remove(key);
semaphore = GetSemaphore(cacheName, cacheInstanceId, key, logger);
}
}
Comment thread
JoelNygren-Norce marked this conversation as resolved.
}

if (acquired)
Expand Down
Loading