Skip to content

Instantly share code, notes, and snippets.

@shuhei
Last active April 1, 2017 13:51
Show Gist options
  • Save shuhei/06d585e40fe1d1536c1d20522c160c61 to your computer and use it in GitHub Desktop.
Save shuhei/06d585e40fe1d1536c1d20522c160c61 to your computer and use it in GitHub Desktop.
Check whether memory leaks on a stream error

Result

There were not memory leaks detected for:

  • error on the unzip stream
  • error on the request object

Couldn't get error on the response object. When the connection is destroyed, request object emits an error.

How to

Run servers:

npm install -S express comporession
node --inspect proxy.js
node server.js

Error on unzip stream:

ab -n 10000 -c 10 http://localhost:8080/fake
# Take snapshot
ab -n 10000 -c 10 http://localhost:8080/fake
# Take snapshot

Error on connection:

ab -n 10000 -c 10 http://localhost:8080/error
# Take snapshot
ab -n 10000 -c 10 http://localhost:8080/error
# Take snapshot
const zlib = require('zlib');
const stream = require('stream');
const http = require('http');
http.createServer((req, res) => {
const request = http.request({
hostname: 'localhost',
port: 8081,
path: req.url,
method: 'GET',
headers: {
'accept-encoding': 'gzip, deflate'
}
}, (response) => {
let bodyStream = response;
const encoding = response.headers['content-encoding'];
console.log(req.url, encoding);
if (encoding === 'gzip' || encoding === 'deflate') {
response.on('error', (e) => {
res.writeHead(500);
res.end('response error: ' + e.message);
});
bodyStream = response.pipe(zlib.createUnzip());
}
const chunks = [];
bodyStream.on('error', (e) => {
res.writeHead(500);
res.end('body stream error: ' + e.message);
});
bodyStream.on('data', (chunk) => {
chunks.push(chunk);
});
bodyStream.on('end', () => {
res.end(Buffer.concat(chunks).toString('utf8'));
});
if (req.url === '/error') {
response.destroy(new Error('destroy connection'));
}
});
request.end();
request.on('error', (e) => {
res.writeHead(500);
res.end('request error: ' + e.message);
});
}).listen(8080);
const express = require('express');
const compression = require('compression');
const app = express();
app.use(compression({
filter(req, res) {
return req.url !== '/fake';
}
}));
app.get('/', (req, res) => {
for (let i = 0; i < 100; i++) {
res.write('hello');
}
res.end();
});
app.get('/fake', (req, res) => {
res.set('content-encoding', 'gzip');
for (let i = 0; i < 100; i++) {
res.write('hello');
}
res.end();
});
app.get('/error', (req, res) => {
for (let i = 0; i < 100; i++) {
res.write('hello');
}
res.end();
});
app.listen(8081);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment