Revert "http_server: cap timeout at 32-bit LONG_MAX seconds"
[rainbows.git] / lib / rainbows / http_server.rb
blob746d534b696d771a75844e972d1365c91e961fbd
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=(nr)
31     @timeout = nr + 1
32   end
34   def load_config!
35     super
36     @worker_connections = 1 if @use == :Base
37   end
39   def worker_loop(worker)
40     Rainbows.forked = true
41     orig = method(:worker_loop)
42     extend(Rainbows.const_get(@use))
43     m = method(:worker_loop)
44     orig == m ? super(worker) : worker_loop(worker)
45   end
47   def spawn_missing_workers
48     # 5: std{in,out,err} + heartbeat FD + per-process listener
49     nofile = 5 + @worker_connections + LISTENERS.size
50     trysetrlimit(:RLIMIT_NOFILE, nofile)
52     case @use
53     when :ThreadSpawn, :ThreadPool, :ActorSpawn,
54          :CoolioThreadSpawn, :RevThreadSpawn,
55          :XEpollThreadSpawn, :WriterThreadPool, :WriterThreadSpawn
56       trysetrlimit(:RLIMIT_NPROC, @worker_connections + LISTENERS.size + 1)
57     when :XEpollThreadPool, :CoolioThreadPool
58       trysetrlimit(:RLIMIT_NPROC, Rainbows::O[:pool_size] + LISTENERS.size + 1)
59     end
60     super
61   end
63   def trysetrlimit(resource, want)
64     var = Process.const_get(resource)
65     cur, max = Process.getrlimit(var)
66     cur <= want and Process.setrlimit(var, cur = max > want ? max : want)
67     if cur == want
68       @logger.warn "#{resource} rlim_cur=#{cur} is barely enough"
69       @logger.warn "#{svc} may monopolize resources dictated by #{resource}" \
70                    " and leave none for your app"
71     end
72     rescue => e
73       @logger.error e.message
74       @logger.error "#{resource} needs to be increased to >=#{want} before" \
75                     " starting #{svc}"
76   end
78   def svc
79     File.basename($0)
80   end
82   def use=(mod)
83     @use = mod.to_s.split(/::/)[-1].to_sym
84     new_defaults = {
85       'rainbows.model' => @use,
86       'rack.multithread' => !!(mod.to_s =~ /Thread/),
87       'rainbows.autochunk' => [:Coolio,:Rev,:Epoll,:XEpoll,
88                                :EventMachine,:NeverBlock].include?(@use),
89     }
90     Rainbows::Const::RACK_DEFAULTS.update(new_defaults)
91   end
93   def keepalive_requests=(nr)
94     Unicorn::HttpRequest.keepalive_requests = nr
95   end
97   def keepalive_requests
98     Unicorn::HttpRequest.keepalive_requests
99   end
101   def client_max_header_size=(bytes)
102     Unicorn::HttpParser.max_header_len = bytes
103   end