TokenBucket

TokenBucket

new TokenBucket(rate, max, interval)

Description:
  • Create a Token Bucket object for rate limiting as per http://en.wikipedia.org/wiki/Token_bucket

    • rate - the rate to refill tokens
    • max - the maximum burst capacity
    • interval - interval for the bucket refills, default 1000 ms

    Store as an array for easier serialization into JSON when keep it in the shared cache.

    Based on https://github.com/thisandagain/micron-throttle

Source:
Parameters:
Name Type Description
rate number | object | Array.<number>
max number
interval number

Methods

(static) configure()

Description:
  • Initialize existing token with numbers for rate calculations

Source:

(static) consume()

Description:
  • Consume N tokens from the bucket, if no capacity, the tokens are not pulled from the bucket.

    Refill the bucket by tracking elapsed time from the last time we touched it.

     min(totalTokens, current + (fillRate * elapsedTime))
    
Source:

(static) equal()

Description:
  • Return true if this bucket uses the same rates in arguments

Source:

(static) toArray()

Description:
  • Return an array object to be serialized/saved, can be used to construct new object as the rate param

Source:

(static) toJSON()

Description:
  • Return a JSON object to be serialized/saved, can be used to construct new object as the rate param

Source:

(static) toString()

Description:
  • Return a string to be serialized/saved, can be used to construct new object as the rate param

Source: