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

49 lines
1.4 KiB
C#
Raw Normal View History

2024-07-15 21:38:47 +00:00
using Medallion.Threading.Redis;
2024-04-30 12:50:01 +00:00
using StackExchange.Redis;
namespace BotSharp.Core.Infrastructures;
public class DistributedLocker
{
2024-11-08 18:10:15 +00:00
private readonly IConnectionMultiplexer _redis;
2024-04-30 12:50:01 +00:00
2024-11-08 18:10:15 +00:00
public DistributedLocker(IConnectionMultiplexer redis)
2024-04-30 12:50:01 +00:00
{
2024-11-08 18:10:15 +00:00
_redis = redis;
2024-07-15 21:38:47 +00:00
}
2024-08-21 15:26:36 +00:00
public async Task<T> Lock<T>(string resource, Func<Task<T>> action, int timeoutInSeconds = 30)
2024-07-15 21:38:47 +00:00
{
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);
2024-04-30 12:50:01 +00:00
2024-11-08 18:10:15 +00:00
var @lock = new RedisDistributedLock(resource, _redis.GetDatabase());
2024-07-15 21:38:47 +00:00
await using (var handle = await @lock.TryAcquireAsync(timeout))
2024-04-30 12:50:01 +00:00
{
2024-08-21 15:26:36 +00:00
if (handle == null)
2024-04-30 12:50:01 +00:00
{
2024-07-15 21:38:47 +00:00
Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
}
2024-08-21 15:26:36 +00:00
return await action();
2024-04-30 12:50:01 +00:00
}
}
2024-11-05 13:29:06 +00:00
public void Lock(string resource, Action action, int timeoutInSeconds = 30)
2024-04-30 12:50:01 +00:00
{
2024-08-21 19:48:12 +00:00
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);
2024-08-21 18:41:52 +00:00
2024-11-08 18:10:15 +00:00
var @lock = new RedisDistributedLock(resource, _redis.GetDatabase());
2024-11-05 13:29:06 +00:00
using (var handle = @lock.TryAcquire(timeout))
2024-04-30 12:50:01 +00:00
{
2024-08-21 16:23:24 +00:00
if (handle == null)
2024-04-30 12:50:01 +00:00
{
2024-07-15 21:38:47 +00:00
Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
2024-04-30 12:50:01 +00:00
}
2024-11-05 13:29:06 +00:00
else
{
action();
}
}
}
2024-04-30 12:50:01 +00:00
}