Last active
August 29, 2015 14:00
-
-
Save myndzi/11357417 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.