about summary refs log tree commit homepage
path: root/lib/rainbows/response/body.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-08-17 08:35:03 +0000
committerEric Wong <normalperson@yhbt.net>2010-08-17 08:41:15 +0000
commitac05e7035e1946b78ce4679548db7680aa01734c (patch)
tree1379997b2686b9fa777216ad06f1ffe9be5b6027 /lib/rainbows/response/body.rb
parent9c5669e232f8f57c2d8e08d84e21bf09cd87bfa1 (diff)
downloadrainbows-ac05e7035e1946b78ce4679548db7680aa01734c.tar.gz
First off we use an FD_MAP to avoid creating redundant IO
objects which map to the same FD.  When that doesn't work, we'll
fall back to trapping Errno::EBADF and IOError where
appropriate.
Diffstat (limited to 'lib/rainbows/response/body.rb')
-rw-r--r--lib/rainbows/response/body.rb8
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/rainbows/response/body.rb b/lib/rainbows/response/body.rb
index cf14f08..2535374 100644
--- a/lib/rainbows/response/body.rb
+++ b/lib/rainbows/response/body.rb
@@ -30,6 +30,12 @@
 module Rainbows::Response::Body # :nodoc:
   ALIASES = {}
 
+  FD_MAP = Rainbows::FD_MAP
+
+  def io_for_fd(fd)
+    FD_MAP.delete(fd) || IO.new(fd)
+  end
+
   # 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
@@ -41,7 +47,7 @@ module Rainbows::Response::Body # :nodoc:
       # 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)
+      path =~ %r{\A/dev/fd/(\d+)\z} ? io_for_fd($1.to_i) : File.open(path)
     end
   end