Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion source/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<CentralPackageVersionOverrideEnabled>false</CentralPackageVersionOverrideEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AsyncKeyedLock" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
Expand All @@ -18,4 +19,4 @@
<PackageVersion Include="Confluent.Kafka" Version="2.5.2" />
<PackageVersion Include="Scrutor" Version="4.2.2" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
using System.Collections.Concurrent;
using AsyncKeyedLock;

namespace TransactionalBox.Internals.InternalPackages.KeyedInMemoryLock
{
internal sealed class InternalKeyedInMemoryLock : IKeyedInMemoryLock
{
private static ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
private static readonly AsyncKeyedLocker<string> _locks = new();

public async Task<ILockInstance> Acquire(string key, CancellationToken cancellationToken = default)
{
var @lock = _locks.GetOrAdd(key, x => new SemaphoreSlim(1, 1));

await @lock.WaitAsync(cancellationToken);

return new LockInstance(@lock);
return new LockInstance(await _locks.LockAsync(key, cancellationToken));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace TransactionalBox.Internals.InternalPackages.KeyedInMemoryLock
{
internal sealed class LockInstance : ILockInstance
internal readonly struct LockInstance : ILockInstance
{
private readonly SemaphoreSlim _semaphoreSlim;
private readonly IDisposable _instance;

internal LockInstance(SemaphoreSlim semaphoreSlim) => _semaphoreSlim = semaphoreSlim;
internal LockInstance(IDisposable instance) => _instance = instance;

public void Dispose() => _semaphoreSlim.Release();
public void Dispose() => _instance.Dispose();
}
}
1 change: 1 addition & 0 deletions source/TransactionalBox/TransactionalBox.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="AsyncKeyedLock" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
Expand Down