1 # -*- encoding: binary -*-
5 Unicorn::SocketHelper::DEFAULTS.merge!({
6 # the value passed to TCP_DEFER_ACCEPT actually matters in Linux 2.6.32+
7 :tcp_defer_accept => 60,
9 # keep-alive performance sucks without this due to
10 # write(headers)-write(body)-read
11 # because we always write headers and bodies with two calls
14 # we always want to send our headers out ASAP since Rainbows!
15 # is designed for apps that could trickle out the body slowly
19 # See http://rainbows.rubyforge.org/ for documentation
24 # map of numeric file descriptors to IO objects to avoid using IO.new
25 # and potentially causing race conditions when using /dev/fd/
27 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'
45 # Sleeps the current application dispatch. This will pick the
46 # optimal method to sleep depending on the concurrency model chosen
47 # (which may still suck and block the entire process). Using this
48 # with the basic :Coolio or :EventMachine models is not recommended.
49 # This should be used within your Rack application.
50 def self.sleep(seconds)
51 case Rainbows.server.use
52 when :FiberPool, :FiberSpawn
53 Rainbows::Fiber.sleep(seconds)
54 when :RevFiberSpawn, :CoolioFiberSpawn
55 Rainbows::Fiber::Coolio::Sleeper.new(seconds)
64 # runs the Rainbows! HttpServer with +app+ and +options+ and does
65 # not return until the server has exited.
66 def self.run(app, options = {}) # :nodoc:
67 HttpServer.new(app, options).start.join
72 attr_accessor :cur # may not always be used
78 def self.config!(mod, *opts)
79 @forked or abort "#{mod} should only be loaded in a worker process"
81 mod.const_set(opt.to_s.upcase, Rainbows.server.__send__(opt))
91 def self.at_quit(&block)
96 @tick_io.chmod(@tick_mod = 0 == @tick_mod ? 1 : 0)
97 exit!(2) if @expire && Time.now >= @expire
98 @alive && @server.master_pid == Process.ppid or quit!
108 Rainbows::HttpParser.quit
109 @expire = Time.now + (@server.timeout * 2.0)
110 Unicorn::HttpServer::LISTENERS.each { |s| s.close rescue nil }.clear
111 @at_quit.each { |task| task.call }
116 autoload :Base, "rainbows/base"
117 autoload :WriterThreadPool, "rainbows/writer_thread_pool"
118 autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"
119 autoload :Revactor, "rainbows/revactor"
120 autoload :ThreadSpawn, "rainbows/thread_spawn"
121 autoload :ThreadPool, "rainbows/thread_pool"
122 autoload :Rev, "rainbows/rev"
123 autoload :RevThreadSpawn, "rainbows/rev_thread_spawn"
124 autoload :RevThreadPool, "rainbows/rev_thread_pool"
125 autoload :RevFiberSpawn, "rainbows/rev_fiber_spawn"
126 autoload :Coolio, "rainbows/coolio"
127 autoload :CoolioThreadSpawn, "rainbows/coolio_thread_spawn"
128 autoload :CoolioThreadPool, "rainbows/coolio_thread_pool"
129 autoload :CoolioFiberSpawn, "rainbows/coolio_fiber_spawn"
130 autoload :Epoll, "rainbows/epoll"
131 autoload :XEpoll, "rainbows/xepoll"
132 autoload :EventMachine, "rainbows/event_machine"
133 autoload :FiberSpawn, "rainbows/fiber_spawn"
134 autoload :FiberPool, "rainbows/fiber_pool"
135 autoload :ActorSpawn, "rainbows/actor_spawn"
136 autoload :NeverBlock, "rainbows/never_block"
137 autoload :XEpollThreadSpawn, "rainbows/xepoll_thread_spawn"
138 autoload :XEpollThreadPool, "rainbows/xepoll_thread_pool"
139 autoload :StreamResponseEpoll, "rainbows/stream_response_epoll"
141 autoload :Fiber, 'rainbows/fiber' # core class
142 autoload :StreamFile, 'rainbows/stream_file'
143 autoload :HttpResponse, 'rainbows/http_response' # deprecated
144 autoload :ThreadTimeout, 'rainbows/thread_timeout'
145 autoload :WorkerYield, 'rainbows/worker_yield'
146 autoload :SyncClose, 'rainbows/sync_close'
147 autoload :ReverseProxy, 'rainbows/reverse_proxy'
148 autoload :JoinThreads, 'rainbows/join_threads'
149 autoload :PoolSize, 'rainbows/pool_size'
152 require 'rainbows/error'
153 require 'rainbows/configurator'