thunks/thunk-ratelimiter

Name: thunk-ratelimiter

Owner: thunks

Description: The fastest abstract rate limiter.

Created: 2015-06-13 03:08:36.0

Updated: 2018-04-03 12:24:02.0

Pushed: 2018-04-03 12:24:41.0

Homepage:

Size: 25

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

thunk-ratelimiter

The fastest abstract rate limiter.

NPM version Build Status Downloads

thunks
Implementations:
Requirements
Installation
install thunk-ratelimiter
Example

Example Connect middleware implementation limiting against a user._id:

t limiter = new Limiter()

ter.connect(redisClient) // connect to a thunk-redis instance
ter.get(req.user._id).then(function (limit) {
sponse.set('X-RateLimit-Limit', limit.total)
sponse.set('X-RateLimit-Remaining', limit.remaining)
sponse.set('X-RateLimit-Reset', Math.ceil(limit.reset / 1000))

 all good
bug('remaining %s/%s %s', limit.remaining, limit.total, id)
 (limit.remaining >= 0) return

 not good
t after = Math.ceil((limit.reset - Date.now()) / 1000)
sponse.set('Retry-After', after)
sponse.end(429, 'Rate limit exceeded, retry in ' + after + ' seconds')

API
new Limiter(options)

Return a limiter instance.

t limiter = new Limiter()
Limiter.prototype.connect([host, options]) => this
Limiter.prototype.connect(redisClient) => this

Connect to redis. Arguments are the same as thunk-redis's createClient, or give a thunk-redis instance.

ter.connect(6379)
Limiter.prototype.get(id, max, duration, max, duration, …)
Limiter.prototype.get([id, max, duration, max, duration, …])

Return a promise that guarantee a limiter result. it support more max and duration pairs ad limit policy. The first pairs will be used as default. If some trigger limit, then the limiter will apply the next pair policy.

ter.get('_userIdxxx').then(function (limit) {
nsole.log(limit)

s
ter.get('_userIdxxx:POST /files', 100, 60000, 50, 60000).then(function (limit) {
nsole.log(limit)

Result Object:

Limiter.prototype.remove(id)
ter.remove('_userIdxxx').then(function (res) {
nsole.log(err, res)


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.