https://github.com/perongh/smooth-rate
A Redis-agnostic, flexible, and easy-to-use rate limiting module for Deno, utilizing a sliding window algorithm.
https://github.com/perongh/smooth-rate
deno ratelimit redis typescript
Last synced: about 2 months ago
JSON representation
A Redis-agnostic, flexible, and easy-to-use rate limiting module for Deno, utilizing a sliding window algorithm.
- Host: GitHub
- URL: https://github.com/perongh/smooth-rate
- Owner: PeronGH
- Created: 2023-06-05T00:59:31.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-08T14:18:42.000Z (almost 2 years ago)
- Last Synced: 2025-01-19T09:42:20.778Z (4 months ago)
- Topics: deno, ratelimit, redis, typescript
- Language: TypeScript
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# smooth-rate
A Redis-agnostic rate limiting module for Deno applications. smooth-rate offers a simple and adaptable interface for managing operation rates, making it an ideal solution for high-concurrency environments.
## Features
- **Redis-Agnostic**: Built to integrate with any Redis library, offering freedom to choose or switch your Redis library as per your needs.
- **Sliding Window Algorithm**: Implements the sliding window algorithm for precise and fluid rate limits, that transition smoothly over time rather than resetting periodically.- **Flexible Configuration**: Easy customization of key prefix, window size, and max requests to adapt to diverse rate limiting requirements.
- **Atomic Operations**: Each operation is atomic, ensuring consistent results in a concurrent environment.
## Basic Usage
Import the `RateLimiter` class from the module:
```typescript
import { RateLimiter } from 'https://deno.land/x/smooth_rate/mod.ts';
```Create an instance of `RateLimiter`:
```typescript
const rateLimiter = new RateLimiter({
keyPrefix: 'ratelimit',
windowMs: 60 * 1000, // 60 seconds
maxRequests: 100,
redis: {
// Take `x/redis` as an example
async eval(script, keys, args) {
const raw = await redis.eval(script, keys, args);
return raw!.toString();
},
async del(key) {
await redis.del(key);
},
},
});
```Use the `limit` and `check` methods:
```typescript
// Limit a request
const limitResult = await rateLimiter.limit('someIdentifier');// Check the rate limit status
const checkResult = await rateLimiter.check('someIdentifier');
```Both `limit` and `check` return a `RateLimitInfo` object:
- `isLimited`: Is the identifier currently limited?
- `nextAvailableTimestamp`: When is the next request allowed?
- `remainingRequests`: How many requests remain for the current window?Use `reset` to reset the rate limit for an identifier:
```typescript
await rateLimiter.reset('someIdentifier');
```## Contributing
Contributions are welcome. Open an issue or pull request on GitHub.