1 # -*- encoding: binary -*-
3 require 'rainbows/error'
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)
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!
20 self.expire ||= Time.now + (server.timeout * 2.0)
21 server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
26 G = State.new(true, 0, 0, 5)
30 require 'rainbows/const'
31 require 'rainbows/http_server'
32 require 'rainbows/http_response'
33 require 'rainbows/base'
34 require 'rainbows/tee_input'
35 autoload :Sendfile, 'rainbows/sendfile'
36 autoload :AppPool, 'rainbows/app_pool'
37 autoload :DevFdResponse, 'rainbows/dev_fd_response'
38 autoload :MaxBody, 'rainbows/max_body'
39 autoload :QueuePool, 'rainbows/queue_pool'
43 # Sleeps the current application dispatch. This will pick the
44 # optimal method to sleep depending on the concurrency model chosen
45 # (which may still suck and block the entire process). Using this
46 # with the basic :Rev or :EventMachine models is not recommended.
47 # This should be used within your Rack application.
50 when :FiberPool, :FiberSpawn
51 Rainbows::Fiber.sleep(nr)
53 Rainbows::Fiber::Rev::Sleeper.new(nr)
61 # runs the Rainbows! HttpServer with +app+ and +options+ and does
62 # not return until the server has exited.
63 def run(app, options = {})
64 HttpServer.new(app, options).start.join
67 # returns nil if accept fails
70 rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
72 rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EINTR
75 # returns nil if accept fails
77 rv = sock.accept_nonblock
78 rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
80 rescue Errno::EAGAIN, Errno::ECONNABORTED
83 # returns a string representing the address of the given client +io+
84 # For local UNIX domain sockets, this will return a string referred
85 # to by the (non-frozen) Unicorn::HttpRequest::LOCALHOST constant.
87 io.respond_to?(:peeraddr) ?
88 io.peeraddr.last : Unicorn::HttpRequest::LOCALHOST
91 # the default max body size is 1 megabyte (1024 * 1024 bytes)
92 @@max_bytes = 1024 * 1024
94 def max_bytes; @@max_bytes; end
95 def max_bytes=(nr); @@max_bytes = nr; end
98 # configures \Rainbows! with a given concurrency model to +use+ and
99 # a +worker_connections+ upper-bound. This method may be called
100 # inside a Unicorn/Rainbows configuration file:
103 # use :Revactor # this may also be :ThreadSpawn or :ThreadPool
104 # worker_connections 400
105 # keepalive_timeout 0 # zero disables keepalives entirely
106 # client_max_body_size 5*1024*1024 # 5 megabytes
109 # # the rest of the Unicorn configuration
112 # See the documentation for the respective Revactor, ThreadSpawn,
113 # and ThreadPool classes for descriptions and recommendations for
114 # each of them. The total number of clients we're able to serve is
115 # +worker_processes+ * +worker_connections+, so in the above example
116 # we can serve 8 * 400 = 3200 clients concurrently.
118 # The default is +keepalive_timeout+ is 5 seconds, which should be
119 # enough under most conditions for browsers to render the page and
120 # start retrieving extra elements for. Increasing this beyond 5
121 # seconds is not recommended. Zero disables keepalive entirely
122 # (but pipelining fully-formed requests is still works).
124 # The default +client_max_body_size+ is 1 megabyte (1024 * 1024 bytes),
125 # setting this to +nil+ will disable body size checks and allow any
126 # size to be specified.
127 def Rainbows!(&block)
128 block_given? or raise ArgumentError, "Rainbows! requires a block"
129 HttpServer.setup(block)
133 # maps models to default worker counts, default worker count numbers are
134 # pretty arbitrary and tuning them to your application and hardware is
136 MODEL_WORKER_CONNECTIONS = {
137 :Base => 1, # this one can't change
138 :WriterThreadPool => 20,
139 :WriterThreadSpawn => 20,
144 :RevThreadSpawn => 50,
145 :RevThreadPool => 50,
151 :RevFiberSpawn => 50,
153 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
154 autoload model, "rainbows/#{u.downcase!}"
157 autoload :Fiber, 'rainbows/fiber' # core class
161 # inject the Rainbows! method into Unicorn::Configurator
162 Unicorn::Configurator.class_eval { include Rainbows }