doc: better document :pool_size options
[rainbows.git] / lib / rainbows / coolio_thread_pool.rb
blob0e616a6665b1e70bd6b2709d846ce2dee171ad92
1 # -*- encoding: binary -*-
3 # A combination of the Coolio and ThreadPool models.  This allows Ruby
4 # Thread-based concurrency for application processing.  It DOES NOT
5 # expose a streamable "rack.input" for upload processing within the
6 # app.  DevFdResponse should be used with this class to proxy
7 # asynchronous responses.  All network I/O between the client and
8 # server are handled by the main thread and outside of the core
9 # application dispatch.
11 # Unlike ThreadPool, Cool.io makes this model highly suitable for
12 # slow clients and applications with medium-to-slow response times
13 # (I/O bound), but less suitable for sleepy applications.
15 # This concurrency model is designed for Ruby 1.9, and Ruby 1.8
16 # users are NOT advised to use this due to high CPU usage.
18 # === :pool_size vs worker_connections
20 # In your Rainbows! config block, you may specify a Thread pool size
21 # to limit your application concurrency independently of
22 # worker_connections.
24 #   Rainbows! do
25 #     use :CoolioThreadPool, :pool_size => 50
26 #     worker_connections 100
27 #   end
29 # In extremely rare cases, this may be combined with Rainbows::AppPool
30 # if you have different concurrency capabilities for different parts of
31 # your Rack application.
32 module Rainbows::CoolioThreadPool
33   # :stopdoc:
34   autoload :Client, 'rainbows/coolio_thread_pool/client'
35   extend Rainbows::PoolSize
36   #:startdoc:
37   include Rainbows::Coolio::Core
39   def init_worker_threads(master, queue) # :nodoc:
40     Rainbows::O[:pool_size].times.map do
41       Thread.new do
42         begin
43           client = queue.pop
44           master << [ client, client.app_response ]
45         rescue => e
46           Rainbows::Error.listen_loop(e)
47         end while true
48       end
49     end
50   end
52   def init_worker_process(worker) # :nodoc:
53     super
54     cloop = Coolio::Loop.default
55     master = Rainbows::Coolio::Master.new(Queue.new).attach(cloop)
56     queue = Client.const_set(:QUEUE, Queue.new)
57     threads = init_worker_threads(master, queue)
58     Watcher.new(threads).attach(cloop)
59     logger.info "CoolioThreadPool pool_size=#{Rainbows::O[:pool_size]}"
60   end
61 end
62 # :enddoc:
63 require 'rainbows/coolio_thread_pool/watcher'