0.2.1 • Published 7 years ago
dthrottle v0.2.1
dthrottle
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 ratelimitedopts.wait, invoke thefuncafteropts.waitmsopts.adapter, adapter to be usedopts.getId, the function to generateidentify idto seprate invocations offuncopts.error, callback that will invoked whenopts.adapterfailed
Adapters
new dthrottle.Adapters.Memory(opts)
opts.expire, expire seconds for any lockedidentify id
new dthrottle.Adapters.Redis(opts)
opts.expire, expire seconds for any lockedidentify idopts.redis, a redis client with Promise APIsopts.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.