Last active
December 13, 2015 20:58
-
-
Save isaacs/4973669 to your computer and use it in GitHub Desktop.
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
var http = require('http'); | |
// good thing we're using the same actual buffer over and over, | |
// or else this would blow up. Can you imagine if it was real | |
// data, and not just the same chunk over and over again? | |
var chunk = new Buffer(65536); | |
var server = http.createServer(function(req, res) { | |
res.write('start it off...'); | |
// give the client time to kill the connection, | |
// then write a metric ton of junk. | |
setTimeout(write, 1000); | |
function write() { | |
// eat until full | |
while (false !== res.write(chunk)); | |
// eat some more! you're too skinny! | |
for (var i = 0; i < 64; i++) { | |
res.write(chunk); | |
} | |
// try again in 100ms | |
setTimeout(write, 100); | |
setTimeout(checkLeaky, 100); | |
} | |
function checkLeaky() { | |
var l = res.output.map(function(c) { | |
return c.length; | |
}).reduce(function(a, b) { | |
return a + b; | |
}); | |
console.error('leaking: %d', l); | |
if (l > 16 * 1024 * 1024) { | |
throw new Error('too much leaking!'); | |
} | |
} | |
}); | |
server.listen(1337, function() { | |
var req = http.request({ | |
port: 1337, | |
path: '/', | |
method: 'GET' | |
}); | |
req.on('response', function(res) { | |
console.error('kill connection'); | |
// srsly, I'm full, stop. | |
res.socket.destroy(); | |
res.on('end', function() { | |
console.error('got end'); | |
server.close(); | |
}); | |
}); | |
req.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
v0.8.19:
v0.8.20: