fs.createReadStream on http cluster suspends requests without throwing events

28 views
Skip to first unread message

nik600 AT 10^100

unread,
Nov 20, 2014, 6:43:11 AM11/20/14
to nod...@googlegroups.com
Dear all

i've got an http cluster based on nodejs, basically this component does the following:

- does some regex matching on the req.url
- case A) -> forward the request to a back-end
- case B) -> check if exists a local file
- case B.KO) -> forward the request to a back-end
- case B.OK) -> pipe the local file to the request

this component handles about 100 req/sec, but i've noticed that an average of 2/1000 requests in case B.OK doesn't close and remains in a undefined state without throwing events

This is the code:

kcache.stats.totalcounter++;
var readStream = fs.createReadStream(file_path_data);

readStream.on('error', function(err) {
kcache.stats.errorscounter++;
});

readStream.on('end', function(err) {
kcache.stats.completedcounter++;
});

readStream.on('open', function () {

if (acceptEncoding.match(/\bdeflate\b/)) {
   res.setHeader('content-encoding', 'deflate');
   return readStream.pipe(zlib.createDeflate()).pipe(res);    
} else if (acceptEncoding.match(/\bgzip\b/)) {
   res.setHeader('content-encoding', 'gzip');
   return readStream.pipe(zlib.createGzip()).pipe(res);    
} else {
 return readStream.pipe(res);
}
});

After some minutes of working, i expect that the "processing" requests should be 

kcache.stats.totalcounter - (kcache.stats.errorscounter + kcache.stats.completedcounter)

But this number continues to grow, seeming that the readStream object doesn't throw any error/end event.

Maybe i should set up a timeout or an inactivity event ? is possible to do that?

Thanks
Reply all
Reply to author
Forward
0 new messages