npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-bucket

v2.0.0

Published

A Redis-backed leaky-bucket rate limiter

Downloads

4,157

Readme

redis-bucket

build status codecov npm version

A Redis-backed rate limiter, based on the leaky-bucket algorithm. Implemented using a purely EVAL-based solution, which provides the following advantages:

  • It is optimized for cases where multiple instances of a service share common rate-limiting metrics, since the counts are shared via a single store.
  • It supports multiple simultaneous metrics, allowing for tiered rates.
  • It works with hosted Redis solutions that may not support custom modules.

Requirements

  • Development - Node.js and a running Redis instance for testing.
  • Runtime - A Redis client supporting EVAL; EVALSHA is recommended but not required.

Example

import * as express from 'express';
import * as redis from 'redis';
import * as limiter from 'redis-bucket';

// Create a Redis client with appropriate configuration and error handling
const client = redis.createClient({});
client.on('error', () => {});

// Create the limiter
const limit = limiter.create({
    capacity: { window: 60, min: 10, max: 20 }, // 10-20 calls per minute
    backoff: x => 2 ** x, // Exponential backoff
    async eval(script: string, keys: string[], argv: unknown[]) {
        return client.eval(script, { keys, arguments: argv.map(String) });
    },
    async evalsha(sha: string, keys: string[], argv: unknown[]) {
        return client.evalSha(sha, { keys, arguments: argv.map(String) });
    },
});

// Simple server, expects a "user" query parameter to identify callers
const app = express();
app.get('/', async (req, res) => {
    // Scope rate-limiting to a given user
    const result = await limit(req.query.user);
    if (result.allow) {
        // Accept this call
        res.sendStatus(200);
    } else {
        // Reject this call with "Too Many Requests"
        res.set('Retry-After', result.wait);
        res.sendStatus(429);
    }
});
app.listen(8080);

API

create(config)

Creates a test function to perform rate limiting against a given set of metrics. Takes a configuration object containing the following options:

  • eval - A callback to execute an EVAL call on Redis.
  • evalsha (default none) - A callback to execute an EVALSHA call on Redis.
  • prefix (default none) - A string prefix to apply to all Redis keys used by this instance.
  • backoff (default linear) - The backoff scaling function used for retries.
  • capacity - A capacity metric (or array thereof) to limit by (see below).
  • rate - A rate metric (or array thereof) to limit by (see below).

test(key, [cost])

Tests whether the given action should be allowed according to the rate limits. Returns a Result object. Takes the following arguments:

  • key - A string specifying the instance to be tested. Limits will only be applied to a given key against itself.
  • cost (default 1) - The capacity cost of this action (see below).

Result

An object representing the result of a test. Contains the following parameters:

  • allow - Whether or not this action should be allowed according to the rate limits.
  • free - The current remaining capacity before actions will be rejected; 0 if allow is false.
  • wait - How long the caller should wait before trying again, in seconds; 0 if allow is true.

Specifying Limits

The allowable limits for a given instance of the rate-limiter can be specified in two ways. Note that 'capacity' is an abstract value; typically it represents a number of actions, but it can also indicate an overall net 'cost' of actions.

Capacity Limits

Capacity limits are specified using three values:

  • window - The time window over which these limits are considered, in seconds.
  • min - The minimum capacity that is guaranteed over this time window, assuming a perfectly uniform call pattern (see note below).
  • max - The maximum capacity that the system can handle over this time window. This value is absolute; callers will be limited in such a way to enforce this. It must be sufficiently greater than the minimum capacity to cover the highest cost the test function will be called with.

Note: If the maximum capacity is set as low as possible (in other words, just greater than the minimum capacity), the caller must request capacity in a perfectly uniform manner in order to receive the minimum capacity. This restriction becomes increasingly loose as the maximum capacity increases, disappearing when the maximum capacity is twice the minimum capacity (at which point the minimum capacity becomes guaranteed independent of the call pattern).

Rate Limits

Rate limits are specified using two values:

  • flow - The rate at which capacity becomes available, per second. In a fully-stressed system, calls will be limited to exactly this rate.
  • burst - The amount of leeway in capacity the system can support. This is the amount of capacity that can be utilized before rate-limiting is applied. It must be at least equal to the highest cost the test function will be called with.

Contributing

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.