2.0.3 • Published 4 years ago

@ya_myn/request-limiter v2.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

request-limiter 2.0.0

onix

Simple request limiter that allows you to control the number of simultaneous request to any resource with cashe (use 'node-cashe')

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
NameTypeDescriptionRequired
instancestringReqLimiter instance`s name |true
optionsobjectReqLimiter configuration optionsfalse
options properties
NameTypeDescriptiondefault
maxAttemptsnumberMaximum number of request attempts for one function42
maxRepeatAttemptsnumberMaximum number of attempts to request a request after receiving an error from the remote resource about exceeding the limit6
checkDelaynumberThe amount of time in millisecond after which the limiter will make a new attempt to add the request to the queue1200
maxOneTimeReqnumberThe number of request that can be executed simultaneously in one port6
clientNamestringThe name to be passed to the callback function in the reqWithCheck methodclient
limitErrorValidatorfunctionA simple function that checks whether an error sent from a remote resource belongs to an error exceeding the request limit returns a booleanerror => error.code === 403
ReqLimitErrorErrorError class or classes that extend it. allows to give a custom error to the clientReqLimitError

ReqLimiter.haveConnect(port)

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

NameTypeDescriptionRequired
portstringname of the port you would like, of usetrue
const isConnect = ReqLimiter.haveConnect('myPort');

ReqLimiter.setClient(port, client)

set client data for your port Returns: void

NameTypeDescriptionRequired
portstringname of the port you would like, of usetrue
clientanyany information you want to saved that it will be passed as a parameter named <ReqLimiter. clientName> for the callback in the reqWithCheck methodtrue
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

NameTypeDescriptionRequired
portstringname of the port you would like, of usetrue
cbfunctioncallback function, that need add to port quetrue
argsobjectarguments for callback functionfalse
attemptsnumberThe number of attempts that have already been made, default 0false
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.

2.0.3

4 years ago

2.0.2

4 years ago

1.5.1

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.5.0

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.2.0

4 years ago

1.3.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago