Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions HzMemoryCache/Diagnostics/HzActivities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static class Names

public const string AcquireLock = "acquire lock";
public const string GetSemaphore = "get semaphore";
public const string WaitForSemaphore = "wait for semaphore";
public const string ReleaseLock = "release lock";
}

Expand Down
16 changes: 12 additions & 4 deletions HzMemoryCache/HzCacheMemoryLocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,18 @@ private SemaphoreSlim GetSemaphore(string cacheName, string cacheInstanceId, str
public async ValueTask<object> AcquireLockAsync(string cacheName, string cacheInstanceId, string operationId, string key, TimeSpan timeout, ILogger? logger,
CancellationToken token)
{
using var activity = HzActivities.Source.StartActivityWithCommonTags(HzActivities.Names.GetSemaphore, HzActivities.Area.HzCacheMemoryLocker, key: key);
using var activity = HzActivities.Source.StartActivityWithCommonTags(HzActivities.Names.AcquireLock, HzActivities.Area.HzCacheMemoryLocker, key: key);
var semaphore = GetSemaphore(cacheName, cacheInstanceId, key, logger);

if (logger?.IsEnabled(LogLevel.Trace) ?? false)
{
logger.Log(LogLevel.Trace, "HZ [N={CacheName} I={CacheInstanceId}] (O={CacheOperationId} K={CacheKey}): waiting to acquire the LOCK", cacheName,
cacheInstanceId, operationId, key);
}

var acquired = await semaphore.WaitAsync(timeout, token).ConfigureAwait(false);
var acquired = false;
using (var waitForSemaphore = HzActivities.Source.StartActivityWithCommonTags(HzActivities.Names.WaitForSemaphore, HzActivities.Area.HzCacheMemoryLocker, key: key)){
acquired = await semaphore.WaitAsync(timeout, token).ConfigureAwait(false);
}

if (acquired)
{
Expand Down Expand Up @@ -128,8 +130,14 @@ public async ValueTask<object> AcquireLockAsync(string cacheName, string cacheIn
logger.Log(LogLevel.Trace, "HZ [N={CacheName} I={CacheInstanceId}] (O={CacheOperationId} K={CacheKey}): waiting to acquire the LOCK", cacheName,
cacheInstanceId, operationId, key);
}
var acquired = false;
using (var waitForSemaphore =
HzActivities.Source.StartActivityWithCommonTags(HzActivities.Names.WaitForSemaphore,
HzActivities.Area.HzCacheMemoryLocker, key: key))
{

var acquired = semaphore.Wait(timeout, token);
acquired = semaphore.Wait(timeout, token);
}

if (acquired)
{
Expand Down
Loading