thread_pool: comment for potential SMP issue under 1.9
[rainbows.git] / lib / rainbows.rb
bloba8985c6baffc91a2d51d8bc1063449eeedfd507c
1 # -*- encoding: binary -*-
2 require 'unicorn'
4 module Rainbows
6   # global vars because class/instance variables are confusing me :<
7   # this struct is only accessed inside workers and thus private to each
8   G = Struct.new(:cur, :max, :logger, :alive, :app).new
9   # G.cur may not be used the network concurrency model
10   G.alive = true
12   require 'rainbows/const'
13   require 'rainbows/http_server'
14   require 'rainbows/http_response'
15   require 'rainbows/base'
16   autoload :AppPool, 'rainbows/app_pool'
17   autoload :DevFdResponse, 'rainbows/dev_fd_response'
19   class << self
21     # runs the Rainbows! HttpServer with +app+ and +options+ and does
22     # not return until the server has exited.
23     def run(app, options = {})
24       HttpServer.new(app, options).start.join
25     end
26   end
28   # configures \Rainbows! with a given concurrency model to +use+ and
29   # a +worker_connections+ upper-bound.  This method may be called
30   # inside a Unicorn/Rainbows configuration file:
31   #
32   #   Rainbows! do
33   #     use :Revactor # this may also be :ThreadSpawn or :ThreadPool
34   #     worker_connections 400
35   #   end
36   #
37   #   # the rest of the Unicorn configuration
38   #   worker_processes 8
39   #
40   # See the documentation for the respective Revactor, ThreadSpawn,
41   # and ThreadPool classes for descriptions and recommendations for
42   # each of them.  The total number of clients we're able to serve is
43   # +worker_processes+ * +worker_connections+, so in the above example
44   # we can serve 8 * 400 = 3200 clients concurrently.
45   def Rainbows!(&block)
46     block_given? or raise ArgumentError, "Rainbows! requires a block"
47     HttpServer.setup(block)
48   end
50   # maps models to default worker counts, default worker count numbers are
51   # pretty arbitrary and tuning them to your application and hardware is
52   # highly recommended
53   MODEL_WORKER_CONNECTIONS = {
54     :Base => 1, # this one can't change
55     :Revactor => 50,
56     :ThreadSpawn => 30,
57     :ThreadPool => 10,
58     :Rev => 50,
59     :EventMachine => 50,
60   }.each do |model, _|
61     u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
62     autoload model, "rainbows/#{u.downcase!}"
63   end
65 end
67 # inject the Rainbows! method into Unicorn::Configurator
68 Unicorn::Configurator.class_eval { include Rainbows }