Sometimes, we want to throttle and queue up API requests due to per second cap with Node.js.
In this article, we’ll look at how to throttle and queue up API requests due to per second cap with Node.js.
How to throttle and queue up API requests due to per second cap with Node.js?
To throttle and queue up API requests due to per second cap with Node.js, we can use the node-rate-limiter
package.
To install it, we run
npm i limiter
Then we use it by writing
const { RateLimiter } = require("limiter");
const limiter = new RateLimiter({ tokensPerInterval: 150, interval: "hour" });
const throttledRequest = async (...args) => {
const remainingRequests = await limiter.removeTokens(1);
callMyMessageSendingFunction(...args);
};
to create a RateLimiter
instance with { tokensPerInterval: 150, interval: "hour" }
to allow 150 requests her hour.
Then we create the throttledRequest
function that calls limiter.removeTokens
with 1 and a callback that calls callMyMessageSendingFunction
to return a function that throttles the request
calls to 150 her hour.
Conclusion
To throttle and queue up API requests due to per second cap with Node.js, we can use the node-rate-limiter
package.