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
17 class Response416 < RangeError; end
19 # map of numeric file descriptors to IO objects to avoid using IO.new
20 # and potentially causing race conditions when using /dev/fd/
22 FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
26 require 'rainbows/const'
27 require 'rainbows/http_parser'
28 require 'rainbows/http_server'
29 autoload :RackInput, 'rainbows/rack_input'
30 autoload :Response, 'rainbows/response'
31 autoload :ProcessClient, 'rainbows/process_client'
32 autoload :Client, 'rainbows/client'
33 autoload :Base, 'rainbows/base'
34 autoload :Sendfile, 'rainbows/sendfile'
35 autoload :AppPool, 'rainbows/app_pool'
36 autoload :DevFdResponse, 'rainbows/dev_fd_response'
37 autoload :MaxBody, 'rainbows/max_body'
38 autoload :QueuePool, 'rainbows/queue_pool'
39 autoload :EvCore, 'rainbows/ev_core'
40 autoload :SocketProxy, 'rainbows/socket_proxy'
42 # Sleeps the current application dispatch. This will pick the
43 # optimal method to sleep depending on the concurrency model chosen
44 # (which may still suck and block the entire process). Using this
45 # with the basic :Coolio or :EventMachine models is not recommended.
46 # This should be used within your Rack application.
48 case Rainbows.server.use
49 when :FiberPool, :FiberSpawn
50 Rainbows::Fiber.sleep(nr)
51 when :RevFiberSpawn, :CoolioFiberSpawn
52 Rainbows::Fiber::Coolio::Sleeper.new(nr)
60 # runs the Rainbows! HttpServer with +app+ and +options+ and does
61 # not return until the server has exited.
62 def self.run(app, options = {}) # :nodoc:
63 HttpServer.new(app, options).start.join
68 attr_accessor :max_bytes, :keepalive_timeout
70 attr_accessor :cur # may not always be used
76 # the default max body size is 1 megabyte (1024 * 1024 bytes)
77 @max_bytes = 1024 * 1024
79 # the default keepalive_timeout is 5 seconds
80 @keepalive_timeout = 5
89 @tick_io.chmod(@tick_mod = 0 == @tick_mod ? 1 : 0)
90 exit!(2) if @expire && Time.now >= @expire
91 @alive && @server.master_pid == Process.ppid or quit!
100 Rainbows::HttpParser.quit
101 @expire ||= Time.now + (@server.timeout * 2.0)
102 @server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
106 # maps models to default worker counts, default worker count numbers are
107 # pretty arbitrary and tuning them to your application and hardware is
109 MODEL_WORKER_CONNECTIONS = {
110 :Base => 1, # this one can't change
111 :WriterThreadPool => 20,
112 :WriterThreadSpawn => 20,
117 :RevThreadSpawn => 50,
118 :RevThreadPool => 50,
119 :RevFiberSpawn => 50,
121 :CoolioThreadSpawn => 50,
122 :CoolioThreadPool => 50,
123 :CoolioFiberSpawn => 50,
131 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
132 autoload model, "rainbows/#{u.downcase!}"
135 autoload :Fiber, 'rainbows/fiber' # core class
136 autoload :StreamFile, 'rainbows/stream_file'
137 autoload :HttpResponse, 'rainbows/http_response' # deprecated
138 autoload :ThreadTimeout, 'rainbows/thread_timeout'
139 autoload :WorkerYield, 'rainbows/worker_yield'
140 autoload :SyncClose, 'rainbows/sync_close'
143 require 'rainbows/error'
144 require 'rainbows/configurator'