From af46450617ef4f0396419b9f68b7dd28a2ebc877 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 27 Feb 2023 09:46:06 +0100 Subject: [PATCH 1/5] Switched to AsyncKeyedLock --- ...Abp.DistributedLocking.Abstractions.csproj | 1 + .../LocalAbpDistributedLock.cs | 22 +++++++++++++------ .../LocalAbpDistributedLockHandle.cs | 10 ++++----- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo.Abp.DistributedLocking.Abstractions.csproj b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo.Abp.DistributedLocking.Abstractions.csproj index 19c1b4f869..c75303ad2a 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo.Abp.DistributedLocking.Abstractions.csproj +++ b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo.Abp.DistributedLocking.Abstractions.csproj @@ -16,6 +16,7 @@ + diff --git a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs index fdec4f1d3f..77e583dcfa 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs @@ -1,14 +1,18 @@ using System; -using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks; +using AsyncKeyedLock; using Volo.Abp.DependencyInjection; namespace Volo.Abp.DistributedLocking; public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency { - private readonly ConcurrentDictionary _localSyncObjects = new(); + private readonly AsyncKeyedLocker _localSyncObjects = new(o => + { + o.PoolSize = 20; + o.PoolInitialFill = 1; + }); protected IDistributedLockKeyNormalizer DistributedLockKeyNormalizer { get; } public LocalAbpDistributedLock(IDistributedLockKeyNormalizer distributedLockKeyNormalizer) @@ -23,14 +27,18 @@ public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency { Check.NotNullOrWhiteSpace(name, nameof(name)); var key = DistributedLockKeyNormalizer.NormalizeKey(name); - - var semaphore = _localSyncObjects.GetOrAdd(key, _ => new SemaphoreSlim(1, 1)); - if (!await semaphore.WaitAsync(timeout, cancellationToken)) + if (timeout == default) { - return null; + var releaser = await _localSyncObjects.LockAsync(key, cancellationToken); + return new LocalAbpDistributedLockHandle(releaser); } - return new LocalAbpDistributedLockHandle(semaphore); + var timeoutReleaser = await _localSyncObjects.LockAsync(key, timeout, cancellationToken); + if (!timeoutReleaser.EnteredSemaphore) + { + return null; + } + return new LocalAbpDistributedLockHandle(timeoutReleaser); } } diff --git a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLockHandle.cs b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLockHandle.cs index f4f6d640bc..d08451657e 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLockHandle.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLockHandle.cs @@ -1,20 +1,20 @@ -using System.Threading; +using System; using System.Threading.Tasks; namespace Volo.Abp.DistributedLocking; public class LocalAbpDistributedLockHandle : IAbpDistributedLockHandle { - private readonly SemaphoreSlim _semaphore; + private readonly IDisposable _disposable; - public LocalAbpDistributedLockHandle(SemaphoreSlim semaphore) + public LocalAbpDistributedLockHandle(IDisposable disposable) { - _semaphore = semaphore; + _disposable = disposable; } public ValueTask DisposeAsync() { - _semaphore.Release(); + _disposable.Dispose(); return default; } } From 445da6a4c7cce900f12afc64985a52735aedd6d9 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 27 Feb 2023 14:22:08 +0100 Subject: [PATCH 2/5] Fixed code to work exactly as the original. --- .../Abp/DistributedLocking/LocalAbpDistributedLock.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs index 77e583dcfa..d7c2dde2b1 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs @@ -28,11 +28,11 @@ public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency Check.NotNullOrWhiteSpace(name, nameof(name)); var key = DistributedLockKeyNormalizer.NormalizeKey(name); - if (timeout == default) - { - var releaser = await _localSyncObjects.LockAsync(key, cancellationToken); - return new LocalAbpDistributedLockHandle(releaser); - } + //if (timeout == default) + //{ + // var releaser = await _localSyncObjects.LockAsync(key, cancellationToken); + // return new LocalAbpDistributedLockHandle(releaser); + //} var timeoutReleaser = await _localSyncObjects.LockAsync(key, timeout, cancellationToken); if (!timeoutReleaser.EnteredSemaphore) From bf3111cb07024e89b49a3734f6e83623a0f09656 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 27 Feb 2023 14:24:18 +0100 Subject: [PATCH 3/5] Fixed issue with TryAcquireAsync --- .../Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs index d7c2dde2b1..526df6464a 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs @@ -28,12 +28,6 @@ public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency Check.NotNullOrWhiteSpace(name, nameof(name)); var key = DistributedLockKeyNormalizer.NormalizeKey(name); - //if (timeout == default) - //{ - // var releaser = await _localSyncObjects.LockAsync(key, cancellationToken); - // return new LocalAbpDistributedLockHandle(releaser); - //} - var timeoutReleaser = await _localSyncObjects.LockAsync(key, timeout, cancellationToken); if (!timeoutReleaser.EnteredSemaphore) { From 0e615b3acfa5352bf6cd03fa113748f6f0a81fc0 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 27 Feb 2023 14:26:22 +0100 Subject: [PATCH 4/5] AggressiveInlining for TryAcquireAsync --- .../Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs index 526df6464a..0b3674c5bf 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using AsyncKeyedLock; @@ -19,7 +20,8 @@ public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency { DistributedLockKeyNormalizer = distributedLockKeyNormalizer; } - + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task TryAcquireAsync( string name, TimeSpan timeout = default, From 73f85a07183d556aa5fc27601c84e60af561dabf Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 27 Feb 2023 19:20:07 +0100 Subject: [PATCH 5/5] Dispose releaser when not entered --- .../Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs index 0b3674c5bf..490f024d36 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs @@ -33,6 +33,7 @@ public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency var timeoutReleaser = await _localSyncObjects.LockAsync(key, timeout, cancellationToken); if (!timeoutReleaser.EnteredSemaphore) { + timeoutReleaser.Dispose(); return null; } return new LocalAbpDistributedLockHandle(timeoutReleaser);