diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr/LINGYUN/Abp/Dapr/AbpDaprModule.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr/LINGYUN/Abp/Dapr/AbpDaprModule.cs index 8928bbfd0..855587bfa 100644 --- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr/LINGYUN/Abp/Dapr/AbpDaprModule.cs +++ b/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 { + [DependsOn(typeof(AbpJsonModule))] public class AbpDaprModule : AbpModule { } diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/AbpDistributedLockingDaprOptions.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/AbpDistributedLockingDaprOptions.cs index 054b6b4b8..6a645ab74 100644 --- a/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/AbpDistributedLockingDaprOptions.cs +++ b/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 { + /// + /// The name of the lock store to be queried. + /// + /// + /// default: lockstore + /// public string StoreName { get; set; } - public string ResourceId { get; set; } + /// + /// Indicates the identifier of lock owner. + /// + /// + /// default: dapr-lock-owner + /// + public string DefaultIdentifier { get; set; } + /// + /// The time after which the lock gets expired. + /// + /// + /// default: 30 seconds + /// public TimeSpan DefaultTimeout { get; set; } public AbpDistributedLockingDaprOptions() { StoreName = "lockstore"; - ResourceId = "dapr-lock-id"; + DefaultIdentifier = "dapr-lock-owner"; DefaultTimeout = TimeSpan.FromSeconds(30); } } diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs index 848fbd819..9a2744c29 100644 --- a/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs +++ b/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)] public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency { + protected ILockOwnerFinder LockOwnerFinder { get; } protected IDaprClientFactory DaprClientFactory { get; } protected AbpDistributedLockingDaprOptions Options { get; } protected ICancellationTokenProvider CancellationTokenProvider { get; } public DaprAbpDistributedLock( + ILockOwnerFinder lockOwnerFinder, IDaprClientFactory daprClientFactory, ICancellationTokenProvider cancellationTokenProvider, IOptions options) { Options = options.Value; + LockOwnerFinder = lockOwnerFinder; DaprClientFactory = daprClientFactory; CancellationTokenProvider = cancellationTokenProvider; } @@ -33,15 +36,21 @@ public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency timeout = Options.DefaultTimeout; } 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) { 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) diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/ILockOwnerFinder.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/ILockOwnerFinder.cs new file mode 100644 index 000000000..47c033127 --- /dev/null +++ b/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 FindAsync(); +} diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/LockOwnerFinder.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/LINGYUN/Abp/DistributedLocking/Dapr/LockOwnerFinder.cs new file mode 100644 index 000000000..c60443a9d --- /dev/null +++ b/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 options) + { + Options = options.Value; + CurrentUser = currentUser; + } + + public Task FindAsync() + { + if (CurrentUser.IsAuthenticated) + { + return Task.FromResult(CurrentUser.GetId().ToString("N")); + } + + return Task.FromResult(Options.DefaultIdentifier); + } +} diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/README.md b/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/README.md index 54c4680d1..a5adf52f0 100644 --- a/aspnet-core/modules/dapr/LINGYUN.Abp.DistributedLocking.Dapr/README.md +++ b/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))] public class YouProjectModule : AbpModule { - public override void ConfigureServices(ServiceConfigurationContext context) + public override void ConfigureServices(ServiceConfigurationContext context) { Configure(options => { options.StoreName = "store-name"; - options.ResourceId = "resource-id"; + options.DefaultIdentifier = "default-owner-id"; options.DefaultTimeout = TimeSpan.FromSeconds(30); }); } @@ -26,9 +26,13 @@ public class YouProjectModule : AbpModule ## 配置说明 -* AbpDistributedLockingDaprOptions.StoreName 在dapr component文件中定义的metadata name,默认: lockstore; -* AbpDistributedLockingDaprOptions.ResourceId 自定义一个资源名称,用于向dapr提供锁定的标识,默认: dapr-lock-id; -* AbpDistributedLockingDaprOptions.DefaultTimeout 默认锁定超时时间,默认: 30s. +* AbpDistributedLockingDaprOptions.StoreName 在dapr component文件中定义的metadata name,默认: lockstore; +* AbpDistributedLockingDaprOptions.DefaultIdentifier 默认锁资源拥有者标识,默认: dapr-lock-owner; +* AbpDistributedLockingDaprOptions.DefaultTimeout 默认锁定超时时间,默认: 30s. +## 接口说明 + +[ILockOwnerFinder](./LINGYUN/Abp/DistributedLocking/Dapr/ILockOwnerFinder), 提供锁资源持有者标识 +默认实现 [LockOwnerFinder](./LINGYUN/Abp/DistributedLocking/Dapr/LockOwnerFinder), 获取用户标识,如果不存在,返回DefaultIdentifier ## 其他