From 894cb73887c106acc793f0317ee849ae215ead56 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 25 Oct 2010 22:15:47 +0000 Subject: reduce dependency on IO#write_nonblock kgio_trywrite is superior if it is available. --- lib/rainbows/error.rb | 8 +++++++- lib/rainbows/process_client.rb | 4 ++-- lib/rainbows/rev/client.rb | 2 +- lib/rainbows/writer_thread_pool.rb | 8 ++------ lib/rainbows/writer_thread_spawn.rb | 8 ++------ 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/rainbows/error.rb b/lib/rainbows/error.rb index bdbfdc5..68cdec4 100644 --- a/lib/rainbows/error.rb +++ b/lib/rainbows/error.rb @@ -9,7 +9,13 @@ module Rainbows::Error # if the socket is already closed or broken. We'll always ensure # the socket is closed at the end of this function def self.write(io, e) - msg = response(e) and io.write_nonblock(msg) + if msg = response(e) + if io.respond_to?(:kgio_trywrite) + io.kgio_trywrite(msg) + else + io.write_nonblock(msg) + end + end rescue end diff --git a/lib/rainbows/process_client.rb b/lib/rainbows/process_client.rb index 5e200e5..1e2d0d9 100644 --- a/lib/rainbows/process_client.rb +++ b/lib/rainbows/process_client.rb @@ -19,13 +19,13 @@ module Rainbows::ProcessClient # Base, ThreadSpawn, ThreadPool def process_client(client) # :nodoc: hp = HttpParser.new - client.readpartial(16384, buf = hp.buf) + client.kgio_read!(16384, buf = hp.buf) remote_addr = client.kgio_addr begin # loop until env = hp.parse wait_headers_readable(client) or return - buf << client.readpartial(16384) + buf << client.kgio_read!(16384) end env[CLIENT_IO] = client diff --git a/lib/rainbows/rev/client.rb b/lib/rainbows/rev/client.rb index 0c02525..20adbc1 100644 --- a/lib/rainbows/rev/client.rb +++ b/lib/rainbows/rev/client.rb @@ -147,7 +147,7 @@ module Rainbows def handle_error(e) close_deferred if msg = Error.response(e) - @_io.write_nonblock(msg) rescue nil + @_io.kgio_trywrite(msg) rescue nil end @_write_buffer.clear ensure diff --git a/lib/rainbows/writer_thread_pool.rb b/lib/rainbows/writer_thread_pool.rb index 335a901..a8ffd7d 100644 --- a/lib/rainbows/writer_thread_pool.rb +++ b/lib/rainbows/writer_thread_pool.rb @@ -29,10 +29,6 @@ module Rainbows to_io.kgio_addr end - def readpartial(size, buf = "") - to_io.readpartial(size, buf) - end - def kgio_read(size, buf = "") to_io.kgio_read(size, buf) end @@ -41,8 +37,8 @@ module Rainbows to_io.kgio_read!(size, buf) end - def write_nonblock(buf) - to_io.write_nonblock(buf) + def kgio_trywrite(buf) + to_io.kgio_trywrite(buf) end def write(buf) diff --git a/lib/rainbows/writer_thread_spawn.rb b/lib/rainbows/writer_thread_spawn.rb index 6468151..c190f89 100644 --- a/lib/rainbows/writer_thread_spawn.rb +++ b/lib/rainbows/writer_thread_spawn.rb @@ -34,10 +34,6 @@ module Rainbows to_io.kgio_addr end - def readpartial(size, buf = "") - to_io.readpartial(size, buf) - end - def kgio_read(size, buf = "") to_io.kgio_read(size, buf) end @@ -46,8 +42,8 @@ module Rainbows to_io.kgio_read!(size, buf) end - def write_nonblock(buf) - to_io.write_nonblock(buf) + def kgio_trywrite(buf) + to_io.kgio_trywrite(buf) end def queue_writer -- cgit v1.2.3-24-ge0c7