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..d15f784 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