GithubHelp home page GithubHelp logo

isabella232 / redis-ratelimit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chriskinsman/redis-ratelimit

0.0 0.0 0.0 9 KB

Sliding window rate limiter based on redis sorted sets

License: MIT License

JavaScript 100.00%

redis-ratelimit's Introduction

Rate limiting / throttling based using Redis

NPM Version NPM Downloads Build Status

var ratelimit = require('redis-ratelimit')
var async = require('async')

var count = 0;
async.doWhilst(function(done) {
    ratelimit.check('counter', 10, 2, function(err, limited) {
        if(limited)
        {
            // Don't do anything, wait some amount of time
            // and check rate limit again
            setTimeout(done, 1000*5);
        }
        else
        {
            // Do work
            count++;
            done();
        }
    })
}, function() {
    return count < 10;
}, function(err) {
    // Done
    process.exit(0);
});

Installation

$ npm install redis-ratelimit

Features

  • Provides a distributed rate limit per key
  • Redis sorted sets used for high performance

Documentation

configure(port, host, options)

Optionally configures the underlying redis instance used by the rate limiter.

Arguments

  • port - Port redis is listening on
  • host - Host redis is on
  • options - Options hash passed through to underlying redis createClient() call

check(key, windowInSeconds, limit, callback)

Checks if the specified key over or under the rate limit. If over rate limit the call to check is not counted against the rate limit

Arguments

  • key - Unique key the rate limit is associated with
  • windowInSeconds - The length of the window during which the calls are rate limited. This is not a bucket but a sliding window
  • limit - Number of calls that are allowed before the rate limit kicks in
  • callback(err, limited) - A callback which indicates if the rate limit has kicked in or not. If an error occurred limited will be set to true. If limited returns true the call to check() does not count against the rate limit.

count(key, callback)

Returns the number of calls that are currently counting against the rate limit.

Arguments

  • key - Unique key the rate limit is associated with
  • callback(err, count) - A callback which returns the count of calls counting against the rate limit.

People

The author is Chris Kinsman

License

MIT

redis-ratelimit's People

Contributors

chriskinsman 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.