Browse Source

feat: Optimize Dapr-DistributedLocking module

pull/647/head
cKey 3 years ago
parent
commit
bec82448f2
  1. 4
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr/LINGYUN/Abp/Dapr/AbpDaprModule.cs
  2. 22
      aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/AbpDistributedLockingDaprOptions.cs
  3. 13
      aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs
  4. 8
      aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/ILockOwnerFinder.cs
  5. 30
      aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/LockOwnerFinder.cs
  6. 14
      aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/README.md

4
aspnet-core/modules/dapr/LINGYUN.Abp.Dapr/LINGYUN/Abp/Dapr/AbpDaprModule.cs

@ -1,7 +1,9 @@
using Volo.Abp.Modularity; using Volo.Abp.Json;
using Volo.Abp.Modularity;
namespace LINGYUN.Abp.Dapr namespace LINGYUN.Abp.Dapr
{ {
[DependsOn(typeof(AbpJsonModule))]
public class AbpDaprModule : AbpModule public class AbpDaprModule : AbpModule
{ {
} }

22
aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/AbpDistributedLockingDaprOptions.cs

@ -4,14 +4,32 @@ namespace LINGYUN.Abp.DistributedLocking.Dapr;
public class AbpDistributedLockingDaprOptions public class AbpDistributedLockingDaprOptions
{ {
/// <summary>
/// The name of the lock store to be queried.
/// </summary>
/// <remarks>
/// default: lockstore
/// </remarks>
public string StoreName { get; set; } public string StoreName { get; set; }
public string ResourceId { get; set; } /// <summary>
/// Indicates the identifier of lock owner.
/// </summary>
/// <remarks>
/// default: dapr-lock-owner
/// </remarks>
public string DefaultIdentifier { get; set; }
/// <summary>
/// The time after which the lock gets expired.
/// </summary>
/// <remarks>
/// default: 30 seconds
/// </remarks>
public TimeSpan DefaultTimeout { get; set; } public TimeSpan DefaultTimeout { get; set; }
public AbpDistributedLockingDaprOptions() public AbpDistributedLockingDaprOptions()
{ {
StoreName = "lockstore"; StoreName = "lockstore";
ResourceId = "dapr-lock-id"; DefaultIdentifier = "dapr-lock-owner";
DefaultTimeout = TimeSpan.FromSeconds(30); DefaultTimeout = TimeSpan.FromSeconds(30);
} }
} }

13
aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs

@ -12,16 +12,19 @@ namespace LINGYUN.Abp.DistributedLocking.Dapr;
[Dependency(ReplaceServices = true)] [Dependency(ReplaceServices = true)]
public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency
{ {
protected ILockOwnerFinder LockOwnerFinder { get; }
protected IDaprClientFactory DaprClientFactory { get; } protected IDaprClientFactory DaprClientFactory { get; }
protected AbpDistributedLockingDaprOptions Options { get; } protected AbpDistributedLockingDaprOptions Options { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; } protected ICancellationTokenProvider CancellationTokenProvider { get; }
public DaprAbpDistributedLock( public DaprAbpDistributedLock(
ILockOwnerFinder lockOwnerFinder,
IDaprClientFactory daprClientFactory, IDaprClientFactory daprClientFactory,
ICancellationTokenProvider cancellationTokenProvider, ICancellationTokenProvider cancellationTokenProvider,
IOptions<AbpDistributedLockingDaprOptions> options) IOptions<AbpDistributedLockingDaprOptions> options)
{ {
Options = options.Value; Options = options.Value;
LockOwnerFinder = lockOwnerFinder;
DaprClientFactory = daprClientFactory; DaprClientFactory = daprClientFactory;
CancellationTokenProvider = cancellationTokenProvider; CancellationTokenProvider = cancellationTokenProvider;
} }
@ -33,15 +36,21 @@ public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency
timeout = Options.DefaultTimeout; timeout = Options.DefaultTimeout;
} }
var client = DaprClientFactory.CreateClient(); var client = DaprClientFactory.CreateClient();
var lockOwner = await LockOwnerFinder.FindAsync();
var res = await client.Lock(Options.StoreName, name, Options.ResourceId, (int)timeout.TotalSeconds, cancellationToken); var res = await client.Lock(
Options.StoreName,
name,
lockOwner,
(int)timeout.TotalSeconds,
GetCancellationToken(cancellationToken));
if (res == null || !res.Success) if (res == null || !res.Success)
{ {
return null; return null;
} }
return new DaprAbpDistributedLockHandle(Options.StoreName, name, Options.ResourceId, client); return new DaprAbpDistributedLockHandle(Options.StoreName, name, lockOwner, client);
} }
protected virtual CancellationToken GetCancellationToken(CancellationToken cancellationToken = default) protected virtual CancellationToken GetCancellationToken(CancellationToken cancellationToken = default)

