
Security News
Astral Launches pyx: A Python-Native Package Registry
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
This gem implements a pessimistic lock using Redis. It correctly handles timeouts and vanishing lock owners (such as machine failures)
This uses setnx, but not the setnx algorithm described in the redis cookbook, which is not robust.
Add this line to your application's Gemfile:
gem 'mlanett-redis-lock', require: 'redis-lock'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mlanett-redis-lock
A lock needs an expected lifetime. If the owner of a lock disappears (due to machine failure, network failure, process death), you want the lock to expire and another owner to be able to acquire the lock. At the same time, the owner of a lock should be able to extend its lifetime. Thus, you can acquire a lock with a conservative estimate on lifetime, and extend it as necessary, rather than acquiring the lock with a very long lifetime which will result in long waits in the event of failures.
A lock needs an owner. Redis::Lock defaults to using an owner id of HOSTNAME:PID.
A lock may need more than one attempt to acquire it. Redis::Lock offers an acquisition timeout; this defaults to 10 seconds.
There are two lock methods: Redis#lock, which is more convenient, and Redis::Lock#lock.
Notice there are two timeouts: the lock's lifetime (:life
option) and the acquisition timeout, which is less important.
The acquisition timeout is set via the :acquire option to Redis#lock or passed directly to Redis::Lock#lock.
This gem adds lock()
and unlock()
to Redis instances.
lock()
takes a block and is safer than using lock()
and unlock()
separately.
lock()
takes a key and lifetime and optionally an acquisition timeout (defaulting to 10 seconds).
redis.lock("test") { |lock| do_something }
redis.lock("test", life: 2*60, acquire: 2) do |lock|
array.each do |entry|
do_something(entry)
lock.extend_life(60)
end
end
I wrote this when noticing that other lock gems were using non-robust approaches.
You need to be able to handle race conditions while acquiring the lock. You need to be able to handle the owner of the lock failing to release it. You need to be able to detect stale locks. You need to handle race conditions while cleaning the stale lock and acquiring a new one. The code which cleans the stale lock may not be able to assume it gets the new one. The code which cleans the stale lock must not interfere with a different owner acquiring the lock.
Alexander Lang (langalex), Jonathan Hyman (jonhyman), Jamie Cobbett (jamiecobbett), Ravil Bayramgalin (brainopia), and Tom Mornini (tmornini) have contributed to Redis Lock.
git checkout -b my-new-feature
)git commit -am 'Added some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that mlanett-redis-lock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.