Giter Club home page Giter Club logo

limitation's Introduction

Limitation

An efficient rate limiter with several backends, including a Kademlia DHT.

Features

  • Fully synchronous local limit checking for lowest latency and high throughput. Using node 4.3 on a single core, a throughput of about 8 million limit checks per second is typical.
  • Backends scale constant in number of requests, and linear in number of keys. Tested with thousands of distinct limits.
  • Backends:
    • Resilient Kademlia DHT storage backend using an exponentially decaying counter.
    • Simple and very fast local in-memory storage backend.

Installation

npm install limitation

Usage

Simple in-memory backend:

var Limitation = require('limitation');

var ratelimiter = new Limitation();

if (ratelimiter.isAboveLimit('some_key', 10)) {
  console.log('Limit of 10 req/s exceeded!');
}

Kademlia backend:

var Limitation = require('limitation');

var ratelimiter = new Limitation({
    seeds: [{
        address: 'localhost',
        port: 3050
    },
    // Host names are also supported, expands to some.host:3050
    'some.host'],

    // Optional parameters
    // Address / port to listen on
    // Default: localhost:3050, random port fall-back if port used
    listen: {
        address: '127.0.0.1',
        port: 3050
    },
    // Counter update / block interval; Default: 10000ms
    // Shorter interval means quicker limiting, but also higher load.
    interval: 10000
});

// Returns Promise<Limitation>
ratelimiter.setup();

if (ratelimiter.isAboveLimit('some_key', 10)) {
  console.log('Limit of 10 req/s exceeded!');
}

// Clears any set timers so the process can exit cleanly.
ratelimiter.stop()

See also test/index.js for a runnable Kademlia example. It starts multiple DHT nodes, and you can start multiple copies of the script to play with nodes going down etc.

limitation's People

Contributors

arlolra avatar d00rman avatar gwicke avatar kartikm avatar pchelolo 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.