Giter Club home page Giter Club logo

limitur's Introduction

Limitur

Build Status Coverage Status

Limitur is a (very) fast solution to rate limiting your application in under 100 lines of code. It is tailored for use in Redis; for a generic implementation, see limitus. It's written in TypeScript and is type-safe.

Quickstart

Limitur has a central "store" that contains "rules", which are buckets that can limit specific keyed values. For example, you might use this to rate limit a "login" endpoint in Express for a certain username:

const Limitur = require('limitur');
const redis = require('redis');
const limiter = new Limitur(redis.createClient());

limiter.rule('login', {
    limit: 10,
    interval: 15 * 60 * 1000,
});

app.post('/login', (req, res) => {
    limiter.limit('login', req.body.username).then(result => {
        res.set('X-RateLimit-Limit', result.limit);
        res.set('X-RateLimit-Remaining', result.remaining - 1);
        res.set('X-RateLimit-Reset', result.resetsAt);

        if (result.isLimited) {
            res.send(429, 'Too many requests!');
        } else {
            doLogin(req, res);
        }
    });
});

Benchmarks

Limitur is the fastest Redis rate limiting solution for Node that I'm aware of. You can run benchmarks using npm run bench. These below results were run on a Windows 10 machine using Redis 3.2 and Node 6.

{ limitur }  » matcha benchmark.js

          20,243 op/s » under limit: WatchBeam/limitur
          20,987 op/s » over limit: WatchBeam/limitur

          15,307 op/s » under limit: tabdigital/redis-rate-limiter
          15,955 op/s » over limit: tabdigital/redis-rate-limiter

          14,494 op/s » under limit: tj/ratelimiter
          14,576 op/s » over limit: tj/ratelimiter

          13,475 op/s » under limit: WatchBeam/limitus
          13,627 op/s » over limit: WatchBeam/limitus

  Suites:  0
  Benches: 6
  Elapsed: 29,479.43 ms

limitur's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

limitur's Issues

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.