Handle stale locks with stale_client_timeout
masterTo prevent dead clients from holding locks forever, you can use a stale-check mechanism. If a lock is older than the specified timeout, it can be considered stale.
Option 1: Automatic check on lock
Pass :stale_client_timeout (in seconds) during initialization. The semaphore will check for stale locks every time you attempt to call lock.
Option 2: Watchdog thread
Run a separate thread that calls release_stale_locks! frequently. This is more effective as it can unblock processes currently waiting in a lock call.
# Option 1: Automatic check
s = Redis::Semaphore.new(:stale_semaphore, :stale_client_timeout => 5)
# Option 2: Watchdog thread
Thread.new do
watchdog = Redis::Semaphore.new(:semaphore, :stale_client_timeout => 5)
while(true)
watchdog.release_stale_locks!
sleep 1
end
end