Async Ratelimiter

Rate limit made simple, easy, async.
Alternatives To Async Ratelimiter
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Aspnetcoreratelimit2,7971858a day ago20June 15, 2021150mitC#
ASP.NET Core rate limiting middleware
Node Rate Limiter Flexible2,38968812 months ago146September 25, 20229iscJavaScript
Count and limit requests by key with atomic increments in single process or distributed environment.
Bottleneck1,4197,1813153 months ago79August 03, 201975mitJavaScript
Job scheduler and rate limiter, supports Clustering
Webapithrottle1,1193323 years ago31July 31, 201745mitC#
ASP.NET Web API rate limiter for IIS and Owin hosting
Redis Cell1,078
24 days ago1July 31, 201811mitRust
A Redis module that provides rate limiting in Redis as a single command.
Flask Limiter953345323 days ago78June 07, 20221mitPython
Rate Limiting extension for Flask
Gubernator896
3 days ago83June 07, 20229apache-2.0Go
High Performance Rate Limiting MicroService and Library
Sptdataloader625
9 days ago4October 21, 20162apache-2.0Objective-C
The HTTP library used by the Spotify iOS client
Golimit589
2 years agoMay 31, 20211mitGo
Golimit is Uber ringpop based distributed and decentralized rate limiter
Redis_rate5733613 days ago33October 07, 202123bsd-2-clauseGo
Rate limiting for go-redis
Alternatives To Async Ratelimiter
Select To Compare


Alternative Project Comparisons
Readme
microlink

Last version Coverage Status NPM Status

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

Install

$ npm install async-ratelimiter --save

Usage

A simple middleware implementation for whatever HTTP server:

'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)
}

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.max

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

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 · Twitter @microlinkhq

Popular Rating Projects
Popular Rate Limiting Projects
Popular User Interface Components Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Javascript
Types
Rating
Rate Limiting