|
1 |
| -# microlock |
2 |
| -Simple distributed locking with Etcd for Node.js |
| 1 | +# Microlock |
| 2 | +A dead simple distributed locking library for Node.js and [etcd](http://github.com/coreos/etcd) (via [node-etcd](https://github.com/stianeikeland/node-etcd)) |
| 3 | + |
| 4 | +[](https://nodei.co/npm/microlock/) |
| 5 | + |
| 6 | +[](https://circleci.com/gh/thebigredgeek/microlock/tree/master) |
| 7 | + |
| 8 | + |
| 9 | +## What is a distrbuted lock? |
| 10 | + |
| 11 | +A distributed lock is a mechanism that provides serialized flow control on a context that is acted on by more than one process. These processes typically operate on different machines via Service Oriented Architecture. Each process uses an object called a distributed lock to "lock" access to the shared context so that only one process can act on it at a time, thereby ensuring consistency and preventing race conditions. |
| 12 | + |
| 13 | +## Notes |
| 14 | + |
| 15 | +Microlock is currently compatible with Etd 2.2.x. Within the next two weeks, there will be full support and test coverage for Etcd 2.2.x - 3.x. |
| 16 | + |
| 17 | +## Install |
| 18 | +**Requires NodeJS >= 4.0** |
| 19 | + |
| 20 | +```bash |
| 21 | +$ npm install microlock |
| 22 | +``` |
| 23 | + |
| 24 | +## Basic usage |
| 25 | + |
| 26 | +### ES5 |
| 27 | +```javascript |
| 28 | +var os = require('os'); |
| 29 | +var Etcd = require('node-etcd'); |
| 30 | +var Microlock = require('microlock'); |
| 31 | + |
| 32 | +var key = 'foo'; //name of the lock |
| 33 | +var id = os.hostname(); //id of *this* node |
| 34 | +var ttl = 5; //5 second lease on lock |
| 35 | + |
| 36 | +var etcd = new Etcd(); |
| 37 | +var foo = new Microlock.default(etcd, key, id, ttl); |
| 38 | + |
| 39 | +foo.lock().then(function () { |
| 40 | + // foo is locked by this node |
| 41 | + |
| 42 | + // do some stuff... |
| 43 | + |
| 44 | + // release the lock |
| 45 | + return foo.unlock(); |
| 46 | +}, function (e) { |
| 47 | + if (e instanceof Microlock.AlreadyLockedError) { |
| 48 | + // foo is already locked by a different node |
| 49 | + } |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +### ES2015 (with babel) |
| 54 | +```javascript |
| 55 | +import { hostname } from 'os'; |
| 56 | +import Etcd from 'node-etcd'; |
| 57 | +import Microlock, { AlreadyLockedError } from 'microlock'; |
| 58 | + |
| 59 | +const key = 'foo'; //name of the lock |
| 60 | +const id = hostname(); //id of *this* node |
| 61 | +const ttl = 5; //5 second lease on lock |
| 62 | + |
| 63 | +const etcd = new Etcd(); |
| 64 | +const foo = new Microlock(etcd, key, id, ttl); |
| 65 | + |
| 66 | +foo.lock().then(() => { |
| 67 | + // foo is locked by this node |
| 68 | + |
| 69 | + // do some stuff... |
| 70 | + |
| 71 | + // release the lock |
| 72 | + return foo.unlock(); |
| 73 | +}, (e) => { |
| 74 | + if (e instanceof AlreadyLockedError) { |
| 75 | + // foo is already locked by a different node |
| 76 | + } |
| 77 | +}); |
| 78 | +``` |
| 79 | + |
| 80 | +### ES2016/2017 (with babel) |
| 81 | +```javascript |
| 82 | + |
| 83 | +import { hostname } from 'os'; |
| 84 | +import Etcd from 'node-etcd'; |
| 85 | +import Microlock, { AlreadyLockedError } from 'microlock'; |
| 86 | + |
| 87 | +async function main () { |
| 88 | + |
| 89 | + const key = 'foo'; //name of the lock |
| 90 | + const id = hostname(); //id of *this* node |
| 91 | + const ttl = 5; //5 second lease on lock |
| 92 | + |
| 93 | + const etcd = new Etcd(); |
| 94 | + const foo = new Microlock(etcd, key, id, ttl); |
| 95 | + |
| 96 | + try { |
| 97 | + await foo.lock(); |
| 98 | + // foo is locked by this node |
| 99 | + |
| 100 | + // do some stuff... |
| 101 | + |
| 102 | + // release the lock |
| 103 | + await foo.unlock(); |
| 104 | + } catch (e) { |
| 105 | + if (e instanceof AlreadyLockedError) { |
| 106 | + // foo is already locked by a different node |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +main(); |
| 112 | +``` |
| 113 | + |
| 114 | +## Methods |
| 115 | + |
| 116 | +### Microlock(etcd, key, node_id, [ttl = 1]) |
| 117 | +Creates a microlock client for a lock key. |
| 118 | + |
| 119 | +```javascript |
| 120 | +var Etcd = require('node-etcd'); |
| 121 | +var Microlock = require('microlock'); |
| 122 | + |
| 123 | +var etcd = new Etcd(); |
| 124 | +var foo = new Microlock.default(microlock, 'foo', 'bar'); |
| 125 | +``` |
| 126 | + |
| 127 | +### .lock() |
| 128 | +Attempts to lock the `key` for the `node_id`. |
| 129 | + |
| 130 | +```javascript |
| 131 | +foo.lock().then(function () { |
| 132 | + // foo is locked by this node |
| 133 | +}, function (e) { |
| 134 | + if (e instanceof Microlock.AlreadyLockedError) { |
| 135 | + // foo is already locked by a different node |
| 136 | + } |
| 137 | +}); |
| 138 | +``` |
| 139 | + |
| 140 | +### .unlock() |
| 141 | +Attempts to release the `key` for the `node_id`. |
| 142 | + |
| 143 | +```javascript |
| 144 | +foo.unlock().then(function () { |
| 145 | + // foo is unlocked |
| 146 | +}, function (e) { |
| 147 | + if (e instanceof Microlock.LockNotOwnedError) { |
| 148 | + // foo is not locked by `node_id` |
| 149 | + } |
| 150 | +}) |
| 151 | +``` |
| 152 | + |
| 153 | +### .renew() |
| 154 | +Attempts to renew the lock on `key` for the `node_id`. |
| 155 | + |
| 156 | +```javascript |
| 157 | +foo.renew().then(function () { |
| 158 | + // foo lease is renewed... ttl is refreshed |
| 159 | +}, function (e) { |
| 160 | + if (e instanceof Microlock.LockNotOwnedError) { |
| 161 | + // foo is not locked by `node_id` |
| 162 | + } |
| 163 | +}) |
| 164 | +``` |
| 165 | + |
| 166 | +### .destroy() |
| 167 | +Unbinds listeners/watchers from this client |
0 commit comments