1.4.0 • Published 7 years ago

ratelimit-wait v1.4.0

Weekly downloads
2
License
BSD-3-Clause
Repository
github
Last release
7 years ago

node-ratelimit-wait

Build Status

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 any wait() reject its promise instead of waiting, and will make consume() throw an Error instead of returning the number of milliseconds to wait if the time to wait is over rejectOver milliseconds.

Additional methods

  • consume() is the same as wait(), except it returns the number of milliseconds to wait, rather than a promise.
  • getWait() will return the number of milliseconds a wait() would wait, if you issued it right now. This does not consume a request.
  • setRefillTime(time) will change the bucket's refillTime to the new value.
1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago