rate-inhibitor

1.7.1 • Public • Published

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

Package Sidebar

Install

npm i rate-inhibitor

Weekly Downloads

0

Version

1.7.1

License

MIT

Last publish

Collaborators

  • mzelmanovich