1 # -*- encoding: binary -*-
3 require 'rainbows/error'
7 # global vars because class/instance variables are confusing me :<
8 # this struct is only accessed inside workers and thus private to each
9 # G.cur may not be used in the network concurrency model
10 class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp)
12 tmp.chmod(self.m = m == 0 ? 1 : 0)
13 alive && server.master_pid == Process.ppid or quit!
18 server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
22 G = State.new(true, 0, 0, 2)
24 require 'rainbows/const'
25 require 'rainbows/http_server'
26 require 'rainbows/http_response'
27 require 'rainbows/base'
28 autoload :AppPool, 'rainbows/app_pool'
29 autoload :DevFdResponse, 'rainbows/dev_fd_response'
33 # runs the Rainbows! HttpServer with +app+ and +options+ and does
34 # not return until the server has exited.
35 def run(app, options = {})
36 HttpServer.new(app, options).start.join
40 # configures \Rainbows! with a given concurrency model to +use+ and
41 # a +worker_connections+ upper-bound. This method may be called
42 # inside a Unicorn/Rainbows configuration file:
45 # use :Revactor # this may also be :ThreadSpawn or :ThreadPool
46 # worker_connections 400
47 # keepalive_timeout 0 # zero disables keepalives entirely
50 # # the rest of the Unicorn configuration
53 # See the documentation for the respective Revactor, ThreadSpawn,
54 # and ThreadPool classes for descriptions and recommendations for
55 # each of them. The total number of clients we're able to serve is
56 # +worker_processes+ * +worker_connections+, so in the above example
57 # we can serve 8 * 400 = 3200 clients concurrently.
59 # The default is +keepalive_timeout+ is 2 seconds, which should be
60 # enough under most conditions for browsers to render the page and
61 # start retrieving extra elements for. Increasing this beyond 5
62 # seconds is not recommended. Zero disables keepalive entirely
63 # (but pipelining fully-formed requests is still works).
65 block_given? or raise ArgumentError, "Rainbows! requires a block"
66 HttpServer.setup(block)
69 # maps models to default worker counts, default worker count numbers are
70 # pretty arbitrary and tuning them to your application and hardware is
72 MODEL_WORKER_CONNECTIONS = {
73 :Base => 1, # this one can't change
78 :RevThreadSpawn => 50,
83 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
84 autoload model, "rainbows/#{u.downcase!}"
86 autoload :Fiber, 'rainbows/fiber' # core class
90 # inject the Rainbows! method into Unicorn::Configurator
91 Unicorn::Configurator.class_eval { include Rainbows }