1 # -*- encoding: binary -*-
4 Unicorn::SocketHelper::DEFAULTS.merge!({
5 # the value passed to TCP_DEFER_ACCEPT actually matters in Linux 2.6.32+
6 :tcp_defer_accept => 60,
8 # keep-alive performance sucks without this due to
9 # write(headers)-write(body)-read
10 # because we always write headers and bodies with two calls
18 # map of numeric file descriptors to IO objects to avoid using IO.new
19 # and potentially causing race conditions when using /dev/fd/
21 FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
25 require 'rainbows/const'
26 require 'rainbows/http_parser'
27 require 'rainbows/http_server'
28 autoload :Response, 'rainbows/response'
29 autoload :ProcessClient, 'rainbows/process_client'
30 autoload :Client, 'rainbows/client'
31 autoload :Base, 'rainbows/base'
32 autoload :Sendfile, 'rainbows/sendfile'
33 autoload :AppPool, 'rainbows/app_pool'
34 autoload :DevFdResponse, 'rainbows/dev_fd_response'
35 autoload :MaxBody, 'rainbows/max_body'
36 autoload :QueuePool, 'rainbows/queue_pool'
37 autoload :EvCore, 'rainbows/ev_core'
38 autoload :SocketProxy, 'rainbows/socket_proxy'
40 # Sleeps the current application dispatch. This will pick the
41 # optimal method to sleep depending on the concurrency model chosen
42 # (which may still suck and block the entire process). Using this
43 # with the basic :Coolio or :EventMachine models is not recommended.
44 # This should be used within your Rack application.
46 case Rainbows.server.use
47 when :FiberPool, :FiberSpawn
48 Rainbows::Fiber.sleep(nr)
49 when :RevFiberSpawn, :CoolioFiberSpawn
50 Rainbows::Fiber::Coolio::Sleeper.new(nr)
58 # runs the Rainbows! HttpServer with +app+ and +options+ and does
59 # not return until the server has exited.
60 def self.run(app, options = {}) # :nodoc:
61 HttpServer.new(app, options).start.join
66 attr_accessor :max_bytes, :keepalive_timeout
68 attr_accessor :cur # may not always be used
74 # the default max body size is 1 megabyte (1024 * 1024 bytes)
75 @max_bytes = 1024 * 1024
77 # the default keepalive_timeout is 5 seconds
78 @keepalive_timeout = 5
87 @tick_io.chmod(@tick_mod = 0 == @tick_mod ? 1 : 0)
88 exit!(2) if @expire && Time.now >= @expire
89 @alive && @server.master_pid == Process.ppid or quit!
98 Rainbows::HttpParser.quit
99 @expire ||= Time.now + (@server.timeout * 2.0)
100 @server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }.clear
104 # maps models to default worker counts, default worker count numbers are
105 # pretty arbitrary and tuning them to your application and hardware is
107 MODEL_WORKER_CONNECTIONS = {
108 :Base => 1, # this one can't change
109 :WriterThreadPool => 20,
110 :WriterThreadSpawn => 20,
115 :RevThreadSpawn => 50,
116 :RevThreadPool => 50,
117 :RevFiberSpawn => 50,
119 :CoolioThreadSpawn => 50,
120 :CoolioThreadPool => 50,
121 :CoolioFiberSpawn => 50,
130 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
131 autoload model, "rainbows/#{u.downcase!}"
134 autoload :Fiber, 'rainbows/fiber' # core class
135 autoload :StreamFile, 'rainbows/stream_file'
136 autoload :HttpResponse, 'rainbows/http_response' # deprecated
137 autoload :ThreadTimeout, 'rainbows/thread_timeout'
138 autoload :WorkerYield, 'rainbows/worker_yield'
139 autoload :SyncClose, 'rainbows/sync_close'
142 require 'rainbows/error'
143 require 'rainbows/configurator'