From e7d295fd8e3628eba7a1ba52e95b7dee11532e98 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 27 Dec 2010 02:20:00 +0000 Subject: writer_thread_*: split out common socket_proxy code Needless duplication sucks --- lib/rainbows.rb | 1 + lib/rainbows/socket_proxy.rb | 24 ++++++++++++++++++++++++ lib/rainbows/writer_thread_pool.rb | 20 +------------------- lib/rainbows/writer_thread_spawn.rb | 21 +-------------------- 4 files changed, 27 insertions(+), 39 deletions(-) create mode 100644 lib/rainbows/socket_proxy.rb diff --git a/lib/rainbows.rb b/lib/rainbows.rb index e9f2448..adbc37c 100644 --- a/lib/rainbows.rb +++ b/lib/rainbows.rb @@ -47,6 +47,7 @@ module Rainbows autoload :MaxBody, 'rainbows/max_body' autoload :QueuePool, 'rainbows/queue_pool' autoload :EvCore, 'rainbows/ev_core' + autoload :SocketProxy, 'rainbows/socket_proxy' class << self diff --git a/lib/rainbows/socket_proxy.rb b/lib/rainbows/socket_proxy.rb new file mode 100644 index 0000000..e4cb686 --- /dev/null +++ b/lib/rainbows/socket_proxy.rb @@ -0,0 +1,24 @@ +# -*- encoding: binary -*- +# :enddoc: +# +module Rainbows::SocketProxy + def kgio_addr + to_io.kgio_addr + end + + def kgio_read(size, buf = "") + to_io.kgio_read(size, buf) + end + + def kgio_read!(size, buf = "") + to_io.kgio_read!(size, buf) + end + + def kgio_trywrite(buf) + to_io.kgio_trywrite(buf) + end + + def timed_read(buf) + to_io.timed_read(buf) + end +end diff --git a/lib/rainbows/writer_thread_pool.rb b/lib/rainbows/writer_thread_pool.rb index 7b5e861..e4e0228 100644 --- a/lib/rainbows/writer_thread_pool.rb +++ b/lib/rainbows/writer_thread_pool.rb @@ -23,25 +23,7 @@ module Rainbows::WriterThreadPool # used to wrap a BasicSocket to use with +q+ for all writes # this is compatible with IO.select class QueueSocket < Struct.new(:to_io, :q) # :nodoc: - def kgio_addr - to_io.kgio_addr - end - - def kgio_read(size, buf = "") - to_io.kgio_read(size, buf) - end - - def kgio_read!(size, buf = "") - to_io.kgio_read!(size, buf) - end - - def kgio_trywrite(buf) - to_io.kgio_trywrite(buf) - end - - def timed_read(buf) - to_io.timed_read(buf) - end + include Rainbows::SocketProxy def write(buf) q << [ to_io, buf ] diff --git a/lib/rainbows/writer_thread_spawn.rb b/lib/rainbows/writer_thread_spawn.rb index 4ee98dd..4215254 100644 --- a/lib/rainbows/writer_thread_spawn.rb +++ b/lib/rainbows/writer_thread_spawn.rb @@ -28,26 +28,7 @@ module Rainbows::WriterThreadSpawn # this is compatible with IO.select class MySocket < Struct.new(:to_io, :q, :thr) # :nodoc: all include Rainbows::Response - - def kgio_addr - to_io.kgio_addr - end - - def kgio_read(size, buf = "") - to_io.kgio_read(size, buf) - end - - def kgio_read!(size, buf = "") - to_io.kgio_read!(size, buf) - end - - def kgio_trywrite(buf) - to_io.kgio_trywrite(buf) - end - - def timed_read(buf) - to_io.timed_read(buf) - end + include Rainbows::SocketProxy def queue_writer # not using Thread.pass here because that spins the CPU during -- cgit v1.2.3-24-ge0c7