tests: "wc -c" portability for *BSDs
[rainbows.git] / lib / rainbows / coolio_thread_pool.rb
blob7f107b75a28872bf50612781d0e65ffd993f7625
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.
33 # === RubyGem Requirements
34 # * cool.io 1.0.0 or later
35 module Rainbows::CoolioThreadPool
36   # :stopdoc:
37   autoload :Client, 'rainbows/coolio_thread_pool/client'
38   extend Rainbows::PoolSize
39   #:startdoc:
40   include Rainbows::Coolio::Core
42   def init_worker_threads(master, queue) # :nodoc:
43     Rainbows::O[:pool_size].times.map do
44       Thread.new do
45         begin
46           client = queue.pop
47           master << [ client, client.app_response ]
48         rescue => e
49           Rainbows::Error.listen_loop(e)
50         end while true
51       end
52     end
53   end
55   def init_worker_process(worker) # :nodoc:
56     super
57     cloop = Coolio::Loop.default
58     master = Rainbows::Coolio::Master.new(Queue.new).attach(cloop)
59     queue = Client.const_set(:QUEUE, Queue.new)
60     threads = init_worker_threads(master, queue)
61     Watcher.new(threads).attach(cloop)
62     logger.info "CoolioThreadPool pool_size=#{Rainbows::O[:pool_size]}"
63   end
64 end
65 # :enddoc:
66 require 'rainbows/coolio_thread_pool/watcher'