Coder Social home page Coder Social logo

koshnic / ratelimit Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 45 KB

Redis-based rate limiting implementation using the token bucket algorithm for Node.js and TypeScript.

License: MIT License

TypeScript 100.00%
rate-limit rate-limiter tokenbucket

ratelimit's Introduction

Rate limit for ioredis

Redis-based rate limiting using the token bucket algorithm.

This project is an adaptation for Node.js, inspired by go-redis.

Why use ratelimit

  • Easy to use

    Integrate with just two lines of code. The library's design allows for tailored usage to best fit your application's needs.

  • Adopted from Proven Practices

    The Lua script utilized is a mature practice from the go-redis ecosystem. We act as porters of the script and provide a nodejs wrapper for it, ensuring both efficiency and ease of use.

  • Scalability

    Being Redis-based, ratelimit is inherently scalable. Whether you're handling hundreds or millions of requests, the library scales gracefully with your application.

  • Built for Modern Web

    Whether it's traditional web applications, microservices, or serverless functions, ratelimit is designed for the modern web's diverse deployment scenarios.

  • TypeScript Support

    With built-in TypeScript definitions, developers can benefit from type-safety and autocompletion, improving the development experience.

Installation

npm install @koshnic/ratelimit

Usage

Note:

  • All rate limit keys stored in Redis will be prefixed with rate_limit:. For example, when you invoke limiter.allowPerSecond('project:123', 10), the corresponding key name in Redis will be rate_limit:project:123.
  • Please note that this library does not handle errors internally. It's recommended that users implement their own error handling mechanisms when integrating with this library to ensure robust and resilient applications.
const Redis = require("ioredis");
const {RateLimiter} = require('@koshnic/ratelimit');

const redis = new Redis({
    host: 'localhost',
    port: 6379
});

const limiter = new RateLimiter(redis);
let res;

// Allow 10 requests per second, default 1 second for period
res = await limiter.allowPerSecond('project:123', 10);
console.log('PerSecond - allowed: ', res.allowed, ' remaining: ', res.remaining, ' retryAfter:', res.retryAfter, ' resetAfter:', res.resetAfter);

// Allow 10 requests per 20 seconds, override default seconds.
res = await limiter.allowPerSecond('project:456', 10, 20);

// Allow 20 requests per minute, default 1 minute for period
res = await limiter.allowPerMinute('project:456', 20);

// Allow 30 requests per hour, default 1 hour for period
res = await limiter.allowPerHour('project:789', 30);

// Or you want to use raw allow function to meet your custom logic.
res = await limiter.allow('project:cutom_logic', {
    burst: 10,
    ratePerPeriod: 10,
    period: 60,
    cost: 1
});

License

MIT

ratelimit's People

Contributors

heyuanyou avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.