patiently
v2.1.4
Published
API rate limit handler for (Node) JS. Make your API client patient. A legal way to wait for API rate limit resets.
Downloads
55
Maintainers
Readme
patiently
API rate limit handler for (Node) JS. Make your API client patient. A legal way to wait for API rate limit resets.
Purpose
Sometimes there is no other way to be patient and to wait for the API rate limit to reset. Just think of a personalized API that allows you to perform actions in a specific user context. You want to do like 10.000 reqs as fast as possible. What if there are rate limits like 50 reqs per minute and 750 reqs per hour? And what if those limits are not bound to an IP or a host but to your user? In this case there is no other way but to wait for a limit reset. Of course, you can also politely ask the API owner to increase the limits. But even then you need patiently. :)
Features
- patiently will handle API rate limits for you and make your API client wait for a limit reset automatically
- set minutely or hourly rate limits manually
Planned Features
- [x] Add secondly limit feature (e.g. 2 reqs per second). --> Done: You can now adjust a millisecondly waiting time between two calls.
- [ ] If required I can add a "maxConcurrent" feature like in bottleneck
- [ ] I'm also trying to figure out how to deal with rate limit headers like
x-ratelimit-remaining
orretry-after
. At first sight this is not so easy, because the first headers only arrive after the first API call. And until then, any number of API calls could have been fired.
Installing
Using npm:
$ npm install patiently
How to use
Use by setting limits manually
Perhaps you already know about the limits (maybe from the API docs).
import patiently from "patiently";
// let patiently = require("patiently");
let options = {
startWaitingCallback: function(info){console.log(info)}, // default is function(){}, calls a function if waiting necessary
endWaitingCallback: function(info){console.log(info)}, // default is function(){}, calls a function after waiting
waitingTickCallback: function(info){console.log(info)}, // default is function(){}, calls a function every tick
msBetweenTwoCalls: 1000, // default is 0 milliseconds (no waiting time between two calls)
minutelyLimit: 50, // default is Infinity (no minutely limit set)
hourlyLimit: 750, // default is Infinity (no hourly limit set)
test: false // default is false (if true, max waiting time is 5 secs)
}
var waiter = new patiently.LimitWaiter(options);
let myApiCallFunction = async (url, callback) => {
waiter.wait(function(){
// your api call
axios.get(url)
.then(res => {
callback(res.data);
})
.error(err => {
callback(null, err);
})
})
}
// you can call myApiCallFunction as often you want
// patiently can handle asynchronous api calls :)
let url = "https://www.npmjs.com/package/patiently";
myApiCallFunction(url);
myApiCallFunction(url);
myApiCallFunction(url);
myApiCallFunction(url);
// ...
How does it work?
Each function you give to the waiter as a parameter is queued in an array
F -> E D C B A
The longest waiting function is first
F E D C B -> A
Before the function is called, it is checked whether we have to wait first or not
F E D C B -> A (wait?)
If yes the queue processing is paused and when the waiting time elapsed, function A will be called
F E D C B -> A (call)
License
MIT