1 # -*- encoding: binary -*-
3 # This module adds \Rainbows! to the
4 # {Unicorn::Configurator}[http://unicorn.bogomips.org/Unicorn/Configurator.html]
5 module Rainbows::Configurator
6 Unicorn::Configurator::DEFAULTS.merge!({
7 :use => Rainbows::Base,
8 :worker_connections => 50,
9 :keepalive_timeout => 5,
10 :keepalive_requests => 100,
11 :client_max_body_size => 1024 * 1024,
12 :client_header_buffer_size => 1024,
15 # configures \Rainbows! with a given concurrency model to +use+ and
16 # a +worker_connections+ upper-bound. This method may be called
17 # inside a Unicorn/\Rainbows! configuration file:
20 # use :ThreadSpawn # concurrency model to use
21 # worker_connections 400
22 # keepalive_timeout 0 # zero disables keepalives entirely
23 # client_max_body_size 5*1024*1024 # 5 megabytes
24 # keepalive_requests 666 # default:100
25 # client_header_buffer_size 16 * 1024 # 16 kilobytes
28 # # the rest of the Unicorn configuration
31 # See the documentation for the respective Revactor, ThreadSpawn,
32 # and ThreadPool classes for descriptions and recommendations for
33 # each of them. The total number of clients we're able to serve is
34 # +worker_processes+ * +worker_connections+, so in the above example
35 # we can serve 8 * 400 = 3200 clients concurrently.
37 # The default is +keepalive_timeout+ is 5 seconds, which should be
38 # enough under most conditions for browsers to render the page and
39 # start retrieving extra elements for. Increasing this beyond 5
40 # seconds is not recommended. Zero disables keepalive entirely
41 # (but pipelining fully-formed requests is still works).
43 # The default +client_max_body_size+ is 1 megabyte (1024 * 1024 bytes),
44 # setting this to +nil+ will disable body size checks and allow any
45 # size to be specified.
47 # The default +keepalive_requests+ is 100, meaning a client may
48 # complete 100 keepalive requests after the initial request before
49 # \Rainbows! forces a disconnect. Lowering this can improve
50 # load-balancing characteristics as it forces HTTP/1.1 clients to
51 # reconnect after the specified number of requests, hopefully to a
52 # less busy host or worker process. This may also be used to mitigate
53 # denial-of-service attacks that use HTTP pipelining.
55 block_given? or raise ArgumentError, "Rainbows! requires a block"
63 @block or abort "must be inside a Rainbows! block"
66 def worker_connections(nr)
68 set_int(:worker_connections, nr, 1)
71 def use(model, *options)
74 Rainbows.const_get(model)
76 warn "error loading #{model.inspect}: #{e}"
77 e.backtrace.each { |l| warn l }
78 abort "concurrency model #{model.inspect} not supported"
80 Module === mod or abort "concurrency model #{model.inspect} not supported"
84 Rainbows::O.merge!(opt)
86 Rainbows::O[opt] = true
88 abort "cannot handle option: #{opt.inspect} in #{options.inspect}"
91 mod.setup if mod.respond_to?(:setup)
95 def keepalive_timeout(seconds)
97 set_int(:keepalive_timeout, seconds, 0)
100 def keepalive_requests(count)
104 set[:keepalive_requests] = count
106 abort "not an integer or nil: keepalive_requests=#{count.inspect}"
110 def client_max_body_size(bytes)
112 err = "client_max_body_size must be nil or a non-negative Integer"
116 bytes >= 0 or abort err
120 set[:client_max_body_size] = bytes
123 def client_header_buffer_size(bytes)
125 set_int(:client_header_buffer_size, bytes, 1)
130 # inject the Rainbows! method into Unicorn::Configurator
131 Unicorn::Configurator.__send__(:include, Rainbows::Configurator)