Open Source Web Application Framework for ASP.NET Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

41 lines
1.3 KiB

using System;
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 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,
CancellationToken cancellationToken = default)
{
Check.NotNullOrWhiteSpace(name, nameof(name));
var key = DistributedLockKeyNormalizer.NormalizeKey(name);
var timeoutReleaser = await _localSyncObjects.LockAsync(key, timeout, cancellationToken);
if (!timeoutReleaser.EnteredSemaphore)
{
timeoutReleaser.Dispose();
return null;
}
return new LocalAbpDistributedLockHandle(timeoutReleaser);
}
}