From 24040fd1c20ad430ef953e9a671f20da2b3a8381 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Sun, 5 Feb 2023 08:36:54 +0100 Subject: [PATCH 1/4] Switched to AsyncKeyedLock --- src/Chronicle/Async/KeyedLocker.cs | 67 ----------------------- src/Chronicle/Chronicle.csproj | 1 + src/Chronicle/Managers/SagaCoordinator.cs | 10 +++- 3 files changed, 8 insertions(+), 70 deletions(-) delete mode 100644 src/Chronicle/Async/KeyedLocker.cs diff --git a/src/Chronicle/Async/KeyedLocker.cs b/src/Chronicle/Async/KeyedLocker.cs deleted file mode 100644 index 320fd69..0000000 --- a/src/Chronicle/Async/KeyedLocker.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace Chronicle.Async -{ - public sealed class KeyedLocker - { - private static readonly Dictionary> SemaphoreSlims - = new Dictionary>(); - - private SemaphoreSlim GetOrCreate(object key) - { - RefCounted item; - lock (SemaphoreSlims) - { - if (SemaphoreSlims.TryGetValue(key, out item)) - { - ++item.RefCount; - } - else - { - item = new RefCounted(new SemaphoreSlim(1, 1)); - SemaphoreSlims[key] = item; - } - } - return item.Value; - } - - public async Task LockAsync(object key) - { - await GetOrCreate(key).WaitAsync().ConfigureAwait(false); - return new Releaser { Key = key }; - } - - private sealed class RefCounted - { - public RefCounted(T value) - { - RefCount = 1; - Value = value; - } - - public int RefCount { get; set; } - public T Value { get; } - } - - private sealed class Releaser : IDisposable - { - public object Key { get; set; } - - public void Dispose() - { - RefCounted item; - lock (SemaphoreSlims) - { - item = SemaphoreSlims[Key]; - --item.RefCount; - if (item.RefCount is 0) - SemaphoreSlims.Remove(Key); - } - item.Value.Release(); - } - } - } -} diff --git a/src/Chronicle/Chronicle.csproj b/src/Chronicle/Chronicle.csproj index aa237dc..a256dad 100644 --- a/src/Chronicle/Chronicle.csproj +++ b/src/Chronicle/Chronicle.csproj @@ -19,6 +19,7 @@ + diff --git a/src/Chronicle/Managers/SagaCoordinator.cs b/src/Chronicle/Managers/SagaCoordinator.cs index 9e877b8..531c109 100644 --- a/src/Chronicle/Managers/SagaCoordinator.cs +++ b/src/Chronicle/Managers/SagaCoordinator.cs @@ -1,7 +1,7 @@ using System; using System.Linq; using System.Threading.Tasks; -using Chronicle.Async; +using AsyncKeyedLock; namespace Chronicle.Managers { @@ -11,7 +11,11 @@ internal sealed class SagaCoordinator : ISagaCoordinator private readonly ISagaInitializer _initializer; private readonly ISagaProcessor _processor; private readonly ISagaPostProcessor _postProcessor; - private static readonly KeyedLocker Locker = new KeyedLocker(); + private static readonly AsyncKeyedLocker _asyncKeyedLocker = new(o => + { + o.PoolSize = 20; + o.PoolInitialFill = 1; + }); public SagaCoordinator(ISagaSeeker seeker, ISagaInitializer initializer, ISagaProcessor processor, ISagaPostProcessor postProcessor) @@ -49,7 +53,7 @@ private async Task ProcessAsync(TMessage message, ISagaAction Date: Sat, 25 Feb 2023 14:46:11 +0100 Subject: [PATCH 2/4] Update to AsyncKeyedLock 6.2.0 --- src/Chronicle/Chronicle.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chronicle/Chronicle.csproj b/src/Chronicle/Chronicle.csproj index a256dad..d739589 100644 --- a/src/Chronicle/Chronicle.csproj +++ b/src/Chronicle/Chronicle.csproj @@ -19,7 +19,7 @@ - + From 72a8aca5a4e33f413565f8c17cb7df9435dfdf95 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 10 Apr 2023 10:10:57 +0200 Subject: [PATCH 3/4] Update Chronicle.csproj --- src/Chronicle/Chronicle.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chronicle/Chronicle.csproj b/src/Chronicle/Chronicle.csproj index d739589..4c1f29a 100644 --- a/src/Chronicle/Chronicle.csproj +++ b/src/Chronicle/Chronicle.csproj @@ -19,7 +19,7 @@ - + From fa02dfa005bfb5de22e9b97d0d2749b2e79927bb Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Sat, 3 Feb 2024 17:23:51 +0100 Subject: [PATCH 4/4] Bump AsyncKeyedLock to 6.3.4 --- src/Chronicle/Chronicle.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chronicle/Chronicle.csproj b/src/Chronicle/Chronicle.csproj index 4c1f29a..d15f784 100644 --- a/src/Chronicle/Chronicle.csproj +++ b/src/Chronicle/Chronicle.csproj @@ -19,7 +19,7 @@ - +