rate-ping-pong

v1.0.0
Rate limit methods and adjust behavior according to current usage rates
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
  1. 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')
  1. setTimer(Number) -> undefined

Changes the rate limiting logic to execute once every 1000 milliseconds

limiter.setTimer(1000)
  1. incrementTimer(Number) -> undefined

Increment the timer by the inc option, do not increase greater than max if supplied

limiter.incrementTimer()
  1. decrementTimer(Number) -> undefined

Decrement the timer by the inc option, do not decrease below the min

limiter.decrementTimer()
  1. resetTimer() -> undefined

Reset the timer to the originally supplied value

limiter.resetTimer()

Metadata

  • ISC
  • Whatever
  • Ryan P. Hansen
  • released 1/31/2018

Downloads

Maintainers