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 # See http://rainbows.bogomips.org/ for documentation
12 # map of numeric file descriptors to IO objects to avoid using IO.new
13 # and potentially causing race conditions when using /dev/fd/
15 FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
17 require 'rainbows/const'
18 require 'rainbows/http_parser'
19 require 'rainbows/http_server'
20 autoload :Response, 'rainbows/response'
21 autoload :ProcessClient, 'rainbows/process_client'
22 autoload :Client, 'rainbows/client'
23 autoload :Base, 'rainbows/base'
24 autoload :Sendfile, 'rainbows/sendfile'
25 autoload :AppPool, 'rainbows/app_pool'
26 autoload :DevFdResponse, 'rainbows/dev_fd_response'
27 autoload :MaxBody, 'rainbows/max_body'
28 autoload :QueuePool, 'rainbows/queue_pool'
29 autoload :EvCore, 'rainbows/ev_core'
30 autoload :SocketProxy, 'rainbows/socket_proxy'
33 # Sleeps the current application dispatch. This will pick the
34 # optimal method to sleep depending on the concurrency model chosen
35 # (which may still suck and block the entire process). Using this
36 # with the basic :Coolio or :EventMachine models is not recommended.
37 # This should be used within your Rack application.
38 def self.sleep(seconds)
39 case Rainbows.server.use
40 when :FiberPool, :FiberSpawn
41 Rainbows::Fiber.sleep(seconds)
42 when :RevFiberSpawn, :CoolioFiberSpawn
43 Rainbows::Fiber::Coolio::Sleeper.new(seconds)
54 attr_accessor :cur # may not always be used
61 def self.config!(mod, *opts)
62 @forked or abort "#{mod} should only be loaded in a worker process"
64 mod.const_set(opt.to_s.upcase, Rainbows.server.__send__(opt))
73 def self.at_quit(&block)
78 @worker.tick = now.to_i
79 exit!(2) if @expire && now >= @expire
80 @alive && @server.master_pid == Process.ppid or quit!
90 Rainbows::HttpParser.quit
91 @expire = now + (@server.timeout * 2.0)
94 tmp.each { |s| s.close rescue nil }.clear
95 @at_quit.each { |task| task.call }
97 # XXX hack to break out of IO.select in worker_loop for some models
98 Process.kill(:QUIT, $$)
103 # try to use the monotonic clock in Ruby >= 2.1, it is immune to clock
104 # offset adjustments and generates less garbage (Float vs Time object)
106 Process.clock_gettime(Process::CLOCK_MONOTONIC)
108 Process.clock_gettime(Process::CLOCK_MONOTONIC)
110 rescue NameError, NoMethodError
111 def self.now # Ruby <= 2.0
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 :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'
148 autoload :PoolSize, 'rainbows/pool_size'
151 require 'rainbows/error'
152 require 'rainbows/configurator'