Skip to content

Commit a42b058

Browse files
authored
Merge pull request #1227 from yileicn/master
optimize DistributedLocker
2 parents fcbe7ed + 1c85900 commit a42b058

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/Infrastructure/BotSharp.Abstraction/Infrastructures/IDistributedLocker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ namespace BotSharp.Abstraction.Infrastructures;
22

33
public interface IDistributedLocker
44
{
5-
bool Lock(string resource, Action action, int timeout = 30);
6-
Task<bool> LockAsync(string resource, Func<Task> action, int timeout = 30);
5+
bool Lock(string resource, Action action, int timeout = 30, int acquireTimeout = default);
6+
Task<bool> LockAsync(string resource, Func<Task> action, int timeout = 30, int acquireTimeout = default);
77
}

src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ public DistributedLocker(
1717
_logger = logger;
1818
}
1919

20-
public async Task<bool> LockAsync(string resource, Func<Task> action, int timeoutInSeconds = 30)
20+
public async Task<bool> LockAsync(string resource, Func<Task> action, int timeoutInSeconds = 30, int acquireTimeoutInSeconds = default)
2121
{
2222
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);
23+
var acquireTimeout = TimeSpan.FromSeconds(acquireTimeoutInSeconds);
2324

2425
var redis = _services.GetService<IConnectionMultiplexer>();
2526
if (redis == null)
@@ -31,12 +32,12 @@ public async Task<bool> LockAsync(string resource, Func<Task> action, int timeou
3132
return true;
3233
}
3334

34-
var @lock = new RedisDistributedLock(resource, redis.GetDatabase());
35-
await using (var handle = await @lock.TryAcquireAsync(timeout))
35+
var @lock = new RedisDistributedLock(resource, redis.GetDatabase(),option => option.Expiry(timeout));
36+
await using (var handle = await @lock.TryAcquireAsync(acquireTimeout))
3637
{
3738
if (handle == null)
3839
{
39-
_logger.LogWarning($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
40+
_logger.LogWarning($"Acquire lock for {resource} failed due to after {acquireTimeout}s timeout.");
4041
return false;
4142
}
4243

@@ -45,9 +46,10 @@ public async Task<bool> LockAsync(string resource, Func<Task> action, int timeou
4546
}
4647
}
4748

48-
public bool Lock(string resource, Action action, int timeoutInSeconds = 30)
49+
public bool Lock(string resource, Action action, int timeoutInSeconds = 30, int acquireTimeoutInSeconds = default)
4950
{
5051
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);
52+
var acquireTimeout = TimeSpan.FromSeconds(acquireTimeoutInSeconds);
5153

5254
var redis = _services.GetRequiredService<IConnectionMultiplexer>();
5355
if (redis == null)
@@ -59,12 +61,12 @@ public bool Lock(string resource, Action action, int timeoutInSeconds = 30)
5961
return false;
6062
}
6163

62-
var @lock = new RedisDistributedLock(resource, redis.GetDatabase());
63-
using (var handle = @lock.TryAcquire(timeout))
64+
var @lock = new RedisDistributedLock(resource, redis.GetDatabase(), option => option.Expiry(timeout));
65+
using (var handle = @lock.TryAcquire(acquireTimeout))
6466
{
6567
if (handle == null)
6668
{
67-
_logger.LogWarning($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
69+
_logger.LogWarning($"Acquire lock for {resource} failed due to after {acquireTimeout}s timeout.");
6870
return false;
6971
}
7072
else

0 commit comments

Comments
 (0)