fix grammar fail in RDoc
[rainbows.git] / lib / rainbows.rb
blob8923bf68f496dac44589f173d7e9099901bba31a
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.cur may not be used in the network concurrency model
9   class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp)
10     def tick
11       tmp.chmod(self.m = m == 0 ? 1 : 0)
12       alive && server.master_pid == Process.ppid or quit!
13     end
15     def quit!
16       self.alive = false
17       server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
18       false
19     end
20   end
21   G = State.new(true, 0, 0, 2)
23   require 'rainbows/const'
24   require 'rainbows/http_server'
25   require 'rainbows/http_response'
26   require 'rainbows/base'
27   autoload :AppPool, 'rainbows/app_pool'
28   autoload :DevFdResponse, 'rainbows/dev_fd_response'
30   class << self
32     # runs the Rainbows! HttpServer with +app+ and +options+ and does
33     # not return until the server has exited.
34     def run(app, options = {})
35       HttpServer.new(app, options).start.join
36     end
37   end
39   # configures \Rainbows! with a given concurrency model to +use+ and
40   # a +worker_connections+ upper-bound.  This method may be called
41   # inside a Unicorn/Rainbows configuration file:
42   #
43   #   Rainbows! do
44   #     use :Revactor # this may also be :ThreadSpawn or :ThreadPool
45   #     worker_connections 400
46   #     keepalive_timeout 0 # zero disables keepalives entirely
47   #   end
48   #
49   #   # the rest of the Unicorn configuration
50   #   worker_processes 8
51   #
52   # See the documentation for the respective Revactor, ThreadSpawn,
53   # and ThreadPool classes for descriptions and recommendations for
54   # each of them.  The total number of clients we're able to serve is
55   # +worker_processes+ * +worker_connections+, so in the above example
56   # we can serve 8 * 400 = 3200 clients concurrently.
57   #
58   # The default is +keepalive_timeout+ is 2 seconds, which should be
59   # enough under most conditions for browsers to render the page and
60   # start retrieving extra elements for.  Increasing this beyond 5
61   # seconds is not recommended.  Zero disables keepalive entirely
62   # (but pipelining fully-formed requests is still works).
63   def Rainbows!(&block)
64     block_given? or raise ArgumentError, "Rainbows! requires a block"
65     HttpServer.setup(block)
66   end
68   # maps models to default worker counts, default worker count numbers are
69   # pretty arbitrary and tuning them to your application and hardware is
70   # highly recommended
71   MODEL_WORKER_CONNECTIONS = {
72     :Base => 1, # this one can't change
73     :Revactor => 50,
74     :ThreadSpawn => 30,
75     :ThreadPool => 10,
76     :Rev => 50,
77     :RevThreadSpawn => 50,
78     :EventMachine => 50,
79   }.each do |model, _|
80     u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
81     autoload model, "rainbows/#{u.downcase!}"
82   end
84 end
86 # inject the Rainbows! method into Unicorn::Configurator
87 Unicorn::Configurator.class_eval { include Rainbows }