Skip to content

Commit

Permalink
feat: support etcdKeyPrefix option
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Sep 27, 2017
1 parent c4691a4 commit f24c9a6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const locker = new Locker({ endPoint: '127.0.0.1:2379' })
;(async function () {
// Aquire a lock for a specified recource.
const lock = await locker.lock('resource_key', 3 * 1000)
// This lock will be aquired after 3000 ms.
// This lock will be acquired after 3000 ms.
const anotherLock = await locker.lock('resource_key', 3 * 1000)
// Unlock the lock manually.
await anotherLock.unlock()
Expand All @@ -38,7 +38,8 @@ const locker = new Locker({ endPoint: '127.0.0.1:2379' })
### new Locker({ endPoint, defaultTimeout, rootCerts, privateKey, certChain })

- endPoint `String`: The end point address of etcd(v3) server, by default is `'127.0.0.1:2379'`
- defaultTimeout `Number`: Milliseconds of lock's default timeout, by default if `5000`.
- defaultTimeout `Number`: Milliseconds of lock's default timeout, by default is `5000`.
- etcdKeyPrefix `String`: Prefix of the keys of locks in etcd, by default is `'__etcd_lock/'`.
- rootCerts, privateKey, certChain `Buffer`: Options to create a GRPC SSL Credentials object, see https://grpc.io/grpc/node/src_credentials.js.html#line85.

### lock({ keyName, timeout = this.defaultTimeout })
Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ const RPC_PROTO_PATH = path.join(__dirname, './proto/rpc.proto')
const LockProto = grpc.load(LOCK_PROTO_PATH).v3lockpb
const RpcProto = grpc.load(RPC_PROTO_PATH).etcdserverpb

const ETCD_KEY_PREFIX = 'node_etcd_lock/'
const DEFAULT_ETCD_KEY_PREFIX = '__etcd_lock/'

class Locker {
constructor (options = {
endPoint: '127.0.0.1:2379',
defaultTimeout: 5 * 1000,
etcdKeyPrefix: DEFAULT_ETCD_KEY_PREFIX,
rootCerts: null,
privateKey: null,
certChain: null
}) {
let { endPoint, defaultTimeout, rootCerts, privateKey, certChain } = options
let { endPoint, defaultTimeout, etcdKeyPrefix, rootCerts, privateKey, certChain } = options

this.defaultTimeout = defaultTimeout || 5 * 1000
this.endPoint = endPoint || '127.0.0.1:2379'
this.etcdKeyPrefix = etcdKeyPrefix || DEFAULT_ETCD_KEY_PREFIX

let credentials
if (rootCerts) {
Expand Down Expand Up @@ -82,7 +84,7 @@ class Locker {
}

_assembleKeyName (keyName) {
return `${ETCD_KEY_PREFIX}${keyName}`
return `${this.etcdKeyPrefix}${keyName}`
}

_unlock (key) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-etcd-lock",
"version": "0.2.2",
"version": "0.2.3",
"description": "Distributed locks powered by etcd v3 for Node.js",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ describe('node-etcd-lock tests', function () {

describe('locker', function () {
it('use default end point', function () {
const { endPoint } = new Locker({
const { endPoint, etcdKeyPrefix } = new Locker({
rootCerts: Buffer.from(''),
privateKey: Buffer.from(''),
certChain: Buffer.from('')
})

assert(endPoint === '127.0.0.1:2379')
assert(etcdKeyPrefix === '__etcd_lock/')
})

it('lock with a empty key name', function * () {
Expand Down

0 comments on commit f24c9a6

Please sign in to comment.