-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
54 lines (46 loc) · 981 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict'
const async = require('async')
const _ = require('lodash')
const Base = require('bfx-facs-base')
const Redlock = require('redlock')
function redlock (conf, redisClient) {
const redlock = new Redlock([redisClient], _.extend(
{
driftFactor: 0.01,
retryCount: 10,
retryDelay: 200,
retryJitter: 200
}, conf
))
return redlock
}
class RedLockFacility extends Base {
constructor (caller, opts, ctx) {
super(caller, opts, ctx)
this.name = 'redis-lock'
this._hasConf = true
this.init()
}
_start (cb) {
async.series([
next => { super._start(next) },
next => {
this.lock = redlock(this.conf, this.opts.client)
next()
}
], cb)
}
_stop (cb) {
async.series([
next => { super._stop(next) },
next => {
this.lock.quit(next)
},
next => {
delete this.lock
next()
}
], cb)
}
}
module.exports = RedLockFacility