https://github.com/patrickpissurno/tiny-rate-limiter
Allows you to easily limit outgoing API requests (and more). Dependency free!
https://github.com/patrickpissurno/tiny-rate-limiter
async bloat-free dependency-free nodejs outbound outgoing rate-limiter rate-limiting tiny token-bucket
Last synced: 8 months ago
JSON representation
Allows you to easily limit outgoing API requests (and more). Dependency free!
- Host: GitHub
- URL: https://github.com/patrickpissurno/tiny-rate-limiter
- Owner: patrickpissurno
- License: mit
- Created: 2022-03-05T22:26:05.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-26T01:51:06.000Z (about 1 year ago)
- Last Synced: 2025-07-15T01:37:31.117Z (8 months ago)
- Topics: async, bloat-free, dependency-free, nodejs, outbound, outgoing, rate-limiter, rate-limiting, tiny, token-bucket
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tiny-rate-limiter
[](https://www.npmjs.com/package/tiny-rate-limiter)
[](https://snyk.io/test/github/patrickpissurno/tiny-rate-limiter)
[](https://www.npmjs.com/package/tiny-rate-limiter)
[](https://github.com/patrickpissurno/tiny-rate-limiter/blob/master/LICENSE)
Allows you to easily limit outgoing API requests (and more). Dependency free!
## Install
```
npm i tiny-rate-limiter
```
## Importing
```js
const rateLimiter = require('tiny-rate-limiter');
```
## How to use
```js
async function someFunction(){
const key = 'this can be anything'; //things that use the same key share the same bucket
const max_token_count = 100; //how many tokens the bucket can fit
const window_in_milliseconds = 1000; //how many milliseconds between each bucket refill
await rateLimiter(key, max_token_count, window_in_milliseconds);
//from here on code is guaranteed to only execute at most 100 times per second
}
```
Real world:
```js
async function someFunction(){
await rateLimiter('anything', 100, 1000);
//from here on code is guaranteed to only execute at most 100 times per second
}
```
## What is a token bucket
- Put simply: imagine you have a bucket.
- Let's say that bucket can fit at most 10 tokens.
- It also only gets refilled with them, let's say, once every second.
- As soon as you call `await rateLimiter(...)` the function execution is temporarily paused and it's put in a queue.
- One by one, as long as there are tokens in that bucket, tokens are consumed for each function that is removed from that queue and resumes execution.
- If the bucket is empty, no function will resume execution until the bucket is refilled again.
## Example usage scenario
- Imagine your code is calling an API that has a rate limiter in place (let's say 100 requests per second).
- Let's say that if you exceed that quota bad things happen.
- You can use this module to easily prevent you from ever exceeding that quota by preventing the API requests from being made in the first place (that is, only allowing 100 req/s to be made).
- This is known as outbound (or outgoing) rate limiting (which is used on the caller side), as opposed to an inbound (or incoming) rate limiter (that would instead be used on the receiver side)
Although throughout this documentation its usage's been always demonstrated with this scenario in mind, this module has nothing to do with APIs or network requests, and its usage is in no way limited to only that. Feel free to use it in anything Node.js where it could help.
At its present state, though, it does not support being used in a browser environment, due to its reliance on the presence of the `process` object.
## Dependency free and opinionated
This is a minimal implementation that doesn't rely on anything other than Node.js itself. Its code is composed of only 57 lines (including comments, JSDoc annotations and a bunch of empty lines). It's a bit opinionated and exposes an interface that's aimed at making its usage as minimal as its code footprint.
## Performance
Although it's not been benchmarked extensively, due to the way its implemented, it should not degrade performance even with a few thousand requests per second. Your mileage may vary, though. I'm open to ideas on how to push it even further.
## LICENSE
MIT License
Copyright (c) 2022-2025 Patrick Pissurno
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.