From 0294c80bccc542339a2d4e029c11ceb95374a626 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 15 Dec 2025 16:50:45 +0800 Subject: [PATCH 1/2] Remove AsyncKeyedLock dependency and refactor locking --- Directory.Packages.props | 1 - ...Abp.DistributedLocking.Abstractions.csproj | 1 - .../LocalAbpDistributedLock.cs | 19 +++++-------------- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 9a1fcbb674..ef813ad076 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -7,7 +7,6 @@ - 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 83f8f0076b..773c954051 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 @@ -18,7 +18,6 @@ - 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 15956b159e..d8d8eb9d09 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,19 +1,14 @@ using System; -using System.Runtime.CompilerServices; +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 AsyncKeyedLocker _localSyncObjects = new(o => - { - o.PoolSize = 20; - o.PoolInitialFill = 1; - }); + private readonly ConcurrentDictionary _localSyncObjects = new(); protected IDistributedLockKeyNormalizer DistributedLockKeyNormalizer { get; } public LocalAbpDistributedLock(IDistributedLockKeyNormalizer distributedLockKeyNormalizer) @@ -21,7 +16,6 @@ public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency DistributedLockKeyNormalizer = distributedLockKeyNormalizer; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task TryAcquireAsync( string name, TimeSpan timeout = default, @@ -30,11 +24,8 @@ public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency Check.NotNullOrWhiteSpace(name, nameof(name)); var key = DistributedLockKeyNormalizer.NormalizeKey(name); - var timeoutReleaser = await _localSyncObjects.LockOrNullAsync(key, timeout, cancellationToken); - if (timeoutReleaser is not null) - { - return new LocalAbpDistributedLockHandle(timeoutReleaser); - } - return null; + var semaphore = _localSyncObjects.GetOrAdd(key, _ => new SemaphoreSlim(1, 1)); + var acquired = await semaphore.WaitAsync(timeout, cancellationToken); + return acquired ? new LocalAbpDistributedLockHandle(semaphore) : null; } } From 917069fdf59909457cd552b92b40f0d0eca8de39 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 15 Dec 2025 18:05:17 +0800 Subject: [PATCH 2/2] Refactor LocalAbpDistributedLockHandle to use SemaphoreSlim --- .../LocalAbpDistributedLockHandle.cs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) 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 d08451657e..5ffe95af5e 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,21 @@ -using System; +using System.Threading; using System.Threading.Tasks; -namespace Volo.Abp.DistributedLocking; - -public class LocalAbpDistributedLockHandle : IAbpDistributedLockHandle +namespace Volo.Abp.DistributedLocking { - private readonly IDisposable _disposable; - - public LocalAbpDistributedLockHandle(IDisposable disposable) + public class LocalAbpDistributedLockHandle : IAbpDistributedLockHandle { - _disposable = disposable; - } + private readonly SemaphoreSlim _semaphore; - public ValueTask DisposeAsync() - { - _disposable.Dispose(); - return default; + public LocalAbpDistributedLockHandle(SemaphoreSlim semaphore) + { + _semaphore = semaphore; + } + + public ValueTask DisposeAsync() + { + _semaphore.Release(); + return default; + } } }