Skip to content

Instantly share code, notes, and snippets.

@myndzi
Last active August 29, 2015 14:00
Show Gist options
  • Save myndzi/11357417 to your computer and use it in GitHub Desktop.
Save myndzi/11357417 to your computer and use it in GitHub Desktop.
'use strict';
var Transform = require('stream').Transform,
inherits = require('util').inherits;
module.exports = ThrottleStream;
function ThrottleStream(opts) {
if (!(this instanceof ThrottleStream))
return new ThrottleStream(opts);
opts = opts || { };
Transform.call(this, opts);
this.penalty = (typeof opts.penalty === 'number' ? opts.penalty : 2) * 1000;
this.burst = ((typeof opts.burst === 'number' ? opts.burst : 5) - 1) * this.penalty;
this.time = 0;
}
inherits(ThrottleStream, Transform);
ThrottleStream.prototype._transform = function (chunk, encoding, cb) {
var now = Date.now(),
dly = this.time - now - this.burst;
if (dly > 0) {
setTimeout(this._transform.bind(this, chunk, encoding, cb), dly);
} else {
this.time = Math.max(this.time, now) + this.penalty;
cb(null, chunk);
}
};
@myndzi
Copy link
Author

myndzi commented Apr 28, 2014

If you have a rate in lines per second, set the penalty to seconds/(lines-burst). Ex: 20 lines in 30 seconds: penalty should be 30/15 or 0.5. This will result in the 20th chunk being emitted exactly 30 seconds from the 1st chunk even though the first five chunks were sent instantly. Subsequent chunks after 20 will actually come out slower, but if you give it a rest to 'catch its breath' it will come back towards 30 again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment