1 # -*- encoding: binary -*-
4 class Rainbows::HttpServer < Unicorn::HttpServer
5 attr_accessor :copy_stream
6 attr_accessor :worker_connections
7 attr_accessor :keepalive_timeout
8 attr_accessor :client_header_buffer_size
9 attr_accessor :client_max_body_size
13 Rainbows.server.instance_eval(&block)
16 def initialize(app, options)
17 Rainbows.server = self
18 @logger = Unicorn::Configurator::DEFAULTS[:logger]
20 defined?(@use) or self.use = Rainbows::Base
21 @worker_connections ||= @use == :Base ? 1 : 50
24 # Add one second to the timeout since our fchmod heartbeat is less
25 # precise (and must be more conservative) than Unicorn does. We
26 # handle many clients per process and can't chmod on every
27 # connection we accept without wasting cycles. That added to the
28 # fact that we let clients keep idle connections open for long
29 # periods of time means we have to chmod at a fixed interval.
32 @timeout = seconds >= max ? max : seconds + 1
37 @worker_connections = 1 if @use == :Base
40 def worker_loop(worker)
41 Rainbows.forked = true
42 orig = method(:worker_loop)
43 extend(Rainbows.const_get(@use))
44 m = method(:worker_loop)
45 orig == m ? super(worker) : worker_loop(worker)
48 def spawn_missing_workers
49 # 5: std{in,out,err} + heartbeat FD + per-process listener
50 nofile = 5 + @worker_connections + LISTENERS.size
51 trysetrlimit(:RLIMIT_NOFILE, nofile)
54 when :ThreadSpawn, :ThreadPool, :ActorSpawn,
55 :CoolioThreadSpawn, :RevThreadSpawn,
56 :XEpollThreadSpawn, :WriterThreadPool, :WriterThreadSpawn
57 trysetrlimit(:RLIMIT_NPROC, @worker_connections + LISTENERS.size + 1)
58 when :XEpollThreadPool, :CoolioThreadPool
59 trysetrlimit(:RLIMIT_NPROC, Rainbows::O[:pool_size] + LISTENERS.size + 1)
64 def trysetrlimit(resource, want)
65 var = Process.const_get(resource)
66 cur, max = Process.getrlimit(var)
67 cur <= want and Process.setrlimit(var, cur = max > want ? max : want)
69 @logger.warn "#{resource} rlim_cur=#{cur} is barely enough"
70 @logger.warn "#{svc} may monopolize resources dictated by #{resource}" \
71 " and leave none for your app"
74 @logger.error e.message
75 @logger.error "#{resource} needs to be increased to >=#{want} before" \
84 @use = mod.to_s.split(/::/)[-1].to_sym
86 'rainbows.model' => @use,
87 'rack.multithread' => !!(mod.to_s =~ /Thread/),
88 'rainbows.autochunk' => [:Coolio,:Rev,:Epoll,:XEpoll,
89 :EventMachine,:NeverBlock].include?(@use),
91 Rainbows::Const::RACK_DEFAULTS.update(new_defaults)
94 def keepalive_requests=(nr)
95 Unicorn::HttpRequest.keepalive_requests = nr
98 def keepalive_requests
99 Unicorn::HttpRequest.keepalive_requests
102 def client_max_header_size=(bytes)
103 Unicorn::HttpParser.max_header_len = bytes