AsyncKeyedLock 3.2.0

AsyncKeyedLock AsyncKeyedLock

GitHub Workflow Status Nuget Nuget

An asynchronous .NET Standard 2.0 library that allows you to lock based on a key (keyed semaphores), only allowing a defined number of concurrent threads that share the same key.

For example, if you're processing transactions, you may want to limit to only one transaction per user so that the order is maintained, but meanwhile allowing parallel processing of multiple users.

Benchmarks

Tests show that AsyncKeyedLock is faster than similar libraries, while consuming less memory.

Installation

The recommended means is to use NuGet, but you could also download the source code from here.

Usage

You need to start off with creating an instance of AsyncKeyedLocker or AsyncKeyedLocker<T>. The recommended way is to use the latter, which consumes less memory. The former uses object and may be slightly faster, but at the expense of higher memory usage.

Dependency injection

services.AddSingleton<IAsyncKeyedLocker, AsyncKeyedLocker>();

or:

services.AddSingleton<IAsyncKeyedLocker<string>, AsyncKeyedLocker<string>>();

Variable instantiation

var asyncKeyedLocker = new AsyncKeyedLocker();

or:

var asyncKeyedLocker = new AsyncKeyedLocker<string>();

or if you would like to set the maximum number of requests for the semaphore that can be granted concurrently (set to 1 by default):

var asyncKeyedLocker = new AsyncKeyedLocker<string>(2);

Locking

using (var lockObj = await asyncKeyedLocker.LockAsync(myObject))
{
	...
}

There are other overloaded methods for LockAsync which allow you to use CancellationToken, milliseconds timeout, System.TimeSpan or a combination of these.

There are also synchronous Lock methods available.

If you would like to see how many concurrent requests there are for a semaphore for a given key:

int myRemainingCount = asyncKeyedLocker.GetRemainingCount(myObject);

If you would like to see the number of remaining threads that can enter the lock for a given key:

int myCurrentCount = asyncKeyedLocker.GetCurrentCount(myObject);

If you would like to check whether any request is using a specific key:

bool isInUse = asyncKeyedLocker.IsInUse(myObject);

And if for some reason you need to force release the requests in the semaphore for a key:

asyncKeyedLocker.ForceRelease(myObject);

Credits

This library was inspired by Stephen Cleary's solution.

Showing the top 20 packages that depend on AsyncKeyedLock.

Packages Downloads
Volo.Abp.DistributedLocking.Abstractions
Package Description
9
Volo.Abp.DistributedLocking.Abstractions
Package Description
4
Volo.Abp.DistributedLocking.Abstractions
Package Description
1

ForceRelease now releases everything in the semaphore before removing the key from the dictionary.

.NET Standard 2.0

  • No dependencies.

Version Downloads Last updated
7.0.2 0 10/13/2024
7.0.1 1 9/12/2024
7.0.0 0 7/21/2024
7.0.0-rc3 1 9/19/2024
7.0.0-rc2 0 7/20/2024
7.0.0-rc1 1 9/12/2024
7.0.0-beta 1 9/12/2024
7.0.0-alpha 1 9/19/2024
6.4.2 0 4/21/2024
6.4.1 1 8/29/2024
6.4.0 0 4/20/2024
6.3.4 2 8/30/2024
6.3.4-rc 2 8/29/2024
6.3.4-beta 0 1/23/2024
6.3.3 0 1/14/2024
6.3.2 0 1/14/2024
6.3.0 0 1/14/2024
6.2.6 1 9/12/2024
6.2.5 1 9/12/2024
6.2.4 1 9/12/2024
6.2.3 1 9/19/2024
6.2.3-beta 1 9/19/2024
6.2.2 2 11/16/2023
6.2.1 4 5/18/2023
6.2.0 2 6/29/2023
6.1.1 1 7/13/2023
6.1.1-rc 0 1/27/2023
6.1.1-beta 1 9/19/2024
6.1.0 3 6/29/2023
6.0.5 1 9/12/2024
6.0.5-alpha 0 12/30/2022
6.0.4 2 6/29/2023
6.0.4-rc6 3 6/29/2023
6.0.4-rc5 1 6/29/2023
6.0.4-rc3 0 12/30/2022
6.0.4-rc 2 6/29/2023
6.0.4-beta 1 6/29/2023
6.0.4-alpha 1 6/29/2023
6.0.3 1 6/29/2023
6.0.2 1 6/29/2023
6.0.1 1 4/15/2024
5.1.2 1 6/29/2023
5.1.1 1 9/12/2024
5.1.0 2 6/29/2023
5.0.4 1 6/29/2023
5.0.3 1 6/29/2023
5.0.3-rc 2 6/29/2023
5.0.2-rc 2 6/29/2023
5.0.1 1 6/29/2023
4.0.2 1 6/29/2023
3.2.3 0 10/26/2022
3.2.1 0 10/23/2022
3.2.0 1 6/29/2023
3.0.1 2 6/29/2023
3.0.0 1 6/29/2023
2.0.3 1 6/29/2023
2.0.2 1 6/29/2023
2.0.1 1 6/29/2023
2.0.0 2 6/29/2023
1.1.0 0 12/11/2021
1.0.1 1 6/29/2023
1.0.0 1 9/19/2024