Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Bottleneck | 1,419 | 7,181 | 315 | 3 months ago | 79 | August 03, 2019 | 75 | mit | JavaScript | |
Job scheduler and rate limiter, supports Clustering | ||||||||||
Node Rate Limiter | 1,319 | 36,427 | 230 | 8 months ago | 16 | May 19, 2021 | 17 | mit | TypeScript | |
A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled | ||||||||||
Gubernator | 896 | a day ago | 83 | June 07, 2022 | 9 | apache-2.0 | Go | |||
High Performance Rate Limiting MicroService and Library | ||||||||||
Node Ratelimiter | 706 | 122 | 47 | 4 months ago | 19 | February 24, 2020 | 9 | JavaScript | ||
Abstract rate limiter for nodejs | ||||||||||
Slowapi | 632 | 1 | 3 days ago | 5 | August 28, 2021 | 31 | mit | Python | ||
A rate limiter for Starlette and FastAPI | ||||||||||
Golimit | 589 | 2 years ago | May 31, 2021 | 1 | mit | Go | ||||
Golimit is Uber ringpop based distributed and decentralized rate limiter | ||||||||||
Hammer | 506 | 12 | 5 | 10 days ago | 16 | June 13, 2022 | 15 | mit | Elixir | |
An Elixir rate-limiter with pluggable backends | ||||||||||
Go Limiter | 378 | 21 | 3 months ago | 16 | September 25, 2021 | 1 | apache-2.0 | Go | ||
A supersonic rate limiting package for Go with HTTP middleware. | ||||||||||
Diehard | 304 | a month ago | 31 | September 20, 2020 | 5 | epl-1.0 | Clojure | |||
Clojure resilience library for flexible retry, circuit breaker and rate limiter | ||||||||||
Rolling Rate Limiter | 287 | 42 | 15 | 2 months ago | 29 | September 13, 2022 | 3 | mit | TypeScript | |
Rate limiter for node.js that supports a rolling window, either in-memory or backed by redis |
This is async/await
friendly utility to limit the execution rate of any function. If you call the wrapped function more frequently than the rate limit allows, the wrapper avoided immediate call but arranges calls in the internal queue and call them later to ensure rate limit.
Often API providers prevent (or even ban) you from calling their API endpoints more often than specified number of times during defined time frame. As a responsible developer, you want to respect this restrictions on your own side and not give API provider a reason to restrict your code from the API access. call-rate-limiter
package provides utility functions to help you in achieving this goal.
Rate limiting functions provided use sliding window rate limiter under the hood. Every function wrapped in rate limiter becomes a Promise
-returning function. This Promise
resolves then the function is called and, if it was async as well, only after returned Promise
is resolved. Result of function execution is passed to resolve
function.
npm install --save call-rate-limiter
rateLimit
takes limitCount
, limitInterval
and fn
as arguments and returns rate limited function which should be called instead of the function passed as fn
.
This means if you call rateLimitedFunc
150 times and only 100 can be called in time frame, the next 50 calls will be postponed and executed later to respect given rate limits.
import {rateLimit} from 'call-rate-limiter'
const rateLimitedFetch = rateLimit(
1200,
60 * 1000,
id => fetch(`https://swapi.co/api/starships/${id}/`).then(res => res.json())
)
/*
fetch Death Star specs
rateLimit transparently passes args to wrapped function
*/
const deathStar = await rateLimitedFetch('9')
objectRateLimit
methodSuppose, there's number of APIs which have the same and only rate limit.
const basePath = 'https://swapi.co/api/'
function character(id) {
return fetch(`${basePath}/people/${id}`).then(res => res.json())
}
function planet(id) {
return fetch(`${basePath}/planet/${id}`).then(res => res.json())
}
module.exports = {
character: character,
planet: planet
}
You can setup single rate limiter for a bunch of functions with objectRateLimit
method. objectRateLimit
takes object
as last argument and return new object with rate-limited functions assigned for same keys:
import {objectRateLimit} from 'call-rate-limiter'
import api from './api'
const rateLimitedApi = objectRateLimit(1200, 60 * 1000, api)
// trying to list all planets in a galaxy far away
let i = 0
while(i < 100000) {
console.log(await rateLimitedApi.planet(i))
i++
}
Call burst
is a situation where single function is called many times during short time frame. That's a common issue with rate limiters, as they tend to unfreeze and send multiple requests to limited APIs as limit time window slides to allow new calls. Often you want to separate this calls by small timeout. To achieve this, you could use throttle
functions, like the one lodash provides. Just wrap your API-calling function with throttle
first and then wrap it in rateLimit
.
import {yourFunction} from 'your-module'
import {rateLimit} from 'call-rate-limiter'
import throttle from 'lodash.throttle'
const waitBetweenCalls = 100 // 100ms
const limitCount = 30 // max 30 calls per minute
const limitInterval = 60 * 1000 // one minute
const rateLimitedNoBurstingFunc = rateLimit(
limitCount,
limitInterval,
throttle(yourFunction, waitBetweenCalls)
)