New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rolling-rate-limiter

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rolling-rate-limiter

Rate limiter that supports a rolling window, either in-memory or backed by redis

  • 0.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
33K
decreased by-28.45%
Maintainers
1
Weekly downloads
 
Created
Source

Rolling Rate Limiter

Description

This is an implementation of a rate limiter in node.js that allows for rate limiting with a rolling window. This means that if a user is allowed 5 actions per 60 seconds, any action will be blocked if 5 actions have already occured in the preceeding 60 seconds, without any set points at which this interval resets. This contrasts with many existing implementations, in which a user could make 5 requests at 0:59 and another 5 requests at 1:01. The implementation uses what I believe to be a novel algorithm, using sorted sets.

Method of operation

  • Users are namespaced by an identifier, which is combined with a rate limiter namespace to form unique keys in redis.
  • Each key corresponds to a sorted set. The keys and values are both set to the (microsecond) times at which actions were attempted.
  • When a new action comes in, all elements in the set with keys less than (now - rate limit window) are dropped.
  • If there are still (limit) actions in the set, the current action is blocked.
  • If the most recent previous key is too close to the current time, and a minimum difference has been set, the current action is blocked.
  • The current action is added to the set.
  • Note: if an action is blocked, it is still added to the set.
  • All redis operations are performed as an atomic transaction.

Examples

In memory

  
  var RateLimiter = require("rolling-rate-limiter");

  var limiter = RateLimiter({
    interval: 1000 // in miliseconds
    maxInInterval: 10
    minDifference: 100 // optional, in miliseconds
  });

  limiter("user1234", function(err, success) {
    // errors if redis connection failed, etc
    if (err) throw err;

    if (success) {
      // limit was not exceeded, action should be allowed
    } else {
      // limit was exceeded, action should not be allowed
    }
  });

With a redis instance

This allows multiple processes (e.g. multiple instances of a server application) to use a single redis to share rate limiter state.

  
  var RateLimiter = require("rolling-rate-limiter");
  var Redis = require("redis");
  var client = Redis.createClient(config);

  var limiter = RateLimiter({
    redis: client,
    namespace: "UserLoginLimiter" // optional, allows one redis instance to handle multiple rate limiters
    interval: 1000 // in miliseconds
    maxInInterval: 10
    minDifference: 100 // optional, in miliseconds
  });

  limiter("user1234", function(err, success) {
    // errors if redis connection failed, etc
    if (err) throw err;

    if (success) {
      // limit was not exceeded, action should be allowed
    } else {
      // limit was exceeded, action should not be allowed
    }
  });

Keywords

FAQs

Package last updated on 05 Feb 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc