fix locker timeout issue.
This commit is contained in:
parent
f95dc6599d
commit
be83f2071e
|
|
@ -12,7 +12,7 @@ public class DistributedLocker
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Lock(string resource, Func<Task> action, int timeoutInSeconds = 30)
|
public async Task<T> Lock<T>(string resource, Func<Task<T>> action, int timeoutInSeconds = 30)
|
||||||
{
|
{
|
||||||
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);
|
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);
|
||||||
|
|
||||||
|
|
@ -20,14 +20,12 @@ public class DistributedLocker
|
||||||
var @lock = new RedisDistributedLock(resource, connection.GetDatabase());
|
var @lock = new RedisDistributedLock(resource, connection.GetDatabase());
|
||||||
await using (var handle = await @lock.TryAcquireAsync(timeout))
|
await using (var handle = await @lock.TryAcquireAsync(timeout))
|
||||||
{
|
{
|
||||||
if (handle != null)
|
if (handle == null)
|
||||||
{
|
|
||||||
await action();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
|
Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return await action();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue