GJK
GJK

Reputation: 37369

Streaming large files causing server to hang

I have a feature in my web app that allows users to upload and download files. I serve up the app with Express, but the files are stored in a different server, so I proxy the requests to that server. Here's the proxy code using the request library:

module.exports = function(req, res) {
    req.headers['x-private-id'] = getId();

    var url = rewriteUrl(req.url);

    var newRequest = request(url, function(error) {
        if (error) console.log(error);
    });

    req.pipe(newRequest).on('response', function(res) {
        delete res.headers['x-private-id'];
    }).pipe(res);
};

This works fine for all of my requests, including downloading the file. However, I run into issues when 'streaming' the file. And by streaming, I mean I use fancybox to display the video using a video tag. The video displays fine the first few times.

But if I close fancybox and then reopen it enough times (5 specifically), it quits working after that; the video no longer shows up. The entire Express server seems to hang, unable to process any more requests. If I restart the server, everything is OK. To me it seems like the sockets from the proxy requests aren't being closed properly, but I can't figure out why. Is there something wrong with my proxy code?

Upvotes: 0

Views: 1145

Answers (1)

mscdex
mscdex

Reputation: 106696

You need to either increase the pool.maxSockets value passed in the request() config since it defaults to node's HTTP Agent's maxSockets which is 5, or opt out of connection pooling altogether with pool: false in the request() config.

Upvotes: 1

Related Questions