Acquire and keep distributed locks alive using redis
There are already quite some ruby libraries available which use redis for the purpose of distributed locking, but they require you to specify the time time-to-live of your locks. Contrary, Redlocker allows you to easily acquire and keep distributed locks alive using redis. An acquired lock gets automatically renewed every second from a thread, i.e. its 5 second expiry value gets renewed in redis every second, and it gets released as soon as the given block finishes.
Add this line to your application's Gemfile:
And then execute:
$ bundle install
Or install it yourself as:
$ gem install redlocker
Using Redlocker could not be easier:
RedlockerClient = ::.(redis: Redis.new) RedlockerClient.with_lock('some_lock', timeout: 5) do # lock acquired end
If the lock can not be acquired within the specified
Redlocker::TimeoutError is raised.
When the block finishes or raises, the acquired lock gets freed.
You can optionally pass a
delay when acquiring a lock, which specifies the
time to wait between subsequent calls which check in redis whether or not the
lock is free. Default is 0.25 seconds:
RedlockerClient.with_lock("some lock", timeout: 5, delay: 1) do # lock acquired end
If you are using a shared redis, you can pass a namespace, which will be used for
prefixing redis keys in addition to the default
RedlockerClient = ::.(redis: Redis.new, namespace: "my-namespace")
Please find the reference docs at http://www.rubydoc.info/github/mrkamel/redlocker
After checking out the repo, run
bin/setup to install dependencies. Then, run
bundle exec rspec to run the tests. You can also run
bin/console for an
interactive prompt that will allow you to experiment.
Bug reports and pull requests are welcome on GitHub at https://github.com/mrkamel/redlocker.
The gem is available as open source under the terms of the MIT License.