Skip to content

Commit

Permalink
low/high buffer watermark
Browse files Browse the repository at this point in the history
  • Loading branch information
sidorares committed Aug 19, 2012
1 parent 582bc0c commit 23ac9c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ output:

parameters

shaper(byteRate, chunkRate, bufferLowWatermarkInSeconds, bufferHighWatermarkInSeconds)
shaper(byteRate, chunkRate, lowWatermark, highWatermark)

`byteRate` - targeted speed in bytes per second
`chunkRate` - output chunk rate. If target speed is 20000 bytes per second and chunk rate is 100, you'll have 100 chunks per second stream, each 200 bytes in size (on average). Note that if input stream is slower then target, chunks are sent immideately at input rate, wich could be higher than target chunk rate. If input is 1000 chunks per second, each 10 bytes `shape(20000, 500)` should give same 1000 chunk per second x 10 bytes stream.
`bufferLowWatermarkInSeconds` - if estimated time to empty buffer is less than this parameter, stream emits `drain` event
`bufferHighWatermarkInSeconds` - if estimated time to full buffer is less than this parameter, stream `write` returns false.
`chunkRate` - (default is 10) - output chunk rate. If target speed is 20000 bytes per second and chunk rate is 100, you'll have 100 chunks per second stream, each 200 bytes in size (on average). Note that if input stream is slower then target, chunks are sent immideately at input rate, wich could be higher than target chunk rate. If input is 1000 chunks per second, each 10 bytes `shape(20000, 500)` should give same 1000 chunk per second x 10 bytes stream.
`lowWatermark` - emit `drain` if buffer size length less than this paremeter. Default to 0.
`highWatermark` - size buffer considered full. Default to 0.


[nt]: https://github.com/TooTallNate/node-throttle
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ var assert = require('assert');


// creates new shaped trough stream
var ShapeStream = function (byteRate, chunkRate) {
var ShapeStream = function (byteRate, chunkRate, lowWatermark, highWatermark) {
this.rate = byteRate; // float number!
if (!chunkRate)
chunkRate = 10;
if (!lowWatermark)
lowWatermark = 0;
if (!highWatermark)
highWatermark = 0;

this.checkInterval = 1000/chunkRate;
this.buffer = buffers();
this.startTime = Date.now();
Expand Down Expand Up @@ -56,11 +61,12 @@ ShapeStream.prototype.processBuffer = function() {
var chunk = this.buffer.splice(0, lengthToWrite);
this.doWrite(chunk.toBuffer());
}
if (buffer.lengh <= this.lowWatermark && !this.stopping)
this.emit('drain');

if (this.buffer.length == 0) {
if (this.stopping)
this.emit('close');
else
this.emit('drain');
} else
this.sendTimer = setTimeout(this.processBuffer.bind(this), this.checkInterval);
}
Expand All @@ -83,7 +89,7 @@ ShapeStream.prototype.write = function(chunk, encoding) {
this.buffer.push(chunk);
if (!this.paused)
this.processBuffer();
return false;
return this.buffer.length < this.highWatermark;
};

ShapeStream.prototype.end = function(chunk, encoding) {
Expand Down

0 comments on commit 23ac9c9

Please sign in to comment.