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

dynamic-throttle

v1.0.1

Published

Redis-backed dynamic rate limiting

Downloads

5

Readme

Dynamically set application rate limits

There are many rate limiters out there and almost all work as follows:

Step One: Initialization. Here, you set a rate Limit

Step Two: Rate Limiting. Then you start consuming tokens with every call.

While this works, it means that you cannot change rate limits after initialization. You cannot build an app that dynamically sets its own rate limits depending of traffic as well as other parameters.

While this is scenario is difficult to encounter, we happened to build an app that monitors peoples browsing habits and dynamically sets rate limits. In other words, we influential users to drive a bigger traffic torrent to our app than other users.

Usage

First, yarn add dynamic-throttle


const Throttle = require('dynamic-throttle'),
    //initialize
    throttler = Throttle({/*options*/}),
    //key used to throttle hits
    key = 'test key',
    //how many tokens per window
    tokens = 500,
    //duration before ones tokens are renewed
    window = 'minute'; 
    

  throttler.throttle(key, tokens, window)
        .then(function (resp) {
            //Execute your code here
        })
        .catch(function (resp) {
            //Rate Limit exceeded. Stop right here
        })

Initialization

The first step is to initialize your rate limiter. Unlike other limiters, this process exists mostly to help you properly initialize your Redis instance.

    Throttle({        
        redisClient :  redis.createClient(),
        maxAbuseRate : 5 //default = 5
    })
  • redisClient: a Redis instance defaults to a Redis client initialized with: {host: '127.0.0.1', port: 6379, db: 2}
  • maxAbuseRate: the rate past which users who exceed rate limits become temporarily banned from the system. If the window was set to 1 minute then users who continually abuse the system past the maxAbuseRate are banned/rate limited for window duration * 10 * abuseRate

Responses

Both then(fn) and catch(fn) functions return responses as shown below:

.then(fn)

When we still have tokens...

{ 
    message: "You are within your rate limit.",
    expiry: "2018-05-30T11:09:31.169Z",
    tokensRemaining: 485 
}

.catch(fn)

When the user has exceeded their limit...

{ 
    message: "You have exceeded your rate limit! Please stop abusing this system or get banned for longer!",
    tokensExceeded: -14,
    abuseRate: 1.4,
    bannedTill: "2018-05-30T11:28:04.432Z" 
}

API

.throttle(key, tokens, window)

This is the main method, and likely the only you will use.

It checks or sets new token sessions and ensures tokens are consumed with each call. By having all this functionality in one function, we enable you to dynamically call only this one function for all your throttling needs.

It is a setter, getter & token consumer all in one!

.reset(key, tokens, window)

Use this function if you need to immediately (hard) reset tokens for any key.

NOTE: If you dynamically set new token values, the new value is only applied in the next window session. This is because we have to wait for key expiration from redis in order to set the new values.

However, if you would like the changes to apply immediately, use .reset().

.quit()

Quit the Redis session and close all connections. Normally, you would only call this if exiting application.