Coder Social home page Coder Social logo

redlock-php's Introduction

RedLock – Redis distributed locks in PHP

Based on Redlock-rb by Salvatore Sanfilippo

This library implements the Redis-based distributed lock manager algorithm described in this blog post.

To create a lock manager:

$servers = [
    ['127.0.0.1', 6379, 0.01],
    ['127.0.0.1', 6389, 0.01],
    ['127.0.0.1', 6399, 0.01],
];

$redLock = new RedLock($servers);

To acquire a lock:

$lock = $redLock->lock('my_resource_name', 1000);

Where the resource name is an unique identifier of what you are trying to lock and 1000 is the number of milliseconds for the validity time.

The returned value is false if the lock was not acquired (you may try again), otherwise an array representing the lock is returned, having three keys:

Array
(
    [validity] => 9897.3020019531
    [resource] => my_resource_name
    [token] => 53771bfa1e775
)
  • validity, an integer representing the number of milliseconds the lock will be valid.
  • resource, the name of the locked resource as specified by the user.
  • token, a random token value which is used to safe reclaim the lock.

To release a lock:

    $redLock->unlock($lock)

It is possible to setup the number of retries (by default 3) and the retry delay (by default 200 milliseconds) used to acquire the lock.

The retry delay is actually chosen at random between $retryDelay / 2 milliseconds and the specified $retryDelay value.

Disclaimer: As stated in the original antirez's version, 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 your production environments.

redlock-php's People

Contributors

joni2back avatar ronnylt avatar sp-ronny-lopez avatar zeroasterisk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

redlock-php's Issues

Can't PHP version 7 use this package?

My PHP version is 7.2.10, and I installed the package to report an error

[InvalidArgumentException]                                                                                 
  Package ronnylt/redlock-php at version  has a PHP requirement incompatible with your PHP version (7.2.10) 

Please release

Please tag a version so that we could reference this library.

License?

Hi @ronnylt!

What license is this code released under? Would you consider Apache, MIT, or LGPL so that it can be used in commercial software products?

Thanks,
Mike

NOAUTH Authentication required

            list($host, $port, $timeout,$auth) = $server;
            $redis = new \Redis();
            $redis->connect($host, $port, $timeout);

            if ($auth) {
                $redis->auth($auth);
            }

Your composer.json requires phpredis 2.2.5, but your code requires >= 2.6.12

Hi,
Your composer.json only requires phpredis 2.2.5:
https://github.com/ronnylt/redlock-php/blob/master/composer.json#L12

Your actual code requires phpredis >= 2.6.12:
https://github.com/ronnylt/redlock-php/blob/master/src/RedLock.php#L99

For details, see this:
phpredis/phpredis@711f053

When I run your test with phpredis 2.2.5, I get a Segmentation Fault.

I would recommend you update your composer.json to require phpredis >= 2.6.12

Fatal error: Uncaught exception 'RedisException' with message 'read error on connection'

when i test the redlock file, the program terminates with following fatal error:

Fatal error: Uncaught exception 'RedisException' with message 'read error on connection' in C:\OpenSource\xampp\htdocs\demo\vendor\ronnylt\redlock-php\src\RedLock.php on line 99
RedisException: read error on connection in C:\OpenSource\xampp\htdocs\demo\vendor\ronnylt\redlock-php\src\RedLock.php on line 99

The test code is as follow:

    $servers = [
        ['127.0.0.1', 6379, 0.01]
    ];

    $redLock = new RedLock($servers);
    $i = 0;
    while ($i<100) {
        echo $i."\r\n";
        $lock = $redLock->lock('newOrder', 1000);

        if ($lock) {
            var_dump($lock);
        } else {
            echo "Lock not acquired\r\n";
        }
        $i++;
    }

Sometimes it runs without error, however, it terminates with error most of the time, what's the problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.