-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (56 loc) · 1.65 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var EventEmitter = require('events').EventEmitter
var backoff = require('backoff')
var inherits = require('inherits')
module.exports = Reconnect
function Reconnect(r, opts) {
var self = this
if (!(this instanceof Reconnect)) return new Reconnect(r, opts)
this.backoff = (backoff[opts.type] || backoff.fibonacci)({
randomisationFactor: 0,
initialDelay: opts.initialDelay || 10,
maxDelay: opts.maxDelay || 1000
})
opts.failAfter && this.backoff.failAfter(opts.failAfter)
this.conn = null
this.reconnect = true
this.backoff.on('ready', function(number, delay) {
if (!self.reconnect) return
self.emit('reconnect', number, delay)
_connect(this.cb)
})
this.backoff.on('backoff', function (number, delay) {
self.emit('backoff', number, delay)
})
this.backoff.on('fail', function(){
self.reconnect = false
self.emit('_cleanup')
self.emit('fail')
})
this.on('_cleanup', function(err){
this.conn && this.conn.removeAllListeners()
this.emit('disconnect', err)
this.backoff.backoff()
})
function _connect() {
r.connect(opts, function(err, conn) {
if (err) return self.emit('_cleanup', err)
conn.on('error', self.emit.bind(self, '_cleanup', err))
conn.on('close', self.emit.bind(self, '_cleanup'))
self.conn = conn
self.emit('connect', conn)
self.backoff.reset()
self.cb && self.cb(conn)
})
}
}
Reconnect.prototype.connect = function(cb) {
this.reconnect = true
this.cb = cb || null
this.backoff.backoff()
}
Reconnect.prototype.disconnect = function() {
this.reconnect = false
this.backoff.reset()
this.conn && this.conn.close()
}
inherits(Reconnect, require('events').EventEmitter)