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
25 # use :CoolioThreadPool, :pool_size => 50
26 # worker_connections 100
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
34 autoload :Client, 'rainbows/coolio_thread_pool/client'
35 extend Rainbows::PoolSize
37 include Rainbows::Coolio::Core
39 def init_worker_threads(master, queue) # :nodoc:
40 Rainbows::O[:pool_size].times.map do
44 master << [ client, client.app_response ]
46 Rainbows::Error.listen_loop(e)
52 def init_worker_process(worker) # :nodoc:
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]}"
63 require 'rainbows/coolio_thread_pool/watcher'