Last active
December 10, 2015 14:49
-
-
Save jessetane/4450228 to your computer and use it in GitHub Desktop.
excessive memory usage on large incoming http request?
This file contains 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
// run this locally: node <gist.js> | |
// then POST a big (>300mb) file to it with cURL: | |
// curl -X POST -d @"/path/to/big.file" localhost:8080 | |
var http = require('http') | |
var PORT = 8080 | |
var peak = 0 | |
// watch mem usage | |
setInterval(function () { | |
peak = Math.max(peak, process.memoryUsage().rss) | |
console.log('memory usage ' + (peak / 1000000).toFixed(2) + 'mb') | |
}, 500) | |
// http server | |
http.createServer(function (req, res) { | |
// create a destination | |
var dest = http.request({ | |
port: 8080, | |
method: 'POST', | |
hostname: 'remote.server.com' // a running instance of https://gist.github.com/4449811 | |
}, function (resp) { | |
resp.pipe(process.stdout) | |
}) | |
dest.on('finish', function () { | |
console.log('request ended') | |
res.end('all done\n') | |
}) | |
// pipe incoming data to dest | |
req.pipe(dest) | |
}).listen(PORT, function () { | |
console.log('server listening on ' + PORT) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment