Switched to AsyncKeyedLock

pull/15817/head
Mark Cilia Vincenti 3 years ago
parent 71e5f29618
commit af46450617

@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" />
<PackageReference Include="AsyncKeyedLock" Version="6.2.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="$(MicrosoftPackageVersion)" />
</ItemGroup>

@ -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<string, SemaphoreSlim> _localSyncObjects = new();
private readonly AsyncKeyedLocker<string> _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);
}
}

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

Loading…
Cancel
Save