1.0.0 • Published 3 years ago

invocation-throttler v1.0.0

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
3 years ago

README

Helper for throttling function or method invocation with built-in optional exponential back-off. Depends on:

Installation

npm install @mavenmachines/invocation-throttler --save

Usage

In an example bellow the function callThirdPartyAPI should be throttled. InvocationThrottler is configured to pass not more than 5 executions of a given function in a second. Each second it refreshes. If callThirdPartyAPI is passed to the invocationThrottler's invoke method but a requests limit is already exhausted - it will wait until the limit will be refreshed. If an execution of callThirdPartyAPI function failed - InvocationThrottler will perform retries if a retriesNumber option is provided.

const { InvocationThrottler, INTERVALS } = require('@mavenmachines/invocation-throttler');

const invocationThrottler = new InvocationThrottler({
    tokensPerInterval: 5, // how many executions are allowed in given interval
    interval: INTERVALS.SECOND, // second|minute|hour|day
    retriesNumber: 3 // number of retryies for failed executions
});

function callThirdPartyAPI(a, b) {
    // implementation
}
const result = await invocationThrottler.invoke(callThirdPartyAPI.bind(undefined, 'foo', 'bar'))