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
13 # we always want to send our headers out ASAP since Rainbows!
14 # is designed for apps that could trickle out the body slowly
22 # map of numeric file descriptors to IO objects to avoid using IO.new
23 # and potentially causing race conditions when using /dev/fd/
25 FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
29 require 'rainbows/const'
30 require 'rainbows/http_parser'
31 require 'rainbows/http_server'
32 autoload :Response, 'rainbows/response'
33 autoload :ProcessClient, 'rainbows/process_client'
34 autoload :Client, 'rainbows/client'
35 autoload :Base, 'rainbows/base'
36 autoload :Sendfile, 'rainbows/sendfile'
37 autoload :AppPool, 'rainbows/app_pool'
38 autoload :DevFdResponse, 'rainbows/dev_fd_response'
39 autoload :MaxBody, 'rainbows/max_body'
40 autoload :QueuePool, 'rainbows/queue_pool'
41 autoload :EvCore, 'rainbows/ev_core'
42 autoload :SocketProxy, 'rainbows/socket_proxy'
44 # Sleeps the current application dispatch. This will pick the
45 # optimal method to sleep depending on the concurrency model chosen
46 # (which may still suck and block the entire process). Using this
47 # with the basic :Coolio or :EventMachine models is not recommended.
48 # This should be used within your Rack application.
50 case Rainbows.server.use
51 when :FiberPool, :FiberSpawn
52 Rainbows::Fiber.sleep(nr)
53 when :RevFiberSpawn, :CoolioFiberSpawn
54 Rainbows::Fiber::Coolio::Sleeper.new(nr)
62 # runs the Rainbows! HttpServer with +app+ and +options+ and does
63 # not return until the server has exited.
64 def self.run(app, options = {}) # :nodoc:
65 HttpServer.new(app, options).start.join
70 attr_accessor :max_bytes, :keepalive_timeout
72 attr_accessor :cur # may not always be used
78 # the default max body size is 1 megabyte (1024 * 1024 bytes)
79 @max_bytes = 1024 * 1024
81 # the default keepalive_timeout is 5 seconds
82 @keepalive_timeout = 5
91 @tick_io.chmod(@tick_mod = 0 == @tick_mod ? 1 : 0)
92 exit!(2) if @expire && Time.now >= @expire
93 @alive && @server.master_pid == Process.ppid or quit!
103 Rainbows::HttpParser.quit
104 @expire = Time.now + (@server.timeout * 2.0)
105 Unicorn::HttpServer::LISTENERS.each { |s| s.close rescue nil }.clear
110 # maps models to default worker counts, default worker count numbers are
111 # pretty arbitrary and tuning them to your application and hardware is
113 MODEL_WORKER_CONNECTIONS = {
114 :Base => 1, # this one can't change
115 :WriterThreadPool => 20,
116 :WriterThreadSpawn => 20,
121 :RevThreadSpawn => 50,
122 :RevThreadPool => 50,
123 :RevFiberSpawn => 50,
125 :CoolioThreadSpawn => 50,
126 :CoolioThreadPool => 50,
127 :CoolioFiberSpawn => 50,
136 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
137 autoload model, "rainbows/#{u.downcase!}"
140 autoload :Fiber, 'rainbows/fiber' # core class
141 autoload :StreamFile, 'rainbows/stream_file'
142 autoload :HttpResponse, 'rainbows/http_response' # deprecated
143 autoload :ThreadTimeout, 'rainbows/thread_timeout'
144 autoload :WorkerYield, 'rainbows/worker_yield'
145 autoload :SyncClose, 'rainbows/sync_close'
146 autoload :ReverseProxy, 'rainbows/reverse_proxy'
147 autoload :JoinThreads, 'rainbows/join_threads'
150 require 'rainbows/error'
151 require 'rainbows/configurator'