Skip to content

yamyn/RequestLimiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

onix

Simple request limiter that allows you to control the number of simultaneous request to any resource

Install

npm i request-limiter

Examples

Require module

const ReqLimiter = require('request-limiter').getInstance('myInstance', options);

ReqLimiter.getInstance(instance, options)

Get or create instance Returns: instance of ReqLimiter

Parameters
Name Type Description Required
instance string ReqLimiter instance`s name true
options object ReqLimiter configuration options false
options properties
Name Type Description default
maxAttempts number Maximum number of request attempts for one function 42
maxRepeatAttempts number Maximum number of attempts to request a request after receiving an error from the remote resource about exceeding the limit 6
checkDelay number The amount of time in millisecond after which the limiter will make a new attempt to add the request to the queue 1200
maxOneTimeReq number The number of request that can be executed simultaneously in one port 6
clientName string The name to be passed to the callback function in the reqWithCheck method client
limitErrorValidator function A simple function that checks whether an error sent from a remote resource belongs to an error exceeding the request limit returns a boolean error => error.code === 403
ReqLimitError Error Error class or classes that extend it. allows to give a custom error to the client ReqLimitError

ReqLimiter.haveConnect(port)

checks if a port exists and if there is cached data for it Returns: any

Name Type Description Required
port string name of the port you would like, of use true
const isConnect = ReqLimiter.haveConnect('myPort');

ReqLimiter.setClient(port, client)

set client data for your port Returns: void

Name Type Description Required
port string name of the port you would like, of use true
client any any information you want to saved that it will be passed as a parameter named <ReqLimiter. clientName> for the callback in the reqWithCheck method true
ReqLimiter.setClient('myPort', { accessToken: 'myAccessToken' });

ReqLimiter.reqWithCheck(port, cb, args, attempts)

Method that takes a function that needs to be queued for a given port

Returns: cb call results

Name Type Description Required
port string name of the port you would like, of use true
cb function callback function, that need add to port que true
args object arguments for callback function false
attempts number The number of attempts that have already been made, default 0 false
const fetchToImage = ({ client, id }) => {
    return myFetcher('url', { accessToken: client.accessToken, id });
};
ReqLimiter.reqWithCheck('myPort', fetchToImage, { id: 'myImgId' }).then((data) => {
    // console.log(data)
    // returns result to call fetchToImage
});

Documentation generated with doxdox.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published