I'm in a situation where I'm serving a handful of 1MB static files to 
clients.  We have our logic that determines which file to serve in node, so 
putting nginx or varnish in front of our node app isn't a great solution. 
Our first naive attempt was to load the entire file into a Buffer and cache 
each one for 60 seconds, and when a client connected we would just 
res.end(buffer).

This worked fine up until yesterday, when a small number of slow clients 
caused our app to fall over due to what I think was the kernel send buffer 
filling up.  I looked around for a buffer-to-stream implementation that 
would be able to read a file off the disk in a single shot into a buffer, 
then repeatedly stream it to later clients, but I couldn't find anything. 
 Here's what I ended up writing:

https://gist.github.com/3866612

For the first client who requests the file, I call 
file.pipe(cachedStream).pipe(response), then store cachedStream in memory. 
 For later clients, I can call cachedStream.pipeCached(response);

It seems to work but is (acceptably) slower than the naive implementation 
for fast clients.  My questions are: is there a reason none of the current 
static file implementations use something like this? Are there obvious 
problems with it? And is there an optimal bufferSize setting for sending to 
web clients, or is it solely based on the speed of the clients?

Thanks,
Michael

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to