You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a requirement to implement a distributed lock for a fixed number of clients, so the lock should allow for say 5 clients to acquire the lock, but the sixth client lock acquisition attempt should fail. Any suggestions for implementing this with this library? Thanks!
The text was updated successfully, but these errors were encountered:
Hi @alastairtree thanks for your interest in the library.
It sounds like what you are really looking for is a distributed semaphore. You may be interested in the discussion on this topic here: #7
In short, there is no automated way to do this with the current library although I am very interested in building one in if we can find a robust and performant implementation. Since you need only 5 concurrent clients, you could likely get away with simply looping over a set of 5 locks until one of them works:
class SqlDistributedSemaphore {
private readonly IReadOnlyList<SqlDistributedLock> _locks;
public SqlDistributedSemaphore(string lockName, int maxCount)
{
this._locks = Enumerable.Range(0, maxCount).Select(i => new SqlDistributedLock(..., lockName: lockName + maxCount)).ToArray();
}
public IDisposable Acquire()
{
Random random = null;
while (true)
{
for (var i = 0; i < this._locks.Count; ++i)
{
var handle = this._locks[i].TryAcquire();
if (handle != null) { return handle; }
}
Thread.Sleep((random ?? random = new Random()).Next(50));
}
}
}
I have a requirement to implement a distributed lock for a fixed number of clients, so the lock should allow for say 5 clients to acquire the lock, but the sixth client lock acquisition attempt should fail. Any suggestions for implementing this with this library? Thanks!
The text was updated successfully, but these errors were encountered: