keepalive_timeout defaults to 5 seconds
[rainbows.git] / lib / rainbows.rb
blob5c8ee940170b41935d07f36a3c9d4fdc52049f22
1 # -*- encoding: binary -*-
2 require 'unicorn'
3 require 'rainbows/error'
4 require 'fcntl'
6 module Rainbows
8   # global vars because class/instance variables are confusing me :<
9   # this struct is only accessed inside workers and thus private to each
10   # G.cur may not be used in the network concurrency model
11   class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp,:expire)
12     def tick
13       tmp.chmod(self.m = m == 0 ? 1 : 0)
14       exit!(2) if expire && Time.now >= expire
15       alive && server.master_pid == Process.ppid or quit!
16     end
18     def quit!
19       self.alive = false
20       self.expire ||= Time.now + (server.timeout * 2.0)
21       server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
22       false
23     end
24   end
25   G = State.new(true, 0, 0, 5)
26   O = {}
28   require 'rainbows/const'
29   require 'rainbows/http_server'
30   require 'rainbows/http_response'
31   require 'rainbows/base'
32   autoload :AppPool, 'rainbows/app_pool'
33   autoload :DevFdResponse, 'rainbows/dev_fd_response'
35   class << self
37     # Sleeps the current application dispatch.  This will pick the
38     # optimal method to sleep depending on the concurrency model chosen
39     # (which may still suck and block the entire process).  Using this
40     # with the basic :Rev or :EventMachine models is not recommended.
41     # This should be used within your Rack application.
42     def sleep(nr)
43       case G.server.use
44       when :FiberPool, :FiberSpawn
45         Rainbows::Fiber.sleep(nr)
46       when :RevFiberSpawn
47         Rainbows::Fiber::Rev::Sleeper.new(nr)
48       when :Revactor
49         Actor.sleep(nr)
50       else
51         Kernel.sleep(nr)
52       end
53     end
55     # runs the Rainbows! HttpServer with +app+ and +options+ and does
56     # not return until the server has exited.
57     def run(app, options = {})
58       HttpServer.new(app, options).start.join
59     end
61     # returns nil if accept fails
62     def sync_accept(sock)
63       rv = sock.accept
64       rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
65       rv
66     rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EINTR
67     end
69     # returns nil if accept fails
70     def accept(sock)
71       rv = sock.accept_nonblock
72       rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
73       rv
74     rescue Errno::EAGAIN, Errno::ECONNABORTED
75     end
76   end
78   # configures \Rainbows! with a given concurrency model to +use+ and
79   # a +worker_connections+ upper-bound.  This method may be called
80   # inside a Unicorn/Rainbows configuration file:
81   #
82   #   Rainbows! do
83   #     use :Revactor # this may also be :ThreadSpawn or :ThreadPool
84   #     worker_connections 400
85   #     keepalive_timeout 0 # zero disables keepalives entirely
86   #   end
87   #
88   #   # the rest of the Unicorn configuration
89   #   worker_processes 8
90   #
91   # See the documentation for the respective Revactor, ThreadSpawn,
92   # and ThreadPool classes for descriptions and recommendations for
93   # each of them.  The total number of clients we're able to serve is
94   # +worker_processes+ * +worker_connections+, so in the above example
95   # we can serve 8 * 400 = 3200 clients concurrently.
96   #
97   # The default is +keepalive_timeout+ is 2 seconds, which should be
98   # enough under most conditions for browsers to render the page and
99   # start retrieving extra elements for.  Increasing this beyond 5
100   # seconds is not recommended.  Zero disables keepalive entirely
101   # (but pipelining fully-formed requests is still works).
102   def Rainbows!(&block)
103     block_given? or raise ArgumentError, "Rainbows! requires a block"
104     HttpServer.setup(block)
105   end
107   # maps models to default worker counts, default worker count numbers are
108   # pretty arbitrary and tuning them to your application and hardware is
109   # highly recommended
110   MODEL_WORKER_CONNECTIONS = {
111     :Base => 1, # this one can't change
112     :Revactor => 50,
113     :ThreadSpawn => 30,
114     :ThreadPool => 20,
115     :Rev => 50,
116     :RevThreadSpawn => 50,
117     :RevThreadPool => 50,
118     :EventMachine => 50,
119     :EventMachineDefer => 50,
120     :FiberSpawn => 50,
121     :FiberPool => 50,
122     :ActorSpawn => 50,
123     :NeverBlock => 50,
124     :RevFiberSpawn => 50,
125   }.each do |model, _|
126     u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
127     autoload model, "rainbows/#{u.downcase!}"
128   end
129   autoload :Fiber, 'rainbows/fiber' # core class
133 # inject the Rainbows! method into Unicorn::Configurator
134 Unicorn::Configurator.class_eval { include Rainbows }