AsyncKeyedLock 7.1.4
AsyncKeyedLock
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 |
---|---|
Volo.Abp.DistributedLocking.Abstractions
Package Description
|
17 |
Volo.Abp.DistributedLocking.Abstractions
Package Description
|
16 |
Volo.Abp.DistributedLocking.Abstractions
Package Description
|
15 |
Volo.Abp.DistributedLocking.Abstractions
Package Description
|
14 |
.NET 5.0
- System.Threading.Tasks.Extensions (>= 4.6.0)
.NET 6.0
- System.Threading.Tasks.Extensions (>= 4.6.0)
.NET 7.0
- System.Threading.Tasks.Extensions (>= 4.6.0)
.NET 8.0
- System.Threading.Tasks.Extensions (>= 4.6.0)
.NET 9.0
- No dependencies.
.NET Standard 2.0
- System.Threading.Tasks.Extensions (>= 4.6.0)
.NET Standard 2.1
- System.Threading.Tasks.Extensions (>= 4.6.0)
Version | Downloads | Last updated |
---|---|---|
7.1.4 | 3 | 11/29/2024 |
7.1.4-preview | 3 | 12/01/2024 |
7.1.3 | 5 | 11/14/2024 |
7.0.2 | 8 | 10/18/2024 |
7.0.1 | 11 | 08/21/2024 |
7.0.0 | 10 | 07/24/2024 |
7.0.0-rc3 | 10 | 07/23/2024 |
7.0.0-rc2 | 9 | 07/23/2024 |
7.0.0-rc1 | 10 | 06/30/2024 |
7.0.0-beta | 9 | 06/30/2024 |
7.0.0-alpha | 9 | 06/30/2024 |
6.4.2 | 12 | 04/26/2024 |
6.4.1 | 12 | 05/01/2024 |
6.4.0 | 11 | 05/10/2024 |
6.3.4 | 20 | 02/01/2024 |
6.3.4-rc | 15 | 01/31/2024 |
6.3.4-beta | 12 | 02/01/2024 |
6.3.3 | 12 | 01/17/2024 |
6.3.2 | 13 | 01/17/2024 |
6.3.0 | 12 | 01/17/2024 |
6.2.6 | 18 | 01/13/2024 |
6.2.5 | 16 | 01/08/2024 |
6.2.5-alpha | 16 | 01/03/2024 |
6.2.4 | 14 | 12/20/2023 |
6.2.3 | 15 | 12/15/2023 |
6.2.3-beta | 16 | 12/19/2023 |
6.2.2 | 17 | 10/19/2023 |
6.2.1 | 28 | 06/02/2023 |
6.2.0 | 17 | 07/01/2023 |
6.1.1 | 16 | 07/02/2023 |
6.1.1-rc | 21 | 10/18/2023 |
6.1.1-beta | 16 | 06/20/2023 |
6.1.0 | 17 | 07/01/2023 |
6.0.5 | 16 | 07/02/2023 |
6.0.5-alpha | 17 | 06/19/2023 |
6.0.4 | 17 | 06/19/2023 |
6.0.4-rc6 | 17 | 06/30/2023 |
6.0.4-rc5 | 20 | 06/20/2023 |
6.0.4-rc3 | 17 | 06/19/2023 |
6.0.4-rc | 18 | 06/19/2023 |
6.0.4-beta | 14 | 07/02/2023 |
6.0.4-alpha | 19 | 07/02/2023 |
6.0.3 | 18 | 06/21/2023 |
6.0.2 | 18 | 07/02/2023 |
6.0.1 | 18 | 06/19/2023 |
5.1.2 | 14 | 06/20/2023 |
5.1.1 | 18 | 06/30/2023 |
5.1.0 | 17 | 07/19/2023 |
5.0.4 | 13 | 06/20/2023 |
5.0.3 | 20 | 07/14/2023 |
5.0.3-rc | 16 | 06/30/2023 |
5.0.2-rc | 17 | 06/21/2023 |
5.0.1 | 19 | 06/30/2023 |
4.0.2 | 20 | 07/01/2023 |
3.2.3 | 18 | 08/05/2023 |
3.2.1 | 14 | 06/20/2023 |
3.2.0 | 19 | 06/20/2023 |
3.0.1 | 17 | 06/19/2023 |
3.0.0 | 17 | 07/02/2023 |
2.0.3 | 16 | 06/19/2023 |
2.0.2 | 16 | 07/02/2023 |
2.0.1 | 14 | 06/19/2023 |
2.0.0 | 18 | 06/19/2023 |
1.1.0 | 16 | 06/19/2023 |
1.0.1 | 17 | 07/01/2023 |
1.0.0 | 19 | 06/20/2023 |