1 # -*- encoding: binary -*-
4 # the value passed to TCP_DEFER_ACCEPT actually matters in Linux 2.6.32+
5 Unicorn::SocketHelper::DEFAULTS[:tcp_defer_accept] = 60
7 require 'rainbows/error'
8 require 'rainbows/configurator'
12 # global vars because class/instance variables are confusing me :<
13 # this struct is only accessed inside workers and thus private to each
14 # G.cur may not be used in the network concurrency model
16 class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp,:expire)
18 tmp.chmod(self.m = m == 0 ? 1 : 0)
19 exit!(2) if expire && Time.now >= expire
20 alive && server.master_pid == Process.ppid or quit!
25 self.expire ||= Time.now + (server.timeout * 2.0)
26 server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
30 G = State.new(true, 0, 0, 5)
32 class Response416 < RangeError; end
34 # map of numeric file descriptors to IO objects to avoid using IO.new
35 # and potentially causing race conditions when using /dev/fd/
37 FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
41 require 'rainbows/const'
42 require 'rainbows/http_server'
43 require 'rainbows/response'
44 require 'rainbows/client'
45 require 'rainbows/http_request'
46 require 'rainbows/tee_input'
47 require 'rainbows/process_client'
48 autoload :Base, 'rainbows/base'
49 autoload :Sendfile, 'rainbows/sendfile'
50 autoload :AppPool, 'rainbows/app_pool'
51 autoload :DevFdResponse, 'rainbows/dev_fd_response'
52 autoload :MaxBody, 'rainbows/max_body'
53 autoload :QueuePool, 'rainbows/queue_pool'
57 # Sleeps the current application dispatch. This will pick the
58 # optimal method to sleep depending on the concurrency model chosen
59 # (which may still suck and block the entire process). Using this
60 # with the basic :Rev or :EventMachine models is not recommended.
61 # This should be used within your Rack application.
64 when :FiberPool, :FiberSpawn
65 Rainbows::Fiber.sleep(nr)
67 Rainbows::Fiber::Rev::Sleeper.new(nr)
75 # runs the Rainbows! HttpServer with +app+ and +options+ and does
76 # not return until the server has exited.
77 def run(app, options = {}) # :nodoc:
78 HttpServer.new(app, options).start.join
82 # the default max body size is 1 megabyte (1024 * 1024 bytes)
83 @@max_bytes = 1024 * 1024
85 def max_bytes; @@max_bytes; end
86 def max_bytes=(nr); @@max_bytes = nr; end
91 # maps models to default worker counts, default worker count numbers are
92 # pretty arbitrary and tuning them to your application and hardware is
94 MODEL_WORKER_CONNECTIONS = {
95 :Base => 1, # this one can't change
96 :WriterThreadPool => 20,
97 :WriterThreadSpawn => 20,
102 :RevThreadSpawn => 50,
103 :RevThreadPool => 50,
109 :RevFiberSpawn => 50,
111 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
112 autoload model, "rainbows/#{u.downcase!}"
115 autoload :Fiber, 'rainbows/fiber' # core class
116 autoload :StreamFile, 'rainbows/stream_file'
117 autoload :HttpResponse, 'rainbows/http_response' # deprecated
118 autoload :ThreadTimeout, 'rainbows/thread_timeout'