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;
}
}