Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created April 18, 2015 17:03
Show Gist options
  • Save bennadel/eb1bf6b6d0fc5fe31586 to your computer and use it in GitHub Desktop.
Save bennadel/eb1bf6b6d0fc5fe31586 to your computer and use it in GitHub Desktop.
The Affect Of Back-Pressure When Piping Data Into Multiple Writable Streams In Node.js
// Require node modules.
var http = require( "http" );
var fileSystem = require( "fs" );
var stream = require( "stream" );
// Create an instance of our http server.
var httpServer = http.createServer(
function handleRequest( request, response ) {
// Create our file-input stream.
var hankhill = fileSystem.createReadStream( "./hank-hill.png" );
// Pipe it into a simple transform stream.
// --
// NOTE: Our transform stream is not being piped into anything else. However,
// this time, the highWaterMark is sufficiently high to buffer the entire file
// before causing back-pressure. As such, the source stream never needs to be
// paused.
hankhill.pipe(
new stream.PassThrough({
highWaterMark: ( 200 * 1024 )
})
);
// ALSO pipe it into the response stream.
hankhill.pipe( response );
}
);
httpServer.listen( 8080 );
console.log( "Server running on port 8080" );
// Require node modules.
var http = require( "http" );
var fileSystem = require( "fs" );
var stream = require( "stream" );
// Create an instance of our http server.
var httpServer = http.createServer(
function handleRequest( request, response ) {
// Create our file-input stream.
var hankhill = fileSystem.createReadStream( "./hank-hill.png" );
// Pipe it into a simple transform stream.
// --
// NOTE: Our transform stream is not being piped into anything else. As such,
// its internal buffers MAY fill up (depending on the highWaterMark setting and
// the size of the file being streamed), creating backpressure and subsequently
// pausing the file-input stream.
hankhill.pipe( new stream.PassThrough() );
// ALSO pipe it into the response stream.
hankhill.pipe( response );
}
);
httpServer.listen( 8080 );
console.log( "Server running on port 8080" );
// Require node modules.
var http = require( "http" );
var fileSystem = require( "fs" );
var stream = require( "stream" );
// Create an instance of our http server.
var httpServer = http.createServer(
function handleRequest( request, response ) {
// Create our file-input stream.
var hankhill = fileSystem.createReadStream( "./hank-hill.png" );
// Pipe it into a simple transform stream.
// --
// NOTE: This time, our passthrough / transform stream is piping into a file-
// output stream. As such, the passthrough buffers can drain and the transform
// stream doesn't have to pause the file-input stream (at least not indefinitely).
hankhill.pipe(
new stream.PassThrough().pipe(
fileSystem.createWriteStream( "./copy.png" )
)
);
// ALSO pipe it into the response stream.
hankhill.pipe( response );
}
);
httpServer.listen( 8080 );
console.log( "Server running on port 8080" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment