AsyncKeyedLock 5.0.1

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>(new AsyncKeyedLockOptions(maxCount: 2));

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

Pooling

Whenever a lock needs to be acquired for a key that is not currently being processed, an AsyncKeyedLockReleaser object needs to exist for that key and added to a ConcurrentDictionary. In order to reduce allocations having to create objects only to dispose of them shortly after, AsyncKeyedLock allows for object pooling. Whenever a new key is needed, it is taken from the pool (rather than created from scratch). If the pool is empty, a new object is created. This means that the pool will not throttle or limit the number of keys being concurrently processed. Once a key is no longer in use, the AsyncKeyedLockReleaser object is returned back to the pool, unless the pool is already full up.

Usage of the pool can lead to big performance gains, but it can also very easily lead to inferior performance. If the pool is too small, the benefit from using the pool might be outweighed by the extra overhead from the pool itself. If, on the other hand, the pool is too big, then that's a number of objects in memory for nothing, consuming memory.

It is recommended to run benchmarks and tests if you intend on using pooling to make sure that you choose an optimal pool size.

Setting the pool size can be done via the AsyncKeyedLockOptions in one of the overloaded constructors, such as this:

var asyncKeyedLocker = new AsyncKeyedLocker<string>(new AsyncKeyedLockOptions(poolSize: 100));

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 originally 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 allowing for object pooling.

.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