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

rateman

v0.1.7

Published

Redis-based rate limiter with multi-window support.

Downloads

8

Readme

NPM version Coverage

Rateman

Rateman is a redis-based rate limiter with multi-window support.

Installation

npm install rateman

Usage

import ms from 'ms';
import {RateLimiter, RateLimitExceededError} from 'rateman';

const rateLimiter = new RateLimiter({
  name: 'user',
  windows: [
    {span: ms('1m'), limit: 10},
    {span: ms('1h'), limit: 100},
  ],
});

try {
  await rateLimiter.attempt('<user id>');
} catch (error) {
  if (error instanceof RateLimitExceededError) {
    console.error('rate limit exceeded', error.liftsAt);
  } else {
    console.error(error);
  }
}

Methods

Attempts

Rateman provides three methods that record attempts: attempt(), throttle() and the under the hood record().

You can also use the multiplier option to record multiple attempts at once:

await rateLimiter.attempt('<user id>', 3);

Reset

Use reset() to reset the limit for an identifier.

Express

Using Rateman with Express is easy:

app.use((req, _res, next) => {
  const ip = req.headers['x-forwarded-for'] || req.ip;

  void rateLimiter.attempt(ip).then(
    () => next(),
    error => next(error),
  );
});

Options

Redis Connection

Rateman uses ioredis for redis connection.

Provide a Redis instance or RedisOptions for connection other than localhost:6379.

import Redis from 'ioredis';

const rateLimiterA = new RateLimiter({
  // ...
  redis: new Redis(),
});

const rateLimiterA = new RateLimiter({
  // ...
  redis: {
    host: 'localhost',
    port: 6379,
  },
});

Record Throttled

Rateman by default ignores throttled attempts. It means that if an attempt is throttled, it will not have effect on the subsequential attempts.

Otherwise you can enable recordThrottled to record throttled attempts.

const rateLimiter = new RateLimiter({
  // ...
  recordThrottled: true,
});

Performance

Tested on WSL2 / Redis (local, Docker) with i9 13900K.

0-1,000 multiplied attempts with up to 1,000 records.

0-100: 22.570ms in total / 0.226ms per attempt (with multiplier 1).
100-200: 29.027ms in total / 0.290ms per attempt (with multiplier 1).
200-300: 30.777ms in total / 0.308ms per attempt (with multiplier 1).
300-400: 33.582ms in total / 0.336ms per attempt (with multiplier 1).
400-500: 39.601ms in total / 0.396ms per attempt (with multiplier 1).
500-600: 42.859ms in total / 0.429ms per attempt (with multiplier 1).
600-700: 48.280ms in total / 0.483ms per attempt (with multiplier 1).
700-800: 50.846ms in total / 0.508ms per attempt (with multiplier 1).
800-900: 57.854ms in total / 0.579ms per attempt (with multiplier 1).
900-1,000: 63.314ms in total / 0.633ms per attempt (with multiplier 1).

0-100,000 multiplied attempts with up to 1,000 records.

0-10,000: 68.204ms in total / 0.682ms per attempt (with multiplier 100).
10,000-20,000: 157.541ms in total / 1.575ms per attempt (with multiplier 100).
20,000-30,000: 249.387ms in total / 2.494ms per attempt (with multiplier 100).
30,000-40,000: 348.766ms in total / 3.488ms per attempt (with multiplier 100).
40,000-50,000: 438.351ms in total / 4.384ms per attempt (with multiplier 100).
50,000-60,000: 540.998ms in total / 5.410ms per attempt (with multiplier 100).
60,000-70,000: 622.636ms in total / 6.226ms per attempt (with multiplier 100).
70,000-80,000: 715.999ms in total / 7.160ms per attempt (with multiplier 100).
80,000-90,000: 820.891ms in total / 8.209ms per attempt (with multiplier 100).
90,000-100,000: 902.005ms in total / 9.020ms per attempt (with multiplier 100).

0-50,000 multiplied attempts with up to 50,000 records.

0-10,000: 24943.718ms in total / 2.494ms per attempt (with multiplier 1).
10,000-20,000: 72074.504ms in total / 7.207ms per attempt (with multiplier 1).
20,000-30,000: 120660.886ms in total / 12.066ms per attempt (with multiplier 1).
30,000-40,000: 165407.125ms in total / 16.541ms per attempt (with multiplier 1).
40,000-50,000: 219170.968ms in total / 21.917ms per attempt (with multiplier 1).

License

MIT License.