about summary refs log tree commit homepage
path: root/lib/unicorn/http_response.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-02-06 22:25:36 -0800
committerEric Wong <normalperson@yhbt.net>2009-02-09 19:52:18 -0800
commitafdae82eb156320cbee0fbd88800a9f56f78b645 (patch)
treef89e3fa4f806978db5867957afae5221e9d1981c /lib/unicorn/http_response.rb
parent8c176fa571511d0a4dc5fa04777db4565a9d453d (diff)
downloadunicorn-afdae82eb156320cbee0fbd88800a9f56f78b645.tar.gz
Avoid needless userspace copies and craziness.  We'll need to
handle EINTR since writes to sockets means stupid things like
this, but it's a small cost...
Diffstat (limited to 'lib/unicorn/http_response.rb')
-rw-r--r--lib/unicorn/http_response.rb20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb
index e2a4e2f..eab3a82 100644
--- a/lib/unicorn/http_response.rb
+++ b/lib/unicorn/http_response.rb
@@ -48,9 +48,25 @@ module Unicorn
         end
       end
 
-      socket.write("#{HTTP_STATUS_HEADERS[status]}\r\n#{out.join}\r\n")
-      body.each { |chunk| socket.write(chunk) }
+      socket_write(socket, "#{HTTP_STATUS_HEADERS[status]}\r\n#{out.join}\r\n")
+      body.each { |chunk| socket_write(socket, chunk) }
     end
 
+    private
+
+      # write(2) can return short on slow devices like sockets as well
+      # as fail with EINTR if a signal was caught.
+      def self.socket_write(socket, buffer)
+        loop do
+          begin
+            written = socket.syswrite(buffer)
+            return written if written == buffer.length
+            buffer = buffer[written..-1]
+          rescue Errno::EINTR
+            retry
+          end
+        end
+      end
+
   end
 end