From 5b1a9d913e212bd5169135e23226c7e539e31c04 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Wed, 21 Aug 2024 11:23:24 -0500 Subject: [PATCH] Lock sync --- .../BotSharp.Core/Infrastructures/DistributedLocker.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs index 0c20d983..8e88d9c3 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs @@ -29,7 +29,7 @@ public class DistributedLocker } } - public async Task Lock(string resource, Action action, int timeoutInSeconds = 30) + public async Task Lock(string resource, Func action, int timeoutInSeconds = 30) { var timeout = TimeSpan.FromSeconds(timeoutInSeconds); @@ -37,14 +37,12 @@ public class DistributedLocker var @lock = new RedisDistributedLock(resource, connection.GetDatabase()); await using (var handle = await @lock.TryAcquireAsync(timeout)) { - if (handle != null) - { - action(); - } - else + if (handle == null) { Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout."); + } + return action(); } } }