From 015daa81f26afc59d1da857b8bbedfb80eb532b1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 20 Jul 2010 05:18:48 +0000 Subject: event_machine: avoid redundant open() for static files This is cheaper for serving static files and only slightly more expensive for pipes and sockets (extra path lookup for File.stat). --- lib/rainbows/event_machine.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'lib/rainbows/event_machine.rb') diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb index 8de02ea..4402c72 100644 --- a/lib/rainbows/event_machine.rb +++ b/lib/rainbows/event_machine.rb @@ -115,8 +115,7 @@ module Rainbows headers[CONNECTION] = CLOSE if headers alive = true elsif body.respond_to?(:to_path) - io = body_to_io(body) - st = io.stat + st = File.stat(path = body.to_path) if st.file? cb = lambda do @@ -124,11 +123,11 @@ module Rainbows quit unless alive end write(response_header(status, headers)) if headers - io.close - @body = stream = stream_file_data(body.to_path) + @body = stream = stream_file_data(path) stream.errback(&cb) return stream.callback(&cb) elsif st.socket? || st.pipe? + io = body_to_io(body) chunk = stream_response_headers(status, headers) if headers m = chunk ? ResponseChunkPipe : ResponsePipe return EM.watch(io, m, self, alive, body).notify_readable = true -- cgit v1.2.3-24-ge0c7