forked from koding/koding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackoff.coffee
More file actions
25 lines (21 loc) · 799 Bytes
/
Copy pathbackoff.coffee
File metadata and controls
25 lines (21 loc) · 799 Bytes
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
module.exports = (ctx, options) ->
[options, ctx] = [ctx, options] unless options
options ?= {}
ctx ?= this
totalReconnectAttempts = 0
initalDelayMs = options.initialDelayMs ? 700
multiplyFactor = options.multiplyFactor ? 1.4
maxDelayMs = options.maxDelayMs ? 1000 * 15 # 15 seconds
maxReconnectAttempts = options.maxReconnectAttempts ? 50
ctx.clearBackoffTimeout = ->
totalReconnectAttempts = 0
ctx.setBackoffTimeout = (attemptFn, failFn) ->
if totalReconnectAttempts < maxReconnectAttempts
timeout = Math.min initalDelayMs * Math.pow(
multiplyFactor, totalReconnectAttempts
), maxDelayMs
setTimeout attemptFn, timeout
totalReconnectAttempts++
else
failFn()
return ctx