1 # -*- encoding: binary -*-
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)
11 tmp.chmod(self.m = m == 0 ? 1 : 0)
12 alive && server.master_pid == Process.ppid or quit!
17 server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
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'
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
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:
44 # use :Revactor # this may also be :ThreadSpawn or :ThreadPool
45 # worker_connections 400
46 # keepalive_timeout 0 # zero disables keepalives entirely
49 # # the rest of the Unicorn configuration
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.
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).
64 block_given? or raise ArgumentError, "Rainbows! requires a block"
65 HttpServer.setup(block)
68 # maps models to default worker counts, default worker count numbers are
69 # pretty arbitrary and tuning them to your application and hardware is
71 MODEL_WORKER_CONNECTIONS = {
72 :Base => 1, # this one can't change
77 :RevThreadSpawn => 50,
80 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
81 autoload model, "rainbows/#{u.downcase!}"
86 # inject the Rainbows! method into Unicorn::Configurator
87 Unicorn::Configurator.class_eval { include Rainbows }