AsyncKeyedLock 6.0.5-alpha
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.
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):
// using AsyncKeyedLockOptions
var asyncKeyedLocker1 = new AsyncKeyedLocker<string>(new AsyncKeyedLockOptions(maxCount: 2));
// using Action<AsyncKeyedLockOptions>
var asyncKeyedLocker2 = new AsyncKeyedLocker<string>(o => o.MaxCount = 2);
There are also AsyncKeyedLocker
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:
// using AsyncKeyedLockOptions
var asyncKeyedLocker1 = new AsyncKeyedLocker<string>(new AsyncKeyedLockOptions(poolSize: 100));
// using Action<AsyncKeyedLockOptions>
var asyncKeyedLocker2 = new AsyncKeyedLocker<string>(o => o.PoolSize = 100);
You can also set the initial pool fill (by default this is set to the pool size):
// using AsyncKeyedLockOptions
var asyncKeyedLocker = new AsyncKeyedLocker<string>(new AsyncKeyedLockOptions(poolSize: 100, poolInitialFill: 50));
// using Action<AsyncKeyedLockOptions>
var asyncKeyedLocker = new AsyncKeyedLocker<string>(o =>
{
o.PoolSize = 100;
o.PoolInitialFill = 50;
});
Locking
// without cancellation token
using (await asyncKeyedLocker.LockAsync(myObject))
{
...
}
// with cancellation token
using (await asyncKeyedLocker.LockAsync(myObject, cancellationToken))
{
...
}
You can also use timeouts with overloaded methods to set the maximum time to wait, either in milliseconds or as a TimeSpan
.
In the case you need to use timeouts to instead give up if unable to obtain a lock by a certain amount of time, 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
and TryLock
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);
Credits
This library was originally inspired by Stephen Cleary's solution, but has gone through a lot of changes since.
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 |
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 |