<commit_before># multiredlock [![Build Status](https://travis-ci.org/lakka/redlock-nodejs.svg?branch=master)](https://travis-ci.org/lakka/redlock-nodejs)
A distributed lock algorithm for redis, see http://redis.io/topics/distlock

This is experimental software, use with your own risk.

## Install
`npm install multiredlock`

## Usage
Simple example, which can be found in `example.js`:

```js
    var Redlock = require('multiredlock');
    
    var redlock = new Redlock([{host:'localhost', port:6379}])
    
    redlock.on('connect', function() { 
      // Let's lock resource 'console' for 10 seconds!
      redlock.lock('console', 10000, function(err, lock) {
        if(err) {
          console.log(err);
          return;
        }
    
        // Do some stuff with the resource...
        console.log(lock);
    
        // Release lock when you're done with the resource
        redlock.unlock('console', lock.value);
      });
    });
```

Will output something like:

    $ node example.js 
    { validity: 9899,
      resource: 'console',
      value: 'yourhost_1421844817874_6f554e61' }

Note: you must have redis-server running on localhost at port 6379.

## API

### Events
- `connect`: emitted when multiredlock has connected to at least `N/2 + 1` redis-servers, where `N` is the total amount of servers supplied to multiredlock. In other words, `connect` will be emitted when multiredlock is able to acquire locks.
- `disconnect`: emitted when multiredlock is connected to less than `N/2 + 1` redis-servers, meaning that it will not be able to acquire locks. However, unlocking currently locked resources is possible.

### Variables
- `connected`: `true`, when connected to at least `N/2 + 1` redis-servers, `false` otherwise.

### Constructor
The constructor takes two arguments, an array `servers` and an object `options`:
- new Redlock() = new Redlock([{host:'localhost', port:6379}]);
- new Redlock([{host:'192.168.0.100', port:6380}, {host:'example.com', port:6379}, {host:'localhost', port:6381}], options); will connect to three redis-servers residing at `192.168.0.100`, `example.com` and `localhost`.

One may omit the `port`-property from the server object. Multiredlock will then will assume port number `6379`.

The object `options` may have the following properties:
- `id`: an optional ID to give for this instance of multiredlock. Is used in identifying locks. Defaults to the machine's hostname.
- `debug`: run multiredlock in debug mode.

### setRetry(retries, retryWait)
Sets the amount of retries and the maximum time to wait between retries when acquiring a lock. The actual time to retry will be chosen randomly between 0 and `retryWait` milliseconds before each retry. The default is 3 retries with 100ms maximum wait. This means that multiredlock will try to acquire a lock a total of 4 times before giving up. Setting `retries` to 0 will disable retrying.

### lock(resource, ttl, callback)<commit_msg>Added drift option<commit_after># multiredlock [![Build Status](https://travis-ci.org/lakka/redlock-nodejs.svg?branch=master)](https://travis-ci.org/lakka/redlock-nodejs)
A distributed lock algorithm for redis, see http://redis.io/topics/distlock

This is experimental software, use with your own risk.

## Install
`npm install multiredlock`

## Usage
Simple example, which can be found in `example.js`:

```js
    var Redlock = require('multiredlock');
    
    var redlock = new Redlock([{host:'localhost', port:6379}])
    
    redlock.on('connect', function() { 
      // Let's lock resource 'console' for 10 seconds!
      redlock.lock('console', 10000, function(err, lock) {
        if(err) {
          console.log(err);
          return;
        }
    
        // Do some stuff with the resource...
        console.log(lock);
    
        // Release lock when you're done with the resource
        redlock.unlock('console', lock.value);
      });
    });
```

Will output something like:

    $ node example.js 
    { validity: 9899,
      resource: 'console',
      value: 'yourhost_1421844817874_6f554e61' }

Note: you must have redis-server running on localhost at port 6379.

## API

### Events
- `connect`: emitted when multiredlock has connected to at least `N/2 + 1` redis-servers, where `N` is the total amount of servers supplied to multiredlock. In other words, `connect` will be emitted when multiredlock is able to acquire locks.
- `disconnect`: emitted when multiredlock is connected to less than `N/2 + 1` redis-servers, meaning that it will not be able to acquire locks. However, unlocking currently locked resources is possible.

### Variables
- `connected`: `true`, when connected to at least `N/2 + 1` redis-servers, `false` otherwise.

### Constructor
The constructor takes two arguments, an array `servers` and an object `options`:
- new Redlock() = new Redlock([{host:'localhost', port:6379}]);
- new Redlock([{host:'192.168.0.100', port:6380}, {host:'example.com', port:6379}, {host:'localhost', port:6381}], options); will connect to three redis-servers residing at `192.168.0.100`, `example.com` and `localhost`.

One may omit the `port`-property from the server object. Multiredlock will then will assume port number `6379`.

The object `options` may have the following properties:
- `id`: an optional ID string to give for this instance of multiredlock. Is used in identifying locks. Defaults to the machine's hostname.
- `drift`: set the maximum clock drift between servers in milliseconds. This will be subtracted from lock validity time. The default is 100ms.
- `debug`: run multiredlock in debug mode. Boolean.

### setRetry(retries, retryWait)
Sets the amount of retries and the maximum time to wait between retries when acquiring a lock. The actual time to retry will be chosen randomly between 0 and `retryWait` milliseconds before each retry. The default is 3 retries with 100ms maximum wait. This means that multiredlock will try to acquire a lock a total of 4 times before giving up. Setting `retries` to 0 will disable retrying.

### lock(resource, ttl, callback)
{'fraction_non_alphanumeric': 0.10809917355371901, 'fraction_numerical': 0.030578512396694214, 'mean_word_length': 3.7985725614591592, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3534749', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1713, 'n_words': 709}