about summary refs log tree commit homepage
path: root/lib/rainbows.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-06-06 02:14:58 +0000
committerEric Wong <normalperson@yhbt.net>2010-06-06 02:14:58 +0000
commitb1315983056f91467b336aeb426759debc006a4e (patch)
tree8a0d3dee51038b567dcb72bd4af40d5ff707da9d /lib/rainbows.rb
parent6ada0eb9916bbcebbcf2af843c1f5fb11904caee (diff)
downloadrainbows-b1315983056f91467b336aeb426759debc006a4e.tar.gz
centralize body => IO conversion logic
Since EventMachine and Rev shared the same logic for optimizing
and avoiding extra file opens for IO/File-ish response bodies,
so centralize that.

For Ruby 1.9 users, we've also enabled this logic so ThreadPool,
ThreadSpawn, WriterThreadPool, and WriterThreadSpawn can take
advantage of Rainbows::DevFdResponse-generated bodies while
proxying sockets.
Diffstat (limited to 'lib/rainbows.rb')
-rw-r--r--lib/rainbows.rb15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/rainbows.rb b/lib/rainbows.rb
index e186549..5aae746 100644
--- a/lib/rainbows.rb
+++ b/lib/rainbows.rb
@@ -156,6 +156,21 @@ module Rainbows
   # :startdoc:
   autoload :Fiber, 'rainbows/fiber' # core class
 
+  # to_io is not part of the Rack spec, but make an exception here
+  # since we can conserve path lookups and file descriptors
+  # \Rainbows! will never get here without checking for the existence
+  # of body.to_path first.
+  def self.body_to_io(body)
+    if body.respond_to?(:to_io)
+      body.to_io
+    else
+      # try to take advantage of Rainbows::DevFdResponse, calling File.open
+      # is a last resort
+      path = body.to_path
+      path =~ %r{\A/dev/fd/(\d+)\z} ? IO.new($1.to_i) : File.open(path, 'rb')
+    end
+  end
+
 end
 
 # inject the Rainbows! method into Unicorn::Configurator