パッケージの詳細

async-ratelimiter

microlinkhq90.5kMIT1.6.1

Rate limit made simple, easy, async.

async, limit, limiter, promise

readme

microlink logo microlink logo

Last version Coverage Status NPM Status

Rate limit made simple, easy, async. Based on ratelimiter.

Install

$ npm install async-ratelimiter --save

Usage

The most straightforward way to use the rate limiter:

'use strict'

const RateLimiter = require('async-ratelimiter')
const { getClientIp } = require('request-ip')
const Redis = require('ioredis')

const rateLimiter = new RateLimiter({
  db: new Redis()
})

const apiQuota = async (req, res, next) => {
  const clientIp = getClientIp(req)
  const limit = await rateLimiter.get({ id: clientIp })

  if (!res.writableEnded) {
    res.setHeader('X-Rate-Limit-Limit', limit.total)
    res.setHeader('X-Rate-Limit-Remaining', Math.max(0, limit.remaining - 1))
    res.setHeader('X-Rate-Limit-Reset', limit.reset)
  }

  return !limit.remaining
    ? sendFail({
        req,
        res,
        code: HTTPStatus.TOO_MANY_REQUESTS,
        message: MESSAGES.RATE_LIMIT_EXCEDEED()
      })
    : next(req, res)
}

For scenarios where you want to check the limit status before consuming a request, you should to pass { peek: true }:

const apiQuota = async (req, res, next) => {
  const clientIp = getClientIp(req)

  // Check rate limit status without consuming a request
  const status = await rateLimiter.get({ id: clientIp, peek: true })

  if (status.remaining === 0) {
    return sendFail({
      req,
      res,
      code: HTTPStatus.TOO_MANY_REQUESTS,
      message: MESSAGES.RATE_LIMIT_EXCEDEED()
    })
  }

  // Consume a request
  const limit = await rateLimiter.get({ id: clientIp })

  if (!res.writableEnded) {
    res.setHeader('X-Rate-Limit-Limit', limit.total)
    res.setHeader('X-Rate-Limit-Remaining', limit.remaining)
    res.setHeader('X-Rate-Limit-Reset', limit.reset)
  }

  return next(req, res)
}

API

constructor(options)

It creates an rate limiter instance.

options

db

Required
Type: object

The redis connection instance.

max

Type: number
Default: 2500

The maximum number of requests within duration.

duration

Type: number
Default: 3600000

How long keep records of requests in milliseconds.

namespace

Type: string
Default: 'limit'

The prefix used for compound the key.

id

Type: string

The identifier to limit against (typically a user id).

You can pass this value using when you use .get method as well.

.get(options)

Given an id, returns a Promise with the status of the limit with the following structure:

  • total: max value.
  • remaining: number of calls left in current duration without decreasing current get.
  • reset: time since epoch in seconds that the rate limiting period will end (or already ended).

options

id

Type: string Default: this.id

The identifier to limit against (typically a user id).

max

Type: number
Default: this.max

The maximum number of requests within duration. If provided, it overrides the default max value. This is useful for custom limits that differ between IDs.

duration

Type: number
Default: this.duration

How long keep records of requests in milliseconds. If provided, it overrides the default duration value.

peek

Type: boolean
Default: false

When set to true, returns the current rate limit status without consuming a request. This is useful for checking the current rate limit status before deciding whether to proceed with an operation.

defineCommand

It provides the command definition so you can load it into any ioredis instance:

const Redis = require('ioredis')
const redis = new Redis(uri, {
  scripts: { ...require('async-ratelimiter').defineCommand }
})

Related

  • express-slow-down – Slow down repeated requests; use as an alternative (or addition) to express-rate-limit.

License

async-ratelimiter © microlink.io, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.

microlink.io · GitHub microlink.io · X @microlinkhq

更新履歴

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.6.1 (2025-07-02)

1.6.0 (2025-07-02)

Features

1.5.2 (2025-04-10)

1.5.1 (2025-04-10)

1.5.0 (2025-04-10)

Features

1.4.0 (2025-04-08)

Features

1.3.11 (2024-05-07)

1.3.10 (2024-04-09)

1.3.9 (2024-02-08)

1.3.8 (2024-01-12)

1.3.13 (2023-12-12)

1.3.12 (2023-10-23)

1.3.11 (2023-09-07)

1.3.10 (2023-09-05)

1.3.9 (2023-07-27)

1.3.8 (2023-05-13)

1.3.7 (2023-03-03)

1.3.6 (2023-02-14)

1.3.5 (2022-10-22)

1.3.4 (2022-09-30)

1.3.3 (2022-08-18)

1.3.2 (2022-08-18)

1.3.1 (2022-05-15)

1.3.0 (2021-07-17)

Bug Fixes

1.2.8 (2020-06-01)

Bug Fixes

1.2.7 (2019-07-11)

1.2.6 (2019-07-10)

Bug Fixes

Build System

1.2.5 (2019-06-19)

Bug Fixes

Build System

1.2.4 (2019-06-12)

Build System

1.2.3 (2019-04-21)

Bug Fixes

  • specify parse int radix (a8b29f0)

1.2.2 (2019-04-04)

Bug Fixes

  • typescript-declarations: missing decrease param at GetOptions interface (a22b9af)

1.2.1 (2019-03-27)

1.2.0 (2019-03-27)

1.1.4 (2019-03-27)

1.1.3 (2018-12-17)

1.1.2 (2018-07-23)

1.1.1 (2018-07-18)

1.1.0 (2018-07-16)

1.0.2 (2018-06-18)

1.0.1 (2018-06-17)

1.0.0 (2018-06-17)

1.1.2 (2018-07-23)

  • Closes #6 (6614d30), closes #6
  • old data test and delete the old ones by score (31beaed)

1.1.1 (2018-07-18)

  • Remove unnecessary check (f09bff9)

1.1.0 (2018-07-16)

  • allow custom duration in .get (f3edcf2)
  • allow custom limits (a94f501)
  • keep responsibility of tests (2fd9903)

1.0.2 (2018-06-18)

1.0.1 (2018-06-17)

1.0.0 (2018-06-17)