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 }
25 G = State.new(true, 0, 0, 2)
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'
37 # runs the Rainbows! HttpServer with +app+ and +options+ and does
38 # not return until the server has exited.
39 def run(app, options = {})
40 HttpServer.new(app, options).start.join
43 # returns nil if accept fails
44 if defined?(Fcntl::FD_CLOEXEC)
46 rv = sock.accept_nonblock
47 rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
49 rescue Errno::EAGAIN, Errno::ECONNABORTED
54 rescue Errno::EAGAIN, Errno::ECONNABORTED
59 # configures \Rainbows! with a given concurrency model to +use+ and
60 # a +worker_connections+ upper-bound. This method may be called
61 # inside a Unicorn/Rainbows configuration file:
64 # use :Revactor # this may also be :ThreadSpawn or :ThreadPool
65 # worker_connections 400
66 # keepalive_timeout 0 # zero disables keepalives entirely
69 # # the rest of the Unicorn configuration
72 # See the documentation for the respective Revactor, ThreadSpawn,
73 # and ThreadPool classes for descriptions and recommendations for
74 # each of them. The total number of clients we're able to serve is
75 # +worker_processes+ * +worker_connections+, so in the above example
76 # we can serve 8 * 400 = 3200 clients concurrently.
78 # The default is +keepalive_timeout+ is 2 seconds, which should be
79 # enough under most conditions for browsers to render the page and
80 # start retrieving extra elements for. Increasing this beyond 5
81 # seconds is not recommended. Zero disables keepalive entirely
82 # (but pipelining fully-formed requests is still works).
84 block_given? or raise ArgumentError, "Rainbows! requires a block"
85 HttpServer.setup(block)
88 # maps models to default worker counts, default worker count numbers are
89 # pretty arbitrary and tuning them to your application and hardware is
91 MODEL_WORKER_CONNECTIONS = {
92 :Base => 1, # this one can't change
97 :RevThreadSpawn => 50,
100 :EventMachineDefer => 50,
105 :RevFiberSpawn => 50,
107 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
108 autoload model, "rainbows/#{u.downcase!}"
110 autoload :Fiber, 'rainbows/fiber' # core class
114 # inject the Rainbows! method into Unicorn::Configurator
115 Unicorn::Configurator.class_eval { include Rainbows }