1 # -*- encoding: binary -*-
4 class Rainbows::HttpServer < Unicorn::HttpServer
6 Rainbows.server.instance_eval(&block)
9 def initialize(app, options)
10 Rainbows.server = self
11 @logger = Unicorn::Configurator::DEFAULTS[:logger]
13 defined?(@use) or use(:Base)
14 @worker_connections ||= @use == :Base ? 1 : 50
17 def reopen_worker_logs(worker_nr)
18 logger.info "worker=#{worker_nr} reopening logs..."
19 Unicorn::Util.reopen_logs
20 logger.info "worker=#{worker_nr} done reopening logs"
22 Rainbows.quit! # let the master reopen and refork us
25 # Add one second to the timeout since our fchmod heartbeat is less
26 # precise (and must be more conservative) than Unicorn does. We
27 # handle many clients per process and can't chmod on every
28 # connection we accept without wasting cycles. That added to the
29 # fact that we let clients keep idle connections open for long
30 # periods of time means we have to chmod at a fixed interval.
37 Rainbows.keepalive_timeout = 5
38 Rainbows.max_bytes = 1024 * 1024
39 @worker_connections = nil
41 @worker_connections ||= @use == :Base ? 1 : 50
44 def worker_loop(worker)
45 orig = method(:worker_loop)
46 extend(Rainbows.const_get(@use))
47 m = method(:worker_loop)
48 orig == m ? super(worker) : worker_loop(worker)
51 def spawn_missing_workers
52 # 5: std{in,out,err} + heartbeat FD + per-process listener
53 nofile = 5 + @worker_connections + LISTENERS.size
54 trysetrlimit(:RLIMIT_NOFILE, nofile)
57 when :ThreadSpawn, :ThreadPool, :ActorSpawn,
58 :CoolioThreadSpawn, :RevThreadSpawn,
59 :WriterThreadPool, :WriterThreadSpawn
60 trysetrlimit(:RLIMIT_NPROC, @worker_connections + LISTENERS.size + 1)
65 def trysetrlimit(resource, want)
66 var = Process.const_get(resource)
67 cur, max = Process.getrlimit(var)
68 cur <= want and Process.setrlimit(var, cur = max > want ? max : want)
70 @logger.warn "#{resource} rlim_cur=#{cur} is barely enough"
71 @logger.warn "#{svc} may monopolize resources dictated by #{resource}" \
72 " and leave none for your app"
75 @logger.error e.message
76 @logger.error "#{resource} needs to be increased to >=#{want} before" \
85 model = args.shift or return @use
87 Rainbows.const_get(model)
89 logger.error "error loading #{model.inspect}: #{e}"
90 e.backtrace.each { |l| logger.error l }
91 raise ArgumentError, "concurrency model #{model.inspect} not supported"
95 raise ArgumentError, "concurrency model #{model.inspect} not supported"
98 when Hash; Rainbows::O.update(opt)
99 when Symbol; Rainbows::O[opt] = true
100 else; raise ArgumentError, "can't handle option: #{opt.inspect}"
103 mod.setup if mod.respond_to?(:setup)
105 'rainbows.model' => (@use = model.to_sym),
106 'rack.multithread' => !!(model.to_s =~ /Thread/),
107 'rainbows.autochunk' => [:Coolio,:Rev,:Epoll,:XEpoll,
108 :EventMachine,:NeverBlock].include?(@use),
110 Rainbows::Const::RACK_DEFAULTS.update(new_defaults)
113 def worker_connections(*args)
114 return @worker_connections if args.empty?
116 (Integer === nr && nr > 0) or
117 raise ArgumentError, "worker_connections must be a positive Integer"
118 @worker_connections = nr
121 def keepalive_timeout(nr)
122 (Integer === nr && nr >= 0) or
123 raise ArgumentError, "keepalive_timeout must be a non-negative Integer"
124 Rainbows.keepalive_timeout = nr
127 def keepalive_requests(nr)
129 raise ArgumentError, "keepalive_requests must be a non-negative Integer"
130 Unicorn::HttpRequest.keepalive_requests = nr
133 def client_max_body_size(nr)
134 err = "client_max_body_size must be nil or a non-negative Integer"
138 nr >= 0 or raise ArgumentError, err
140 raise ArgumentError, err
142 Rainbows.max_bytes = nr
145 def client_header_buffer_size(bytes)
146 Integer === bytes && bytes > 0 or raise ArgumentError,
147 "client_header_buffer_size must be a positive Integer"
148 Rainbows.client_header_buffer_size = bytes