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-ratelimit

v2.0.0

Published

Rate limiter using redis - sliding window based on sorted sets and fixed window

Downloads

10

Readme

Rate limiting / throttling using Redis

Async/await based. For callbacks checkout v1.x.

import slidingWindow from "./lib/slidingwindow.mjs";
import timers from "timers/promises";

for (let count = 0; count < 10; count++) {
  let limited = await slidingWindow.check("counter", 10, 2);
  if (limited) {
    // Don't do anything, wait some amount of time
    // and check rate limit again
    console.log("Rate limited");
    await timers.setTimeout(1000 * 5);
  } else {
    // Do work!
    console.log("Doing work");
  }
}

process.exit(0);

Installation

$ npm install redis-ratelimit

Features

  • Provides a distributed rate limit per key
  • Sliding window uses redis sorted sets for high performance
  • Fixed window using simple counter with expiration

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

fixedWindow

A fixed window rate limiter. Rate limit is applied for duration of window. Reset at window expiration. No more requests are allowed until window expires.

check(key, windowInSeconds, limit)

Checks if the specified key is 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 a bucket not a sliding window. The underlying redis key expires after this duration.
  • limit - Number of calls that are allowed before the rate limit kicks in

Returns

  • bool - Indicates if the rate limit has kicked in or not. If an error occurred limited will be set to true. If limited returns true.

count(key)

Returns the number of calls that are currently counting against the rate limit. Note this number can be greater than the limit as each call to check increments the counter.

Arguments

  • key - Unique key the rate limit is associated with

Returns

  • int - The count of calls counting against the rate limit.

fixedTimeWindow

A fixed time window based rate limiter. Allows a fixed number of requests per time window. i.e. 10 requests per minute

check(keyPrefix, windowUnit, limit)

Checks if the specified key is over or under the rate limit for the specified time window.

Arguments

  • keyPrefix - Prefix for a unique key the rate limit is associated with
  • windowUnit - The time unit for the rate limiter. Valid units:
  • second
  • minute
  • hour
  • week
  • isoWeek
  • month
  • quarter
  • year

This is a bucket not a sliding window. The underlying redis key expires after the duration of the windowUnit.

  • limit - Number of calls that are allowed before the rate limit kicks in

Returns

  • bool - True if the rate limit has kicked in or not. If an error occurred true will be returned.

count(key)

Returns the number of calls that are currently counting against the rate limit. Note this number can be greater than the limit as each call to check increments the counter.

Arguments

  • key - Unique key the rate limit is associated with

Returns

  • int - The count of calls counting against the rate limit.

slidingWindow

A sliding window rate limiter. Individual requests are expired out based on request time. Once oldest request expires a new request will be allowed to occur.

check(key, windowInSeconds, limit)

Checks if the specified key is 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

Returns

  • bool - True if the rate limit has kicked in or not. If an error occurred return will be true. If true is returned the call to check() does not count against the rate limit.

count(key)

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

Arguments

  • key - Unique key the rate limit is associated with

Returns

  • int - The count of calls counting against the rate limit.

People

The author is Chris Kinsman

License

MIT