1.4.0 • Published 7 years ago
ratelimit-wait v1.4.0
node-ratelimit-wait
A ratelimit library intended for API clients, which will return a promise that waits until it's safe to run your code, rather than just giving a yes/no.
Usage
First off, require()
and instantiate a bucket for your ratelimit:
const ratelimitWait = require('node-ratelimit-wait');
const ratelimit = new ratelimitWait({
bucketSize: 1,
refillTime: 1000
});
bucketSize
is how many requests you can make at once, assuming you haven't used any requests. In other words, it's your burst capacity. refillTime
is how long it takes to refill the whole bucket (in milliseconds), or in other words, it's the minimum sustained time between requests, multiplied by the bucketSize
.
Once you have that object, just call its wait()
method and wait for the returned promise to resolve before running your API call.
ratelimit.wait().then(() => {
// dostuff
})
Additional options
rejectOver
will make anywait()
reject its promise instead of waiting, and will makeconsume()
throw an Error instead of returning the number of milliseconds to wait if the time to wait is overrejectOver
milliseconds.
Additional methods
consume()
is the same aswait()
, except it returns the number of milliseconds to wait, rather than a promise.getWait()
will return the number of milliseconds await()
would wait, if you issued it right now. This does not consume a request.setRefillTime(time)
will change the bucket'srefillTime
to the new value.