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 the func
after opts.wait
msopts.adapter
, adapter to be usedopts.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 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.