Browse Source

Merge pull request #24425 from abpframework/LocalAbpDistributedLock

Remove `AsyncKeyedLock` dependency from `LocalAbpDistributedLock`.
pull/24496/head
Engincan VESKE 9 months ago
committed by GitHub
parent
commit
85b70a11d8
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      Directory.Packages.props
  2. 1
      framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo.Abp.DistributedLocking.Abstractions.csproj
  3. 19
      framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLock.cs
  4. 27
      framework/src/Volo.Abp.DistributedLocking.Abstractions/Volo/Abp/DistributedLocking/LocalAbpDistributedLockHandle.cs

1
Directory.Packages.props

@ -7,7 +7,6 @@
<PackageVersion Include="AlibabaCloud.SDK.Dysmsapi20170525" Version="4.0.0" /> <PackageVersion Include="AlibabaCloud.SDK.Dysmsapi20170525" Version="4.0.0" />
<PackageVersion Include="aliyun-net-sdk-sts" Version="3.1.3" /> <PackageVersion Include="aliyun-net-sdk-sts" Version="3.1.3" />
<PackageVersion Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" /> <PackageVersion Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
<PackageVersion Include="AsyncKeyedLock" Version="7.1.6" />
<PackageVersion Include="Autofac" Version="8.4.0" /> <PackageVersion Include="Autofac" Version="8.4.0" />
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="10.0.0" /> <PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="10.0.0" />
<PackageVersion Include="Autofac.Extras.DynamicProxy" Version="7.1.0" /> <PackageVersion Include="Autofac.Extras.DynamicProxy" Version="7.1.0" />

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

@ -18,7 +18,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> <ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" />
<PackageReference Include="AsyncKeyedLock" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" /> <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
</ItemGroup> </ItemGroup>

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

@ -1,19 +1,14 @@
using System; using System;
using System.Runtime.CompilerServices; 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 AsyncKeyedLocker<string> _localSyncObjects = new(o => private readonly ConcurrentDictionary<string, SemaphoreSlim> _localSyncObjects = new();
{
o.PoolSize = 20;
o.PoolInitialFill = 1;
});
protected IDistributedLockKeyNormalizer DistributedLockKeyNormalizer { get; } protected IDistributedLockKeyNormalizer DistributedLockKeyNormalizer { get; }
public LocalAbpDistributedLock(IDistributedLockKeyNormalizer distributedLockKeyNormalizer) public LocalAbpDistributedLock(IDistributedLockKeyNormalizer distributedLockKeyNormalizer)
@ -21,7 +16,6 @@ public class LocalAbpDistributedLock : IAbpDistributedLock, ISingletonDependency
DistributedLockKeyNormalizer = distributedLockKeyNormalizer; DistributedLockKeyNormalizer = distributedLockKeyNormalizer;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async Task<IAbpDistributedLockHandle?> TryAcquireAsync( public async Task<IAbpDistributedLockHandle?> TryAcquireAsync(
string name, string name,
TimeSpan timeout = default, TimeSpan timeout = default,
@ -30,11 +24,8 @@ 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 timeoutReleaser = await _localSyncObjects.LockOrNullAsync(key, timeout, cancellationToken); var semaphore = _localSyncObjects.GetOrAdd(key, _ => new SemaphoreSlim(1, 1));
if (timeoutReleaser is not null) var acquired = await semaphore.WaitAsync(timeout, cancellationToken);
{ return acquired ? new LocalAbpDistributedLockHandle(semaphore) : null;
return new LocalAbpDistributedLockHandle(timeoutReleaser);
}
return null;
} }
} }

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

@ -1,20 +1,21 @@
using System; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Volo.Abp.DistributedLocking; namespace Volo.Abp.DistributedLocking
public class LocalAbpDistributedLockHandle : IAbpDistributedLockHandle
{ {
private readonly IDisposable _disposable; public class LocalAbpDistributedLockHandle : IAbpDistributedLockHandle
public LocalAbpDistributedLockHandle(IDisposable disposable)
{ {
_disposable = disposable; private readonly SemaphoreSlim _semaphore;
}
public ValueTask DisposeAsync() public LocalAbpDistributedLockHandle(SemaphoreSlim semaphore)
{ {
_disposable.Dispose(); _semaphore = semaphore;
return default; }
public ValueTask DisposeAsync()
{
_semaphore.Release();
return default;
}
} }
} }

Loading…
Cancel
Save