doc: misc cleanups and additions for RDoc
[rainbows.git] / lib / rainbows / rev_thread_pool.rb
blob4366086367d132bca6d0e51ba29bb10e4836fcef
1 # -*- encoding: binary -*-
3 # CoolioThreadPool is the new version of this, use that instead.
5 # A combination of the Rev and ThreadPool models.  This allows Ruby
6 # Thread-based concurrency for application processing.  It DOES NOT
7 # expose a streamable "rack.input" for upload processing within the
8 # app.  DevFdResponse should be used with this class to proxy
9 # asynchronous responses.  All network I/O between the client and
10 # server are handled by the main thread and outside of the core
11 # application dispatch.
13 # Unlike ThreadPool, Rev makes this model highly suitable for
14 # slow clients and applications with medium-to-slow response times
15 # (I/O bound), but less suitable for sleepy applications.
17 # This concurrency model is designed for Ruby 1.9, and Ruby 1.8
18 # users are NOT advised to use this due to high CPU usage.
19 module Rainbows::RevThreadPool
21   # :stopdoc:
22   DEFAULTS = {
23     :pool_size => 20, # same default size as ThreadPool (w/o Rev)
24   }
25   #:startdoc:
27   def self.setup # :nodoc:
28     o = Rainbows::O
29     DEFAULTS.each { |k,v| o[k] ||= v }
30     Integer === o[:pool_size] && o[:pool_size] > 0 or
31       raise ArgumentError, "pool_size must a be an Integer > 0"
32   end
33   include Rainbows::Rev::Core
35   def init_worker_threads(master, queue) # :nodoc:
36     Rainbows::O[:pool_size].times.map do
37       Thread.new do
38         begin
39           client = queue.pop
40           master << [ client, client.app_response ]
41         rescue => e
42           Rainbows::Error.listen_loop(e)
43         end while true
44       end
45     end
46   end
48   def init_worker_process(worker) # :nodoc:
49     super
50     master = Rainbows::Rev::Master.new(Queue.new).attach(Rev::Loop.default)
51     queue = Rainbows::RevThreadPool::Client.const_set(:QUEUE, Queue.new)
52     threads = init_worker_threads(master, queue)
53     Rainbows::RevThreadPool::Watcher.new(threads).attach(Rev::Loop.default)
54     logger.info "RevThreadPool pool_size=#{Rainbows::O[:pool_size]}"
55   end
56 end
57 # :enddoc:
58 require 'rainbows/rev_thread_pool/client'
59 require 'rainbows/rev_thread_pool/watcher'