http_server: cap timeout at 32-bit LONG_MAX seconds
[rainbows.git] / lib / rainbows / http_server.rb
blob2573032c5cf1ee53e15cee0b54a6d09e0f1acc90
1 # -*- encoding: binary -*-
2 # :enddoc:
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
10   attr_reader :use
12   def self.setup(block)
13     Rainbows.server.instance_eval(&block)
14   end
16   def initialize(app, options)
17     Rainbows.server = self
18     @logger = Unicorn::Configurator::DEFAULTS[:logger]
19     super(app, options)
20     defined?(@use) or self.use = Rainbows::Base
21     @worker_connections ||= @use == :Base ? 1 : 50
22   end
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.
30   def timeout=(seconds)
31     max = 0x7fffffff
32     @timeout = seconds >= max ? max : seconds + 1
33   end
35   def load_config!
36     super
37     @worker_connections = 1 if @use == :Base
38   end
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)
46   end
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)
53     case @use
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)
60     end
61     super
62   end
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)
68     if cur == 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"
72     end
73     rescue => e
74       @logger.error e.message
75       @logger.error "#{resource} needs to be increased to >=#{want} before" \
76                     " starting #{svc}"
77   end
79   def svc
80     File.basename($0)
81   end
83   def use=(mod)
84     @use = mod.to_s.split(/::/)[-1].to_sym
85     new_defaults = {
86       'rainbows.model' => @use,
87       'rack.multithread' => !!(mod.to_s =~ /Thread/),
88       'rainbows.autochunk' => [:Coolio,:Rev,:Epoll,:XEpoll,
89                                :EventMachine,:NeverBlock].include?(@use),
90     }
91     Rainbows::Const::RACK_DEFAULTS.update(new_defaults)
92   end
94   def keepalive_requests=(nr)
95     Unicorn::HttpRequest.keepalive_requests = nr
96   end
98   def keepalive_requests
99     Unicorn::HttpRequest.keepalive_requests
100   end
102   def client_max_header_size=(bytes)
103     Unicorn::HttpParser.max_header_len = bytes
104   end