Browse Source

Switched to AsyncKeyedLock

pull/15817/head
Mark Cilia Vincenti 4 years ago
parent
commit
af46450617
  1. 1
      framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo.Abp.DistributedLocking.Abstractions.csproj
  2. 22
      framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs
  3. 10
      framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLockHandle.cs

1
framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo.Abp.DistributedLocking.Abstractions.csproj

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

22
framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs

@ -1,14 +1,18 @@
using System; using System;
using System.Collections.Concurrent;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using AsyncKeyedLock;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
namespace Volo.Abp.DistributedLocking; namespace Volo.Abp.DistributedLocking;
public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency 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; } protected IDistributedLockKeyNormalizer DistributedLockKeyNormalizer { get; }
public LocalAbpDistributedLock(IDistributedLockKeyNormalizer distributedLockKeyNormalizer) public LocalAbpDistributedLock(IDistributedLockKeyNormalizer distributedLockKeyNormalizer)
@ -23,14 +27,18 @@ public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency
{ {
Check.NotNullOrWhiteSpace(name, nameof(name)); Check.NotNullOrWhiteSpace(name, nameof(name));
var key = DistributedLockKeyNormalizer.NormalizeKey(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);
} }
} }

10
framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLockHandle.cs

@ -1,20 +1,20 @@
using System.Threading; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Volo.Abp.DistributedLocking; namespace Volo.Abp.DistributedLocking;
public class LocalAbpDistributedLockHandle : IAbpDistributedLockHandle 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() public ValueTask DisposeAsync()
{ {
_semaphore.Release(); _disposable.Dispose();
return default; return default;
} }
} }

Loading…
Cancel
Save