bottleneck
Version:
Distributed task scheduler and rate limiter
96 lines (77 loc) • 2.74 kB
text/coffeescript
parser = require "./parser"
DLList = require "./DLList"
BottleneckError = require "./BottleneckError"
class Local
constructor: (options) ->
parser.load options, options, @
= Date.now()
= 0
= {}
= 0
= ()
= {}
disconnect: (flush) -> @
yieldLoop: (t=0) -> new (resolve, reject) -> setTimeout resolve, t
computePenalty: -> ? ((15 * ) or 5000)
__updateSettings__: (options) ->
await ()
parser.overwrite options, options, @
true
__running__: ->
await ()
__groupCheck__: (time) ->
await ()
( + ) < time
conditionsCheck: (weight) ->
((not ? or +weight <= ) and
(not ? or -weight >= 0))
__incrementReservoir__: (incr) ->
await ()
+= incr
__currentReservoir__: ->
await ()
isBlocked: (now) -> >= now
check: (weight, now) -> (weight) and (-now) <= 0
__check__: (weight) ->
await ()
now = Date.now()
weight, now
__register__: (index, weight, expiration) ->
await ()
now = Date.now()
if weight
+= weight
[index] =
timeout: if expiration? then setTimeout =>
if not [index].freed
[index].freed = true
-= weight
, expiration
freed: false
if ? then -= weight
wait = Math.max -now, 0
= now + wait +
{ success: true, wait, }
else { success: false }
strategyIsBlock: -> == 3
__submit__: (queueLength, weight) ->
await ()
if ? and weight >
throw new BottleneckError("Impossible to add a job having a weight of #{weight} to a limiter having a maxConcurrent setting of #{@maxConcurrent}")
now = Date.now()
reachedHWM = ? and queueLength == and not (weight, now)
blocked = () and (reachedHWM or now)
if blocked
= now + ()
= +
{ reachedHWM, blocked, strategy: }
__free__: (index, weight) ->
await ()
clearTimeout [index].timeout
if not [index].freed
[index].freed = true
-= weight
{ running: }
module.exports = Local