From c38bc88363d4c4f5f639540ca86f4dd1225df79e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 30 Dec 2010 08:32:26 +0000 Subject: refactor response sendfile body handling for easier debugging Unique method names makes it easier to follow code and determine where our methods come from. --- lib/rainbows/fiber/body.rb | 3 ++- lib/rainbows/response/body.rb | 25 ++++++++++--------------- lib/rainbows/revactor/body.rb | 3 ++- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/rainbows/fiber/body.rb b/lib/rainbows/fiber/body.rb index 29926c6..1d7d325 100644 --- a/lib/rainbows/fiber/body.rb +++ b/lib/rainbows/fiber/body.rb @@ -12,7 +12,7 @@ module Rainbows::Fiber::Body # :nodoc: # the sendfile 1.0.0+ gem includes IO#sendfile_nonblock if IO.method_defined?(:sendfile_nonblock) - def write_body_file(client, body, range) + def write_body_file_sendfile_fiber(client, body, range) sock, n, body = client.to_io, nil, body_to_io(body) offset, count = range ? range : [ 0, body.stat.size ] begin @@ -26,6 +26,7 @@ module Rainbows::Fiber::Body # :nodoc: ensure close_if_private(body) end + ALIASES[:write_body_file] = :write_body_file_sendfile_fiber else ALIASES[:write_body] = :write_body_each end diff --git a/lib/rainbows/response/body.rb b/lib/rainbows/response/body.rb index e80217d..a5d04dd 100644 --- a/lib/rainbows/response/body.rb +++ b/lib/rainbows/response/body.rb @@ -58,26 +58,28 @@ module Rainbows::Response::Body # :nodoc: end if IO.method_defined?(:sendfile_nonblock) - def write_body_file(sock, body, range) + def write_body_file_sendfile(sock, body, range) io = body_to_io(body) range ? sock.sendfile(io, range[0], range[1]) : sock.sendfile(io, 0) ensure close_if_private(io) end + ALIASES[:write_body_file] = :write_body_file_sendfile end if IO.respond_to?(:copy_stream) - unless method_defined?(:write_body_file) + unless method_defined?(:write_body_file_sendfile) # try to use sendfile() via IO.copy_stream, otherwise pread()+write() - def write_body_file(sock, body, range) + def write_body_file_copy_stream(sock, body, range) range ? IO.copy_stream(body, sock, range[1], range[0]) : IO.copy_stream(body, sock, nil, 0) end + ALIASES[:write_body_file] = :write_body_file_copy_stream end # only used when body is a pipe or socket that can't handle # pread() semantics - def write_body_stream(sock, body, range) + def write_body_stream(sock, body) IO.copy_stream(body, sock) end else @@ -85,18 +87,11 @@ module Rainbows::Response::Body # :nodoc: ALIASES[:write_body_stream] = :write_body_each end - if method_defined?(:write_body_file) + if ALIASES[:write_body_file] # middlewares/apps may return with a body that responds to +to_path+ def write_body_path(sock, body, range) - stat = File.stat(body.to_path) - stat.file? ? write_body_file(sock, body, range) : - write_body_stream(sock, body, range) - ensure - body.respond_to?(:close) and body.close - end - elsif method_defined?(:write_body_stream) - def write_body_path(sock, body, range) - write_body_stream(sock, body, range) + File.file?(body.to_path) ? write_body_file(sock, body, range) : + write_body_stream(sock, body) ensure body.respond_to?(:close) and body.close end @@ -106,7 +101,7 @@ module Rainbows::Response::Body # :nodoc: def write_body(client, body, range) body.respond_to?(:to_path) ? write_body_path(client, body, range) : - write_body_each(client, body, range) + write_body_each(client, body) end else ALIASES[:write_body] = :write_body_each diff --git a/lib/rainbows/revactor/body.rb b/lib/rainbows/revactor/body.rb index 7bfb5de..9820df3 100644 --- a/lib/rainbows/revactor/body.rb +++ b/lib/rainbows/revactor/body.rb @@ -7,7 +7,7 @@ module Rainbows::Revactor::Body } if IO.method_defined?(:sendfile_nonblock) - def write_body_file(client, body, range) + def write_body_file_sendfile_revactor(client, body, range) body = body_to_io(body) sock = client.instance_variable_get(:@_io) pfx = Revactor::TCP::Socket === client ? :tcp : :unix @@ -33,6 +33,7 @@ module Rainbows::Revactor::Body ensure close_if_private(body) end + ALIASES[:write_body_file] = :write_body_file_sendfile_revactor else ALIASES[:write_body] = :write_body_each end -- cgit v1.2.3-24-ge0c7