Browse Source

Merge pull request #15817 from MarkCiliaVincenti/AsyncKeyedLock

pull/16061/head
maliming 4 years ago
committed by GitHub
parent
commit
b36c9ad6d1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo.Abp.DistributedLocking.Abstractions.csproj
  2. 21
      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>
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" />
<PackageReference Include="AsyncKeyedLock" Version="6.2.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="$(MicrosoftPackageVersion)" />
</ItemGroup>

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

@ -1,21 +1,27 @@
using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
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)
{
DistributedLockKeyNormalizer = distributedLockKeyNormalizer;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async Task<IAbpDistributedLockHandle> TryAcquireAsync(
string name,
TimeSpan timeout = default,
@ -23,14 +29,13 @@ 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))
var timeoutReleaser = await _localSyncObjects.LockAsync(key, timeout, cancellationToken);
if (!timeoutReleaser.EnteredSemaphore)
{
timeoutReleaser.Dispose();
return null;
}
return new LocalAbpDistributedLockHandle(semaphore);
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;
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