README: update URLs
[rainbows.git] / lib / rainbows.rb
blobc2813a52bb82f2f2887c4f90d95093be13933508
1 # -*- encoding: binary -*-
2 require 'unicorn'
4 module Rainbows
6   require 'rainbows/const'
7   require 'rainbows/http_server'
8   require 'rainbows/http_response'
9   require 'rainbows/base'
11   class << self
13     # runs the Rainbows! HttpServer with +app+ and +options+ and does
14     # not return until the server has exited.
15     def run(app, options = {})
16       HttpServer.new(app, options).start.join
17     end
18   end
20   # configures \Rainbows! with a given concurrency model to +use+ and
21   # a +worker_connections+ upper-bound.  This method may be called
22   # inside a Unicorn/Rainbows configuration file:
23   #
24   #   Rainbows! do
25   #     use :Revactor # this may also be :ThreadSpawn or :ThreadPool
26   #     worker_connections 400
27   #   end
28   #
29   #   # the rest of the Unicorn configuration
30   #   worker_processes 8
31   #
32   # See the documentation for the respective Revactor, ThreadSpawn,
33   # and ThreadPool classes for descriptions and recommendations for
34   # each of them.  The total number of clients we're able to serve is
35   # +worker_processes+ * +worker_connections+, so in the above example
36   # we can serve 8 * 400 = 3200 clients concurrently.
37   def Rainbows!(&block)
38     block_given? or raise ArgumentError, "Rainbows! requires a block"
39     HttpServer.setup(block)
40   end
42   # maps models to default worker counts, default worker count numbers are
43   # pretty arbitrary and tuning them to your application and hardware is
44   # highly recommended
45   MODEL_WORKER_CONNECTIONS = {
46     :Base => 1, # this one can't change
47     :Revactor => 50,
48     :ThreadSpawn => 30,
49     :ThreadPool => 10,
50     :Rev => 50,
51   }.each do |model, _|
52     u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
53     autoload model, "rainbows/#{u.downcase!}"
54   end
56 end
58 # inject the Rainbows! method into Unicorn::Configurator
59 Unicorn::Configurator.class_eval { include Rainbows }