mirror of https://github.com/abpframework/abp.git
csharpabpc-sharpframeworkblazoraspnet-coredotnet-coreaspnetcorearchitecturesaasdomain-driven-designangularmulti-tenancy
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.
40 lines
1.3 KiB
40 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.LockOrNullAsync(key, timeout, cancellationToken);
|
|
if (timeoutReleaser is not null)
|
|
{
|
|
return new LocalAbpDistributedLockHandle(timeoutReleaser);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|