2.0.1 • Published 8 years ago

dist-lock v2.0.1

Weekly downloads
15
License
MIT
Repository
github
Last release
8 years ago

dist-lock

dist-lock is a distributed lock manager that uses a single Redis instance.

A distributed lock manager (DLM) provides distributed software applications with a means to synchronize their accesses to shared resources.

It uses the lock acquisition algorithm described by Redis' author here and requires Redis v2.6.12 or above.

Features

  • Lock acquisition is non-blocking and asynchronous.
  • Option to wait indefinitely for a lock, or timeout after a configurable duration.
  • To avoid potential deadlocks, locks will automatically release after a configurable duration.

Requirements

Installation

npm install dist-lock

Example

var redis = require('redis').createClient();
var distLock = require('dist-lock')(redis);

// Acquire a lock
distLock.acquire('shared-resource-name', 'owner-name', function(err, lock) {
  if (err) {
    // Something went wrong. Is Redis down?
    throw err;
  }
  
  // We have acquired a lock for "shared-resource-name" and identified ourselves (the lock acquirer) as
  // "owner-name". No other process will be able to acquire the lock while we hold it.
  
  // Do some work...
  
  // Work is done, so release the lock
  lock.release();
});

Usage

Initialization

Create a distributed lock manager by passing in a Redis client instance:

var distLock = require('dist-lock')(redis);

You can override the default configuration by passing in options:

var options = {
  ttl: 30000,        // Time in milliseconds after which the lock will automatically release
  retryDelay: 50,    // Time in milliseconds to wait between lock acquisition attempts
  maxRetries: -1,    // Maximum number of times lock acquisition will be attempted
  keyPrefix: "lock:" // String prefix to use for lock strings in Redis
};
var distLock = require('dist-lock')(redis, options);

Default configuration is:

  • ttl: 30000ms
  • retryDelay: 50ms
  • maxRetries: -1 (unlimited)
  • keyPrefix: "lock:"

Acquiring a lock

Call the acquire() function to acquire a lock:

.acquire(resourceName, callback)

Parameters:

NameTypeDescription
resourceNamestringUnique name of the shared resource being locked
ownerstringA string to identify who or which process acquired the lock (optional)
callbackFunctionFunction called when the lock is acquired, an error occurs, or the maximum number of lock acquisition attempts has been reached

On success, the callback function will be called with a new lock instance.

If you configure the lock manager to try a limited number of times to acquire the lock and the lock could not be acquired then lock will be false.

Lock instance

The lock instance will have the following properties and methods:

NameTypeDescription
idstringThe unique ID of the lock
keystringThe key of the lock in Redis
ownerstringThe name of the lock acquirer passed to the acquire() function, or an empty string
acquiredOnAttemptintThe number of attempts made to acquire the lock
acquireDelayintThe duration in milliseconds it took to acquire the lock
release(callback)FunctionCall this function to release the lock
extend(milliseconds, callback)FunctionCall this function to extend the ttl of the lock. The lock's TTL will be reset to milliseconds milliseconds.

Releasing a lock

Call an acquired lock's release() function to release the lock:

lock.release()

Alternatively, call distLock.releaseLock(resourceName, id, callback) to release a lock:

distLock.releaseLock(resourceName, id, callback)

Parameters:

NameTypeDescription
resourceNamestringUnique name of the shared resource that is locked
idstringUnique ID of the lock to be released
callbackFunctionFunction called when the lock is released or an error occurs

On success, the callback function will be called with true (the lock was released) or false (the lock does not exist).

Extending a lock

Call an acquired lock's extend() function to extend the TTL of the lock:

lock.extend(milliseconds)

Alternatively, call distLock.extendLock(resourceName, id, milliseconds, callback) to extend a lock:

distLock.extendLock(resourceName, id, milliseconds, callback)

Parameters:

NameTypeDescription
resourceNamestringUnique name of the shared resource that is locked
idstringUnique ID of the lock to be extended
millisecondsintThe lock's TTL will be reset to this many milliseconds
callbackFunctionFunction called when the lock is extended or an error occurs

On success, the callback function will be called with true (the lock was extended) or false (the lock does not exist).

Getting the details of an acquired lock

distLock.getAcquiredLock(resourceName, callback)

Parameters:

NameTypeDescription
resourceNamestringUnique name of the shared resource that is locked
callbackFunctionFunction called with the lock's details or an error

On success, the callback function will be called with a lock or with null if the lock does not exist.

License

MIT

2.0.1

8 years ago

2.0.0

8 years ago

2.0.0-beta2

8 years ago

2.0.0-beta1

8 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago