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

rate-inhibitor

v1.7.1

Published

Tool to Rate Limit Function Calls

Downloads

12

Readme

Rate Inhibitor

Simple library to show time, in ms, until nexted call allowed.

import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhib(10,1000);

//first 10 calls, no limit
for(let i =0; i<10; i++){
    console.log(inhib.getNext({})); //  0ms
}

// next 10, limit applied
for(let i =0; i<10; i++){
    console.log(inhib.getNext({})); //  ~1000ms
}

//3 seconds pass so no need for limit
setTimeout(()=>{
for(let i =0; i<20; i++){
    console.log(inhib.getNext({})); //  1st 10 will be 0ms, next 10 will be ~1000ms
}
}, 3000);

Instalation

NPM

npm install rate-inhibitor

YARN

yarn add rate-inhibitor

Methods

getNext

returns MS till next call allowed. getNext(offSet, queueReset, timeFrameChange, callsPerChange)

import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhib(10,1000);

//first 10 calls, no limit
for(let i =0; i<10; i++){
    console.log(inhib.getNext({})); //  0ms
}

// next 10, limit applied
for(let i =0; i<10; i++){
    console.log(inhib.getNext({})); //  ~1000ms
}

//3 seconds pass so no need for limit
setTimeout(()=>{
for(let i =0; i<20; i++){
    console.log(inhib.getNext({})); //  1st 10 will be 0ms, next 10 will be ~1000ms
}
}, 3000);

using offSet

import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhib(10,1000);
let offSet = 2000;
 console.log(inhib.getNext({offSet}));
//first 10 calls, no limit
for(let i =0; i<9; i++){
    console.log(inhib.getNext({})); //  2000ms
}


// next 10, limit applied
for(let i =0; i<9; i++){
    console.log(inhib.getNext({})); //  ~3000ms
}

using queueReset

import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhib(10,1000);
let queueReset = true;
 console.log(inhib.getNext({}));
//first 10 calls, no limit
for(let i =0; i<9; i++){
    console.log(inhib.getNext({})); //  0ms
}


// next 10, limit applied
for(let i =0; i<9; i++){
    console.log(inhib.getNext({queueReset})); //  ~0ms
}

using timeFrameChange

import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhib(10,1000);
let timeFrameChange = 1000;
 console.log(inhib.getNext({timeFrameChange}));
//first 10 calls, no limit
for(let i =0; i<9; i++){
    console.log(inhib.getNext({})); //  0ms
}


// next 10, limit applied
for(let i =0; i<9; i++){
    console.log(inhib.getNext({})); //  ~2000ms
}

using callsPerChange

import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhib(10,1000);
let callsPerChange = -9;
 console.log(inhib.getNext({callsPerChange})) //~0ms;
//first 10 calls, no limit
console.log(inhib.getNext({callsPerChange})) //~1000ms;

inhibitFunc

Modifies a function to execute with inhibitor applied. Note that only execution start is guaranteed, completion is depend on the function. inhibitor.inhibitFunc(function, this) => modifiedFunction;

import * as needle from 'needle';
import RateInhibitor from 'rate-inhibitor';
let get = inhib.inhibitFunc(needle.get, needle);


//calls will execute 500 ms appart.
//Due to network conditions call 2 filles call back first;
for (let i =0; i< 2){
    needle.get('http://www.yourapi.com/' + i, function(error, response) {
        if (!error && response.statusCode == 200)
            console.log(response.body);
    });
}

inhibitCB

Modifies a node style callback into a promise function to execute with inhibitor applied. Note that only execution start is guaranteed, completion is depend on the function. inhibitor.inhibitCB(function, this) => modifiedFunction => promise;

import * as needle from 'needle';
import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhibitor(1, 500);
let get = inhib.inhibitCB(needle.get, needle);


//calls will execute 500 ms appart.
//Due to network conditions call 2 filles call back first;
for (let i =0; i< 2){
    get('http://www.yourapi.com/' + i)
    .then(resp => console.log(resp.body))
   .catch( e => console.log(e));
}

inhibitPromise

Modifies a promise into a promise function to execute with inhibitor applied. Note that only execution start is guaranteed, completion is depend on the function. inhibitor.inhibitPromise(function, this) => modifiedFunction => promise;

let got = require('got');
import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhibitor(1, 500);
let get = inhib.inhibitPromise(got);

//calls will execute ~500 ms appart.
//Due to network conditions call 2 filles call back first;
for (let i =0; i< 2){
    get('http://www.yourapi.com/' + i)
    .then(resp => console.log(resp.body))
   .catch( e => console.log(e));
}

Usecases

This library is designed to provide the user a number in ms to the next allowed call

Calling Rest API

Api only allows 10 calls every second.

let needle = require('needle');
import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhibitor(1, 1000);

setTimeout(() => {
    needle.get('http://www.yourapi.com/id', function(error, response) {
        if (!error && response.statusCode == 200)
            console.log(response.body);
    });
}, inhib.getNext({}));

Spreading out calls to a DB

1 call every 200 ms

let needle = require('needle');
import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhibitor(1, 200);

setTimeout(() => {
DBModel.find({}, function (err, docs) {
    console.log(docs);
});
}, inhib.getNext({}));

Limiting a promise

let needle = require('needle');
import RateInhibitor from 'rate-inhibitor';
let inhib = new RateInhibitor(1, 200);

function makeMeAPromise() {
    return new Promise(function(resolve, reject) {
        setTimeout(() => {
            DBModel.find({}, function(err, docs) {
                if (err) {
                    reject(err);
                } else {
                    resolve(docs);
                }
            });
        }, inhib.getNext({}));
    });
}

ToDo

  • Reset Inhibit
  • Inflate/Change Limits on fly