From a50c9d312b9d5274a95f2816b5f53a3738d0cb92 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 26 Dec 2010 22:48:33 +0000 Subject: rev_thread_*: unindent and split out This should make classes easier to find and hopefully make our code easier to follow. --- lib/rainbows/rev_thread_pool.rb | 119 ++++++++++++++------------------ lib/rainbows/rev_thread_pool/client.rb | 8 +++ lib/rainbows/rev_thread_pool/watcher.rb | 14 ++++ lib/rainbows/rev_thread_spawn.rb | 55 ++++++--------- lib/rainbows/rev_thread_spawn/client.rb | 8 +++ 5 files changed, 102 insertions(+), 102 deletions(-) create mode 100644 lib/rainbows/rev_thread_pool/client.rb create mode 100644 lib/rainbows/rev_thread_pool/watcher.rb create mode 100644 lib/rainbows/rev_thread_spawn/client.rb diff --git a/lib/rainbows/rev_thread_pool.rb b/lib/rainbows/rev_thread_pool.rb index b31e1f5..b377062 100644 --- a/lib/rainbows/rev_thread_pool.rb +++ b/lib/rainbows/rev_thread_pool.rb @@ -1,76 +1,57 @@ # -*- encoding: binary -*- -require 'rainbows/rev/thread_client' -module Rainbows - - # A combination of the Rev and ThreadPool models. This allows Ruby - # Thread-based concurrency for application processing. It DOES NOT - # expose a streamable "rack.input" for upload processing within the - # app. DevFdResponse should be used with this class to proxy - # asynchronous responses. All network I/O between the client and - # server are handled by the main thread and outside of the core - # application dispatch. - # - # Unlike ThreadPool, Rev makes this model highly suitable for - # slow clients and applications with medium-to-slow response times - # (I/O bound), but less suitable for sleepy applications. - # - # This concurrency model is designed for Ruby 1.9, and Ruby 1.8 - # users are NOT advised to use this due to high CPU usage. - - module RevThreadPool - - # :stopdoc: - DEFAULTS = { - :pool_size => 20, # same default size as ThreadPool (w/o Rev) - } - #:startdoc: - - def self.setup # :nodoc: - DEFAULTS.each { |k,v| O[k] ||= v } - Integer === O[:pool_size] && O[:pool_size] > 0 or - raise ArgumentError, "pool_size must a be an Integer > 0" - end - - class PoolWatcher < ::Rev::TimerWatcher # :nodoc: all - def initialize(threads) - @threads = threads - super(G.server.timeout, true) - end - - def on_timer - @threads.each { |t| t.join(0) and G.quit! } - end - end - - class Client < Rainbows::Rev::ThreadClient # :nodoc: - def app_dispatch - QUEUE << self - end - end - - include Rainbows::Rev::Core - - def init_worker_threads(master, queue) # :nodoc: - O[:pool_size].times.map do - Thread.new do - begin - client = queue.pop - master << [ client, client.app_response ] - rescue => e - Error.listen_loop(e) - end while true - end +# A combination of the Rev and ThreadPool models. This allows Ruby +# Thread-based concurrency for application processing. It DOES NOT +# expose a streamable "rack.input" for upload processing within the +# app. DevFdResponse should be used with this class to proxy +# asynchronous responses. All network I/O between the client and +# server are handled by the main thread and outside of the core +# application dispatch. +# +# Unlike ThreadPool, Rev makes this model highly suitable for +# slow clients and applications with medium-to-slow response times +# (I/O bound), but less suitable for sleepy applications. +# +# This concurrency model is designed for Ruby 1.9, and Ruby 1.8 +# users are NOT advised to use this due to high CPU usage. +module Rainbows::RevThreadPool + + # :stopdoc: + DEFAULTS = { + :pool_size => 20, # same default size as ThreadPool (w/o Rev) + } + #:startdoc: + + def self.setup # :nodoc: + o = Rainbows::O + DEFAULTS.each { |k,v| o[k] ||= v } + Integer === o[:pool_size] && o[:pool_size] > 0 or + raise ArgumentError, "pool_size must a be an Integer > 0" + end + include Rainbows::Rev::Core + + def init_worker_threads(master, queue) # :nodoc: + Rainbows::O[:pool_size].times.map do + Thread.new do + begin + client = queue.pop + master << [ client, client.app_response ] + rescue => e + Rainbows::Error.listen_loop(e) + end while true end end + end - def init_worker_process(worker) # :nodoc: - super - master = Rev::Master.new(Queue.new).attach(::Rev::Loop.default) - queue = Client.const_set(:QUEUE, Queue.new) - threads = init_worker_threads(master, queue) - PoolWatcher.new(threads).attach(::Rev::Loop.default) - logger.info "RevThreadPool pool_size=#{O[:pool_size]}" - end + def init_worker_process(worker) # :nodoc: + super + master = Rainbows::Rev::Master.new(Queue.new).attach(Rev::Loop.default) + queue = Rainbows::RevThreadPool::Client.const_set(:QUEUE, Queue.new) + threads = init_worker_threads(master, queue) + Rainbows::RevThreadPool::Watcher.new(threads).attach(Rev::Loop.default) + logger.info "RevThreadPool pool_size=#{Rainbows::O[:pool_size]}" end end +# :enddoc: +require 'rainbows/rev_thread_pool/client' +require 'rainbows/rev_thread_pool/watcher' diff --git a/lib/rainbows/rev_thread_pool/client.rb b/lib/rainbows/rev_thread_pool/client.rb new file mode 100644 index 0000000..c282951 --- /dev/null +++ b/lib/rainbows/rev_thread_pool/client.rb @@ -0,0 +1,8 @@ +# -*- encoding: binary -*- +# :enddoc: +class Rainbows::RevThreadPool::Client < Rainbows::Rev::ThreadClient + # QUEUE constant will be set in worker_loop + def app_dispatch + QUEUE << self + end +end diff --git a/lib/rainbows/rev_thread_pool/watcher.rb b/lib/rainbows/rev_thread_pool/watcher.rb new file mode 100644 index 0000000..2419066 --- /dev/null +++ b/lib/rainbows/rev_thread_pool/watcher.rb @@ -0,0 +1,14 @@ +# -*- encoding: binary -*- +# :enddoc: +class Rainbows::RevThreadPool::Watcher < Rev::TimerWatcher + G = Rainbows::G + + def initialize(threads) + @threads = threads + super(G.server.timeout, true) + end + + def on_timer + @threads.each { |t| t.join(0) and G.quit! } + end +end diff --git a/lib/rainbows/rev_thread_spawn.rb b/lib/rainbows/rev_thread_spawn.rb index 38771d3..94e9503 100644 --- a/lib/rainbows/rev_thread_spawn.rb +++ b/lib/rainbows/rev_thread_spawn.rb @@ -1,38 +1,27 @@ # -*- encoding: binary -*- -require 'rainbows/rev/thread_client' -module Rainbows - - # A combination of the Rev and ThreadSpawn models. This allows Ruby - # Thread-based concurrency for application processing. It DOES NOT - # expose a streamable "rack.input" for upload processing within the - # app. DevFdResponse should be used with this class to proxy - # asynchronous responses. All network I/O between the client and - # server are handled by the main thread and outside of the core - # application dispatch. - # - # Unlike ThreadSpawn, Rev makes this model highly suitable for - # slow clients and applications with medium-to-slow response times - # (I/O bound), but less suitable for sleepy applications. - # - # Ruby 1.8 users are strongly advised to use Rev >= 0.3.2 to get - # usable performance. - - module RevThreadSpawn - - class Client < Rainbows::Rev::ThreadClient # :nodoc: all - def app_dispatch - Thread.new(self) { |client| MASTER << [ client, app_response ] } - end - end - - include Rainbows::Rev::Core - - def init_worker_process(worker) # :nodoc: - super - master = Rev::Master.new(Queue.new).attach(::Rev::Loop.default) - Client.const_set(:MASTER, master) - end +# A combination of the Rev and ThreadSpawn models. This allows Ruby +# Thread-based concurrency for application processing. It DOES NOT +# expose a streamable "rack.input" for upload processing within the +# app. DevFdResponse should be used with this class to proxy +# asynchronous responses. All network I/O between the client and +# server are handled by the main thread and outside of the core +# application dispatch. +# +# Unlike ThreadSpawn, Rev makes this model highly suitable for +# slow clients and applications with medium-to-slow response times +# (I/O bound), but less suitable for sleepy applications. +# +# This concurrency model is designed for Ruby 1.9, and Ruby 1.8 +# users are NOT advised to use this due to high CPU usage. +module Rainbows::RevThreadSpawn + include Rainbows::Rev::Core + def init_worker_process(worker) # :nodoc: + super + master = Rainbows::Rev::Master.new(Queue.new).attach(Rev::Loop.default) + Rainbows::RevThreadSpawn::Client.const_set(:MASTER, master) end end +# :enddoc: +require 'rainbows/rev_thread_spawn/client' diff --git a/lib/rainbows/rev_thread_spawn/client.rb b/lib/rainbows/rev_thread_spawn/client.rb new file mode 100644 index 0000000..60afc9b --- /dev/null +++ b/lib/rainbows/rev_thread_spawn/client.rb @@ -0,0 +1,8 @@ +# -*- encoding: binary -*- +# :enddoc: +class Rainbows::RevThreadSpawn::Client < Rainbows::Rev::ThreadClient + # MASTER will be set in worker_loop + def app_dispatch + Thread.new(self) { |client| MASTER << [ client, app_response ] } + end +end -- cgit v1.2.3-24-ge0c7