rate-ping-pong
v1.0.1 rate limiting asynchronous scale scalability
Rate Ping
Maintain zippy user experience while constantly adjusting to various API usage restrictions.
- Install
npm install rate-ping-pong
- Setup
const RatePingPong = require('rate-ping-pong')
const limiter = new RatePingPong(function highUsage () {
// HIGH USAGE METHOD
}, {
timer: 0,
minimum: 0,
maximum: null,
increment: 100
})
- Usage
- run(args) -> Promise
Call the highUsage
method with supplied arguments, only allow 1 function execution per timer interval
Returns a Promise which resolves after function execution
limiter.run('a') // Called in 100ms returns Promise.resolve('a')
limiter.run('b') // Called in 200ms returns Promise.resolve('b')
limiter.run('c') // Called in 300ms returns Promise.resolve('c')
- setTimer(Number) -> undefined
Changes the rate limiting logic to execute once every 1000 milliseconds
limiter.setTimer(1000)
- incrementTimer(Number) -> undefined
Increment the timer by the inc option, do not increase greater than max if supplied
limiter.incrementTimer()
- decrementTimer(Number) -> undefined
Decrement the timer by the inc option, do not decrease below the min
limiter.decrementTimer()
- resetTimer() -> undefined
Reset the timer to the originally supplied value
limiter.resetTimer()