0.2.1 • Published 5 years ago

dthrottle v0.2.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

dthrottle

Build Status Coverage Status

How it works

Rate limit the invocation of a function by ignore following invocations.

Example

const redis = require('redis')
const dthrottle = require('dthrottle')

function tested () {
  console.log(new Date().toISOString(), 'executing ...')
}

let test = dthrottle(tested, {
  wait: 1000,
  adapter: new dthrottle.Adapters.Redis({
    throttle: 2,
    redis: redis.createClient(),
    prefix: 'dthrottle:example'
  })
})

// even `test` invoked every 100ms, `tested` invoked every 1000ms
setInterval(() => {
  test()
}, 100)

Doc

dthrottle(func, opts)

  • func, the function to be ratelimited
  • opts.wait, invoke the func after opts.wait ms
  • opts.adapter, adapter to be used
  • opts.getId, the function to generate identify id to seprate invocations of func
  • opts.error, callback that will invoked when opts.adapter failed

Adapters

new dthrottle.Adapters.Memory(opts)

  • opts.expire, expire seconds for any locked identify id

new dthrottle.Adapters.Redis(opts)

  • opts.expire, expire seconds for any locked identify id
  • opts.redis, a redis client with Promise APIs
  • opts.prefix, add prefix for keys to be used in dthrottle

Add another Adapter

An adapter should have at least two methods: setnx and clear, both return a Promise.

keep the adapter.setnx atomic

On any invocation, An adapter should lock the identify id to ignore later invocations.

0.2.1

5 years ago

0.2.0

7 years ago

0.1.0

7 years ago