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

@brugmanjoost/throttle

v1.0.0

Published

A nodejs module to throttle events at a preset rate.

Downloads

2

Readme

throttle

Throttle is a nodejs module used to throttle events. You can typically use it to prevent overloading external services. Throttle makes sure your code puts on the breaks in time. There are no external dependencies.

  1. Installation
  2. Usage 2.1 Synchronised usage 2.2 Async usage 2.3 Automatic catch up bursts 2.3 Excess bursts

1. Installation

$ npm install @brugmanjoost/throttle

2. Usage

You provide a normalRateLimit which is the number of events that may occur in a given window and you provide normalRateWindow which is the duration of that window. Throttle will make sure you don't exceed that limit.

2.1 Synchronised usage

You'll likely use throttle in a larger context like a loop to process data but barebones this is how you use it:

let throttle = new throttler.Throttle({
    normalRateLimit: 5 * 60 * 60,               // 5 per second = 5 * 60 * 60 per hour
    normalRateWindow: 1000 * 60 * 60,           // 1000ms * 60 * 60 is one hour
});

let t = Date.now();
await throttle.next(); console.log(Date.now() - t); // Approx t+0ms
await throttle.next(); console.log(Date.now() - t); // Approx t+200ms
await throttle.next(); console.log(Date.now() - t); // Approx t+400ms
await throttle.next(); console.log(Date.now() - t); // Approx t+600ms
await throttle.next(); console.log(Date.now() - t); // Approx t+800ms
console.log('Done', Date.now() - t);                // Approx t+800ms, shown in console last

2.2 Async usage

If you don't use await, then throttle.next() will return immediately and the main program continues but the promises will still complete at the same rate:

let t = Date.now();
throttle.next().then(() => console.log(Date.now() - t)); // Approx t+0ms
throttle.next().then(() => console.log(Date.now() - t)); // Approx t+200ms
throttle.next().then(() => console.log(Date.now() - t)); // Approx t+400ms
throttle.next().then(() => console.log(Date.now() - t)); // Approx t+600ms
throttle.next().then(() => console.log(Date.now() - t)); // Approx t+800ms
console.log('Done', Date.now() - t);                     // Approx t+0ms, shown in console first

2.3 Automatic catch up bursts

If your main program has some delay, e.g. fetching data, since sending the first request, then throttle will use that time to schedule the next events in burst mode to 'catch up' to the normal rate of the throttle.

let t = Date.now();
await throttle.next(); console.log(Date.now() - t);     // Approx t+0ms
await throttle.next(); console.log(Date.now() - t);     // Approx t+200ms
await throttle.next(); console.log(Date.now() - t);     // Approx t+400ms
await throttle.next(); console.log(Date.now() - t);     // Approx t+600ms
await throttle.next(); console.log(Date.now() - t);     // Approx t+800ms

setTimeout(async () => {                                // Main program does something that takes 1 second.
    console.log('Staring burst', Date.now() - t);       // Approx t+18000ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+18000ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+18000ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+18000ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+18000ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+18000ms

    console.log('Burst completed', Date.now() - t);     // Approx t+20000ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+20000ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+2200ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+24000ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+26000ms
    await throttle.next(); console.log(Date.now() - t); // Approx t+28000ms

}, 1000);

Room for catch up bursts is calculated based on the past events that occurred in the past window. E.g. with a rate of 3600 events over 1 hour if you generate a single event in the first second and no further events, then in the last second of that hour throttle will have accumulated room for 3599 catch up burst events yet to complete within the last second of the window. As this may exceed expectations of the service you are using it is often usefull to recalculate the rate to a smaller fraction of window that is specified by the external service.

2.4 Excess bursts

Many services allow for brief periods of excess bursts beyond the normal service rate. In many cases your provider may consider catch up bursts as excess bursts. Please check the conditions of the service you are using.

Another aspect of excess burst is that they can usually be initiated right away.To implement that would mean that the first number of calls to next() (e.g. 10, 50, 100) would return immediately as an excess burst only to be followed by delays imposed by the normal rate. To implement this throttle would have to do more examination on the timeline of events and this is currently not implemented.