From 1c8590087ee264fd54435a72c5bfd51f84b7be38 Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Thu, 27 Nov 2025 17:03:37 +0800 Subject: [PATCH] optimize DistributedLocker --- .../Infrastructures/IDistributedLocker.cs | 4 ++-- .../Infrastructures/DistributedLocker.cs | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/IDistributedLocker.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/IDistributedLocker.cs index 2f5a80699..af4fc034d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/IDistributedLocker.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/IDistributedLocker.cs @@ -2,6 +2,6 @@ namespace BotSharp.Abstraction.Infrastructures; public interface IDistributedLocker { - bool Lock(string resource, Action action, int timeout = 30); - Task LockAsync(string resource, Func action, int timeout = 30); + bool Lock(string resource, Action action, int timeout = 30, int acquireTimeout = default); + Task LockAsync(string resource, Func action, int timeout = 30, int acquireTimeout = default); } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs index 30a967e86..06c96944d 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs @@ -17,9 +17,10 @@ public DistributedLocker( _logger = logger; } - public async Task LockAsync(string resource, Func action, int timeoutInSeconds = 30) + public async Task LockAsync(string resource, Func action, int timeoutInSeconds = 30, int acquireTimeoutInSeconds = default) { var timeout = TimeSpan.FromSeconds(timeoutInSeconds); + var acquireTimeout = TimeSpan.FromSeconds(acquireTimeoutInSeconds); var redis = _services.GetService(); if (redis == null) @@ -31,12 +32,12 @@ public async Task LockAsync(string resource, Func action, int timeou return true; } - var @lock = new RedisDistributedLock(resource, redis.GetDatabase()); - await using (var handle = await @lock.TryAcquireAsync(timeout)) + var @lock = new RedisDistributedLock(resource, redis.GetDatabase(),option => option.Expiry(timeout)); + await using (var handle = await @lock.TryAcquireAsync(acquireTimeout)) { if (handle == null) { - _logger.LogWarning($"Acquire lock for {resource} failed due to after {timeout}s timeout."); + _logger.LogWarning($"Acquire lock for {resource} failed due to after {acquireTimeout}s timeout."); return false; } @@ -45,9 +46,10 @@ public async Task LockAsync(string resource, Func action, int timeou } } - public bool Lock(string resource, Action action, int timeoutInSeconds = 30) + public bool Lock(string resource, Action action, int timeoutInSeconds = 30, int acquireTimeoutInSeconds = default) { var timeout = TimeSpan.FromSeconds(timeoutInSeconds); + var acquireTimeout = TimeSpan.FromSeconds(acquireTimeoutInSeconds); var redis = _services.GetRequiredService(); if (redis == null) @@ -59,12 +61,12 @@ public bool Lock(string resource, Action action, int timeoutInSeconds = 30) return false; } - var @lock = new RedisDistributedLock(resource, redis.GetDatabase()); - using (var handle = @lock.TryAcquire(timeout)) + var @lock = new RedisDistributedLock(resource, redis.GetDatabase(), option => option.Expiry(timeout)); + using (var handle = @lock.TryAcquire(acquireTimeout)) { if (handle == null) { - _logger.LogWarning($"Acquire lock for {resource} failed due to after {timeout}s timeout."); + _logger.LogWarning($"Acquire lock for {resource} failed due to after {acquireTimeout}s timeout."); return false; } else