8
aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/ILockOwnerFinder.cs

@ -0,0 +1,8 @@
using System.Threading.Tasks;
namespace LINGYUN.Abp.DistributedLocking.Dapr;
public interface ILockOwnerFinder
{
Task<string> FindAsync();
}

30
aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/LockOwnerFinder.cs

@ -0,0 +1,30 @@
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Users;
namespace LINGYUN.Abp.DistributedLocking.Dapr;
public class LockOwnerFinder : ILockOwnerFinder, ITransientDependency
{
protected ICurrentUser CurrentUser { get; }
protected AbpDistributedLockingDaprOptions Options { get; }
public LockOwnerFinder(
ICurrentUser currentUser,
IOptions<AbpDistributedLockingDaprOptions> options)
{
Options = options.Value;
CurrentUser = currentUser;
}
public Task<string> FindAsync()
{
if (CurrentUser.IsAuthenticated)
{
return Task.FromResult(CurrentUser.GetId().ToString("N"));
}
return Task.FromResult(Options.DefaultIdentifier);
}
}

14
aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/README.md

@ -12,12 +12,12 @@ See: https://docs.dapr.io/developing-applications/building-blocks/distributed-lo
[DependsOn(typeof(AbpDistributedLockingDaprModule))] [DependsOn(typeof(AbpDistributedLockingDaprModule))]
public class YouProjectModule : AbpModule public class YouProjectModule : AbpModule
{ {
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
Configure<AbpDistributedLockingDaprOptions>(options => Configure<AbpDistributedLockingDaprOptions>(options =>
{ {
options.StoreName = "store-name"; options.StoreName = "store-name";
options.ResourceId = "resource-id"; options.DefaultIdentifier = "default-owner-id";
options.DefaultTimeout = TimeSpan.FromSeconds(30); options.DefaultTimeout = TimeSpan.FromSeconds(30);
}); });
} }
@ -26,9 +26,13 @@ public class YouProjectModule : AbpModule
## 配置说明 ## 配置说明
* AbpDistributedLockingDaprOptions.StoreName 在dapr component文件中定义的metadata name,默认: lockstore; * AbpDistributedLockingDaprOptions.StoreName 在dapr component文件中定义的metadata name,默认: lockstore;
* AbpDistributedLockingDaprOptions.ResourceId 自定义一个资源名称,用于向dapr提供锁定的标识,默认: dapr-lock-id; * AbpDistributedLockingDaprOptions.DefaultIdentifier 默认锁资源拥有者标识,默认: dapr-lock-owner;
* AbpDistributedLockingDaprOptions.DefaultTimeout 默认锁定超时时间,默认: 30s. * AbpDistributedLockingDaprOptions.DefaultTimeout 默认锁定超时时间,默认: 30s.
## 接口说明
[ILockOwnerFinder](./LINGYUN/Abp/DistributedLocking/Dapr/ILockOwnerFinder), 提供锁资源持有者标识
默认实现 [LockOwnerFinder](./LINGYUN/Abp/DistributedLocking/Dapr/LockOwnerFinder), 获取用户标识,如果不存在,返回DefaultIdentifier
## 其他 ## 其他

Loading…
Cancel
Save