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

strict-rate-limiter

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

strict-rate-limiter

Rate limiter backed by redis with strict concurrency rules for scalable applications

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

strict-rate-limiter

Rate limiter backed by redis with strict concurrency rules for scalable applications

What

Suitable for cloud applications with multi-node and/or cluster infrastructure.

To keep the limiter up-to-date across multiple workers or nodes, it uses a lock mechanism to throttle the connection while the previous connection is not yet dispatched and the limiter is saved to the redis database. The lock mechanism throttles only those concurrent connections coming from the same origin (identified by the ID).

Requirements

  • Redis 2.6.12+ (2.8 recommended)

Dependecies

  • github.com/kriskowal/q

Install

$ npm install strict-rate-limiter

Usage

var redisClient = redis.createClient();

var id = req.apiKey;

// allow 100 request / 60s
limiters[id] = new RateLimiter({
  id: id,
  limit: 100, // 100 tokens
  duration: 60000 // 60s duration
}, redisClient);

limiters[id].get(function(err, limit, remaining, reset) {
  if (err) {
    return next(err);
  }

  res.setHeader('X-RateLimit-Limit', limit);
  res.setHeader('X-RateLimit-Remaining', remaining);
  res.setHeader('X-RateLimit-Reset', Math.floor(reset / 1000));
  
  if (remaining >= 0) {
    // allowed, call next middleware
    next();
    
  } else {
    // limit exceeded
    res.setHeader('Retry-After', Math.floor((reset - new Date()) / 1000));
    res.statusCode = 429;
    res.end('Rate limit exceeded.');
  }
});

Options

  • id Unique identifier (API key, IP address or user ID)
  • limit Number of tokens
  • duration Duration in milliseconds
  • namespace (optional) Overwrite key prefix (defaults to ratelimit:) (actual redis key is namespace + id)

Test

$ npm test

License

MIT

FAQs

Package last updated on 10 Mar 2016

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