https://github.com/journy-io/ratelimiter
📶 Sliding rate limiter using Redis
https://github.com/journy-io/ratelimiter
ratelimiter redis sliding typescript
Last synced: 8 months ago
JSON representation
📶 Sliding rate limiter using Redis
- Host: GitHub
- URL: https://github.com/journy-io/ratelimiter
- Owner: journy-io
- License: mit
- Created: 2021-02-06T14:13:48.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-02-17T13:58:40.000Z (over 2 years ago)
- Last Synced: 2025-02-03T14:45:27.033Z (8 months ago)
- Topics: ratelimiter, redis, sliding, typescript
- Language: TypeScript
- Homepage:
- Size: 92.8 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://journy.io/?utm_source=github&utm_content=readme-ratelimiter)
# Rate limiter

[](https://www.npmjs.com/package/@journyio/ratelimiter)A sliding rate limiter using Redis. This package depends on [moment/luxon](https://github.com/moment/luxon) and [luin/ioredis](https://github.com/luin/ioredis).
We don't recommend consuming this package in plain JavaScript (to be able to use interfaces).
Inspired by these blogposts:
* https://engagor.github.io/blog/2017/05/02/sliding-window-rate-limiter-redis/
* https://engagor.github.io/blog/2018/09/11/error-internal-rate-limit-reached/## 💾 Installation
You can use your package manager (`npm` or `yarn`) to install:
```bash
npm install --save @journyio/ratelimiter
```
or
```bash
yarn add @journyio/ratelimiter
```## 🔌 Getting started
```ts
import Client from "ioredis";
import { Duration } from "luxon";
import { RateLimiter, RateLimiterRedis, RateLimitedResource } from "@journyio/ratelimiter";
import { ClockSystem } from "@journyio/clock";class API {
constructor(private readonly rateLimiter: RateLimiter) {}async getUser(id: UserId) {
const resource = new RateLimitedResource(
`API-calls-user-${id}`,
100,
Duration.fromObject({ minute: 1 })
);const allowed = await this.rateLimiter.consume(resource);
if (!allowed) {
throw new Error("Rate limited!");
}/* ... */
}
}const redis = new Client(process.env.REDIS_URL)
const rateLimiter = new RateLimiterRedis(redis, new ClockSystem());
const api = new API(rateLimiter);
const user = await api.getUser(/* ... */);
``````ts
const resource = new RateLimitedResource(
"API-calls-user-1313",
100,
Duration.fromObject({ minute: 1 })
);const allowed = await this.rateLimiter.consume(resource);
const remainingCalls = await rateLimiter.remaining(resource);
await rateLimiter.reset(resource);
```## 💯 Tests
To run the tests:
```bash
npm run test
```(assuming redis runs on port 6379)
## 🔒 Security
If you discover any security related issues, please email security at journy io instead of using the issue tracker.