AsyncKeyedLock 4.0.2

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), limiting concurrent threads sharing the same key to a specified number.

For example, suppose you were processing financial transactions, but while working on one account you wouldn't want to concurrently process a transaction for the same account. Of course, you could just add a normal lock, but then you can only process one transaction at a time. If you're processing a transaction for account A, you may want to also be processing a separate transaction for account B. That's where AsyncKeyedLock comes in: it allows you to lock but only if the key matches.

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 is faster and consumes less memory. The former uses object and can be used to mix different types of objects.

Dependency injection

services.AddSingleton<AsyncKeyedLocker>();

or (recommended):

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

Variable instantiation

var asyncKeyedLocker = new AsyncKeyedLocker();

or (recommended):

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);

There are also AsyncKeyedLocker() constructors which accept the parameters of ConcurrentDictionary, namely the concurrency level, the capacity and the IEqualityComparer to use.

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. In the case of timeouts, you can also use TryLockAsync methods which will call a Func<Task> or Action if the timeout is not expired, whilst returning a boolean representing whether or not it waited successfully.

There are also synchronous Lock methods available, including out parameters for checking whether or not the timeout was reached.

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);

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

Performance improvements and also allowing for concurrency level, capacity and an IEqualityComparer to be used.

.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