kenn / redis-mutex

Distributed mutex using Redis
https://rubygems.org/gems/redis-mutex
MIT License
282 stars 44 forks source link

Retry after failed until acquire lock #21

Closed acegilz closed 8 years ago

acegilz commented 8 years ago

I was wondering what should be the best way to keep trying until we ca get a lock

mutex = RedisMutex.new(key)

if mutex.lock

  if exposure.valid?
    ###CODE
  end
  mutex.unlock

else

  ###KEEP RETRYNG UNTIL GET A LOCK

end
kenn commented 8 years ago

From README:

:block  => 1    # Specify in seconds how long you want to wait for the lock to be released.
                # Specify 0 if you need non-blocking sematics and return false immediately. (default: 1)
:sleep  => 0.1  # Specify in seconds how long the polling interval should be when :block is given.
                # It is NOT recommended to go below 0.01. (default: 0.1)
:expire => 10   # Specify in seconds when the lock should be considered stale when something went wrong
                # with the one who held the lock and failed to unlock. (default: 10)

Try parameters in this way:

mutex = RedisMutex.new(key, block: 6.hours, expire: 6.hours)

Keep in mind that it will retry 10 times per second, as sleep is set at 0.1 by default.