https://github.com/mixmaxhq/node-distributed-throttle-function
Distributed throttle() function (like Underscore) that uses Redis
https://github.com/mixmaxhq/node-distributed-throttle-function
corgi-tag
Last synced: 19 days ago
JSON representation
Distributed throttle() function (like Underscore) that uses Redis
- Host: GitHub
- URL: https://github.com/mixmaxhq/node-distributed-throttle-function
- Owner: mixmaxhq
- License: mit
- Created: 2016-04-11T21:37:54.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-11-28T16:11:10.000Z (over 1 year ago)
- Last Synced: 2024-10-11T09:12:27.945Z (7 months ago)
- Topics: corgi-tag
- Language: JavaScript
- Homepage: https://www.mixmax.com/careers
- Size: 75.2 KB
- Stars: 6
- Watchers: 33
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Distributed Throttle
This module allows you to throttle the invocation of a function (just like [Underscore#throttle](http://underscorejs.org/#throttle)) across a distributed system. It is built on top of Redis. It will currently always call the function on the leading edge and the trailing edge, just like Underscore's default implementation.
## Installing
```bash
npm install node-distributed-throttle-function
```## Using
```js
var distributedThrottle = require('node-distributed-throttle-function');
var redis = require('redis');var port = '6381';
var hostname = 'localhost';
var password = '';var redisConnection = redis.createClient(port, hostname, {
auth_pass: password
};var throttle = distributedThrottle(redisConnection);
// This function will be called immediately unless it has been called less than 10 seconds ago.
// In that case, it will be called in 10 seconds—unless some _other_ call has happened in that interval.
throttle.call('my10secThrottledFunction', function(err) {
if (err) {
console.log('error!', err);
return;
}console.log('This function will be called at most once (possibly zero times) within the next 10 seconds.');
}, 10 * 1000 /* 10 sec */);// If you want to cancel any pending call such that the next invocation will fire immediately.
throttle.cancel('my10secThrottledFunction', function(err){
if (err) {
console.log('error!', err);
}
});```
## Bugs & TODOs
See