AsyncKeyedLock 7.1.4

AsyncKeyedLock AsyncKeyedLock

GitHub Workflow Status NuGet NuGet Codacy Grade Codecov

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, with optional pooling for reducing memory allocations.

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.

The library uses two very different methods for locking, AsyncKeyedLocker which uses an underlying ConcurrentDictionary that's cleaned up after use and StripedAsyncKeyedLocker which uses a technique called striped locking. Both have their advantages and disadvantages, and in order to help you choose you are highly recommended to read about it in the wiki.

A simple non-keyed lock is also available through AsyncNonKeyedLocker.

Installation and usage

Using this library is straightforward. Here's a simple example for using AsyncKeyedLocker:

private static readonly AsyncKeyedLocker<string> _asyncKeyedLocker = new();

...

using (await _asyncKeyedLocker.LockAsync("test123"))
{
  ...
}

or with timeouts:

private static readonly AsyncKeyedLocker<string> _asyncKeyedLocker = new();
private const _timeout = 100;

...

using (var releaser = await _asyncKeyedLocker.LockOrNullAsync("test123", _timeout))
{
  if (releaser is not null)
  {
    ...
  }
}

This libary also supports conditional locking, whether for AsyncKeyedLocker, StripedAsyncKeyedLocker or AsyncNonKeyedLocker. This could provide a workaround for reentrancy in some scenarios for example in recursion:

double factorial = Factorial(number);

public static double Factorial(int number, bool isFirst = true)
{
  using (await _asyncKeyedLocker.ConditionalLockAsync("test123", isFirst))
  {
    if (number == 0)
      return 1;
    return number * Factorial(number-1, false);
  }
}

For more help with AsyncKeyedLocker, or for examples with StripedAsyncKeyedLocker or AsyncNonKeyedLocker (for simple, non-keyed locking), please take a look at our wiki.

Pooling change in v7.0.0

Prior to AsyncKeyedLock 7.0.0, pooling was disabled by default and you needed to specify a pool size in order to enable it. Since v7.0.0, pooling is enabled by default with an initial fill of 1 and a pool size of 20. In order to disable pooling (not recommended), you need to now specify a pool size of 0 within AsyncKeyedLockOptions. Read more about pooling in our wiki.

Benchmarks

This library has been extensively benchmarked against several other options and our benchmarks run publicly and transparently on Github Actions.

Credits

Check out our list of contributors!

Showing the top 20 packages that depend on AsyncKeyedLock.

Packages Downloads
EasyCaching.Core
EasyCaching is a open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
6
EasyCaching.Core
EasyCaching is a open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
7
Volo.Abp.DistributedLocking.Abstractions
Package Description
6
Volo.Abp.DistributedLocking.Abstractions
Package Description
7
Volo.Abp.DistributedLocking.Abstractions
Package Description
8
Volo.Abp.DistributedLocking.Abstractions
Package Description
9

Dependency update and nullable changes.

.NET 5.0

.NET 6.0

.NET 7.0

.NET 8.0

.NET 9.0

  • No dependencies.

.NET Standard 2.0

.NET Standard 2.1

Version Downloads Last updated
7.1.4 5 12/02/2024
7.1.4-preview 3 11/30/2024
7.1.3 3 11/19/2024
7.0.2 4 11/25/2024
7.0.1 5 11/28/2024
7.0.0 7 11/21/2024
7.0.0-rc3 3 11/25/2024
7.0.0-rc2 5 11/28/2024
7.0.0-rc1 3 11/30/2024
7.0.0-beta 0 06/29/2024
7.0.0-alpha 4 11/25/2024
6.4.2 5 05/29/2024
6.4.1 7 05/29/2024
6.4.0 7 05/29/2024
6.3.4 10 05/29/2024
6.3.4-rc 7 05/29/2024
6.3.4-beta 7 05/31/2024
6.3.3 6 05/29/2024
6.3.2 4 05/29/2024
6.3.0 11 05/29/2024
6.2.6 6 05/29/2024
6.2.5 8 05/29/2024
6.2.4 9 05/29/2024
6.2.3 7 05/29/2024
6.2.3-beta 7 05/31/2024
6.2.2 6 05/29/2024
6.2.1 9 05/29/2024
6.2.0 7 05/29/2024
6.1.1 6 05/29/2024
6.1.1-rc 8 05/29/2024
6.1.1-beta 9 05/31/2024
6.1.0 9 05/29/2024
6.0.5 8 05/29/2024
6.0.5-alpha 6 05/29/2024
6.0.4 6 05/29/2024
6.0.4-rc6 3 05/29/2024
6.0.4-rc5 7 05/29/2024
6.0.4-rc3 8 05/29/2024
6.0.4-rc 5 05/29/2024
6.0.4-beta 7 05/31/2024
6.0.4-alpha 6 05/29/2024
6.0.3 5 11/28/2024
6.0.2 6 05/29/2024
6.0.1 8 05/29/2024
5.1.2 5 05/29/2024
5.1.1 6 05/29/2024
5.1.0 7 05/29/2024
5.0.4 9 05/29/2024
5.0.3 5 05/29/2024
5.0.3-rc 8 05/29/2024
5.0.2-rc 4 05/29/2024
5.0.1 3 11/27/2024
4.0.2 6 05/29/2024
3.2.3 5 05/29/2024
3.2.1 8 05/29/2024
3.2.0 6 05/29/2024
3.0.1 9 05/29/2024
3.0.0 9 05/29/2024
2.0.3 7 05/29/2024
2.0.2 5 05/29/2024
2.0.1 7 05/29/2024
2.0.0 5 05/29/2024
1.1.0 3 05/29/2024
1.0.1 10 05/29/2024
1.0.0 6 05/29/2024