configurator: move validation logic over
[rainbows.git] / lib / rainbows / configurator.rb
blob95bb590b72eb032a908ecf61d5d73d9861b4b7ce
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,
13   })
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:
18   #
19   #   Rainbows! do
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
26   #   end
27   #
28   #   # the rest of the Unicorn configuration
29   #   worker_processes 8
30   #
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.
36   #
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).
42   #
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.
46   #
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.
54   def Rainbows!(&block)
55     block_given? or raise ArgumentError, "Rainbows! requires a block"
56     @block = true
57     instance_eval(&block)
58     ensure
59       @block = false
60   end
62   def check! # :nodoc:
63     @block or abort "must be inside a Rainbows! block"
64   end
66   def worker_connections(nr)
67     check!
68     set_int(:worker_connections, nr, 1)
69   end
71   def use(model, *options)
72     check!
73     mod = begin
74       Rainbows.const_get(model)
75     rescue NameError => e
76       warn "error loading #{model.inspect}: #{e}"
77       e.backtrace.each { |l| warn l }
78       abort "concurrency model #{model.inspect} not supported"
79     end
80     Module === mod or abort "concurrency model #{model.inspect} not supported"
81     options.each do |opt|
82       case opt
83       when Hash
84         Rainbows::O.merge!(opt)
85       when Symbol
86         Rainbows::O[opt] = true
87       else
88         abort "cannot handle option: #{opt.inspect} in #{options.inspect}"
89       end
90     end
91     mod.setup if mod.respond_to?(:setup)
92     set[:use] = mod
93   end
95   def keepalive_timeout(seconds)
96     check!
97     set_int(:keepalive_timeout, seconds, 0)
98   end
100   def keepalive_requests(count)
101     check!
102     case count
103     when nil, Integer
104       set[:keepalive_requests] = count
105     else
106       abort "not an integer or nil: keepalive_requests=#{count.inspect}"
107     end
108   end
110   def client_max_body_size(bytes)
111     check!
112     err = "client_max_body_size must be nil or a non-negative Integer"
113     case bytes
114     when nil
115     when Integer
116       bytes >= 0 or abort err
117     else
118       abort err
119     end
120     set[:client_max_body_size] = bytes
121   end
123   def client_header_buffer_size(bytes)
124     check!
125     set_int(:client_header_buffer_size, bytes, 1)
126   end
129 # :enddoc:
130 # inject the Rainbows! method into Unicorn::Configurator
131 Unicorn::Configurator.__send__(:include, Rainbows::Configurator)