A distributed semaphore based on Redis.
connectionMultiplexer
- required, Inter-related group of connections to redis serversredisDatabaseId
optional, The ID of the database (default -1).
var connection = ConnectionMultiplexer.Connect("127.0.0.1:6379");
using (var semaphoreFactory = DSemaphoreFactory.Create(connection))
{
// ...
}
semaphoreName
- required, Name of the semaphoremaxCount
- required, The number of requests for the semaphore that can be granted concurrentlyretryTime
optional, How long to wait between retries when trying to acquire a lock. (default 10ms and it cannot be less than this value)
int maxCount = 5;
await using (var semaphore = semaphoreFactory.CreateSemaphore("foo", maxCount))
{
foreach (var entity in collection)
{
// ...
}
}
timeout
- required, observation time interval, after the delay has passed the method returns false. This value is important to prevent dead acquisition requestsid
- optional, Id of the semaphore contextcancellationToken
optional,
var timeout = TimeSpan.FromSeconds(30);
if (await semaphore.WaitAsync(timeout))
{
// an action ...
}
This project is licensed under the MIT License - see the LICENSE.md file for details