14 Jun 2014 14:03:27 UTC
- Distribution: Redis-DistLock
- Module version: 0.04
- Source (raw)
- Browse (raw)
- How to Contribute
- Clone repository
- Testers (281 / 0 / 0)
- KwaliteeBus factor: 0
- License: unknown
- Activity24 month
- Download (6.33KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version
- SEE ALSO
- COPYRIGHT AND LICENSE
Redis::DistLock - Distributed lock manager using Redis
use Redis::DistLock; my $rd = Redis::DistLock->new( servers => [qw[ localhost:6379 ]] ); my $mutex = $rd->lock( "foo", 10 ); die( "failed to get a lock" ) if ! $mutex; # ... critical section ... $rd->release( $mutex );
This is an implementation of the Redlock algorithm using Redis for distributed lock management. It enables lightweight distributed locks in order to prevent cronjob overruns, help with queue processing, many workers of which only one should run at a time, and similar situations.
NOTE: This needs at least Redis version 2.6.12 which adds new options to the
SETcommand making this implementation possible.
Takes a hash or hash reference with below arguments and returns a lock manager instance. Since this module currently does not repair initially failed connections it checks for the majority of connections or
Array reference with servers to connect to or Redis objects to use.
Maximum number of times to try to acquire the lock. Defaults to
Maximum delay between retries in seconds. Defaults to
Flag to check redis server version(s) in the constructor to ensure compatibility. Defaults to
Optional subroutine that will be called with errors as parameter, should any occur. By default, errors are currently just warnings. To disable pass
Flag to enable automatic release of all locks when the lock manager instance goes out of scope. Defaults to
CAVEAT: Ctrl-C'ing a running Perl script does not call DESTROY(). This means you will have to wait for Redis to expire your locks for you if the script is killed manually. Even if you do implement a signal handler, it can be quite unreliable in Perl and does not guarantee the timeliness of your locks being released.
Acquire the lock for the resource with the given time to live (in seconds) until the lock expires. Without a value generates a 32 character base64 string based on 24 random input bytes.
Same as lock() but with a known value instead of a random string.
Release the previously acquired lock.
Version of release() that allows to maintain state solely in Redis when the value is known, e.g. a hostname.
This code implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in production environments.
This module was originally developed at Booking.com. With approval from Booking.com, this module was released as open source, for which the authors would like to express their gratitude.
Simon Bertrang, <email@example.com>
Ryan Bastic, <firstname.lastname@example.org>
Copyright (C) 2014 by Simon Bertrang
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Module Install Instructions
To install Redis::DistLock, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install Redis::DistLock
For more information on module installation, please visit the detailed CPAN module installation guide.