Throttles a promise-returning function using the token-bucket algorithm.
Many AWS APIs are throttled using the token-bucket algorithm. For instance, to prevent hitting the EC2 rate limits, the functions that call the AWS API can be wrapped and throttled.
npm install promise-throttle-bucket
const pThrottleBucket = require('promise-throttle-bucket')
const throttledFn = pThrottleBucket(describeEc2Instance, {
max: 100,
refill: 20
})
Promise.all(
[
/* array of 150 instances */
].map(throttledFn)
)
// After 0s, described instances 0-99
// After 1s, described instances 100-119
// After 2s, described instances 120-139
// After 3s, described instances 140-150
.then(function (instances) {
// Resolves after all the instances were described.
})
Returns a throttled function.
Type: Function
A function.
Type: object
Type: number
The maximum number of tokens in the bucket.
Type: number
The number of refill tokens per interval in milliseconds.
Type: number
Default: 1000
The refill interval in milliseconds.
Type: function
The throttled version of fn
.
It resolves when fn()
resolves and rejects if it rejects.
The time it takes to settle depends on how many calls are in the queue and the throttling parameters.