From afdae82eb156320cbee0fbd88800a9f56f78b645 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 6 Feb 2009 22:25:36 -0800 Subject: HttpResponse: use unbuffered I/O for writing, too 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... --- lib/unicorn/http_response.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'lib/unicorn/http_response.rb') 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 -- cgit v1.2.3-24-ge0c7