问题描述:

I have an Azure Redis Cache and multiple clients are using it. Few of the shared resources (keys) in the cache needs to be mutually exclusive. I am using the redlock algorithm as proposed here.

My requirement is that each client needs to access the shared resources but in a mutually exclusive fashion. If a client is unable to acquire a lock on a resource then it should keep trying to acquire the lock, at an interval of 30 seconds, until it acquires it. To prevent deadlocks, in case a process crashes, a lock should expire after 5 mins.

I have checked the documentation of redock.net from github, but I am not sure how to use the retry, wait and expiry parameters correctly to achieve the above criteria. Can anyone guide me in that direction? Or is there any other detailed documentation available?

The code is written in .NET so I am using StackExchange.Redis and Redlock.net

网友答案:

You can use the following parameters to achieve what you want:

expiryTime: TimeSpan.FromMinutes(5)

This is how long RedLock.net will set the redis key expiry to, in case of e.g. a process crash. The redis key expiry will automatically be extended each time half of this time period passes if the process is still alive (2.5 minutes in this case). Because of the lock extension timer, you can set the expiry time shorter if you want another process to be able to take over sooner in the failure case.

waitTime: TimeSpan.MaxValue

This is how long RedLock.net will sit waiting to get the lock (in this case, effectively forever).

retryTime: TimeSpan.FromSeconds(30)

This is how long RedLock.net will wait between retries while it is blocked waiting for the lock.

Since RedLock.net 1.7.0 you can also provide a cancellation token if you want to be able to break out of waiting for a lock to be acquired.

相关阅读:
Top