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 https://yhbt.net/rainbows/ 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/
14 FD_MAP = {}.compare_by_identity
16 require 'rainbows/const'
17 require 'rainbows/http_parser'
18 require 'rainbows/http_server'
19 autoload :Response, 'rainbows/response'
20 autoload :ProcessClient, 'rainbows/process_client'
21 autoload :Client, 'rainbows/client'
22 autoload :Base, 'rainbows/base'
23 autoload :Sendfile, 'rainbows/sendfile'
24 autoload :AppPool, 'rainbows/app_pool'
25 autoload :DevFdResponse, 'rainbows/dev_fd_response'
26 autoload :MaxBody, 'rainbows/max_body'
27 autoload :QueuePool, 'rainbows/queue_pool'
28 autoload :EvCore, 'rainbows/ev_core'
29 autoload :SocketProxy, 'rainbows/socket_proxy'
32 # Sleeps the current application dispatch. This will pick the
33 # optimal method to sleep depending on the concurrency model chosen
34 # (which may still suck and block the entire process). Using this
35 # with the basic :Coolio or :EventMachine models is not recommended.
36 # This should be used within your Rack application.
37 def self.sleep(seconds)
38 case Rainbows.server.use
39 when :FiberPool, :FiberSpawn
40 Rainbows::Fiber.sleep(seconds)
41 when :RevFiberSpawn, :CoolioFiberSpawn
42 Rainbows::Fiber::Coolio::Sleeper.new(seconds)
53 attr_accessor :cur # may not always be used
60 def self.config!(mod, *opts)
61 @forked or abort "#{mod} should only be loaded in a worker process"
63 mod.const_set(opt.to_s.upcase, Rainbows.server.__send__(opt))
72 def self.at_quit(&block)
77 @worker.tick = now.to_i
78 exit!(2) if @expire && now >= @expire
79 @alive && @server.master_pid == Process.ppid or quit!
89 Rainbows::HttpParser.quit
90 @expire = now + (@server.timeout * 2.0)
93 tmp.each { |s| s.close rescue nil }.clear
96 # XXX hack to break out of IO.select in worker_loop for some models
97 Process.kill(:QUIT, $$)
102 # try to use the monotonic clock in Ruby >= 2.1, it is immune to clock
103 # offset adjustments and generates less garbage (Float vs Time object)
105 Process.clock_gettime(Process::CLOCK_MONOTONIC)
107 Process.clock_gettime(Process::CLOCK_MONOTONIC)
109 rescue NameError, NoMethodError
110 def self.now # Ruby <= 2.0
115 autoload :Base, "rainbows/base"
116 autoload :WriterThreadPool, "rainbows/writer_thread_pool"
117 autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"
118 autoload :Revactor, "rainbows/revactor"
119 autoload :ThreadSpawn, "rainbows/thread_spawn"
120 autoload :ThreadPool, "rainbows/thread_pool"
121 autoload :Rev, "rainbows/rev"
122 autoload :RevThreadSpawn, "rainbows/rev_thread_spawn"
123 autoload :RevThreadPool, "rainbows/rev_thread_pool"
124 autoload :RevFiberSpawn, "rainbows/rev_fiber_spawn"
125 autoload :Coolio, "rainbows/coolio"
126 autoload :CoolioThreadSpawn, "rainbows/coolio_thread_spawn"
127 autoload :CoolioThreadPool, "rainbows/coolio_thread_pool"
128 autoload :CoolioFiberSpawn, "rainbows/coolio_fiber_spawn"
129 autoload :Epoll, "rainbows/epoll"
130 autoload :XEpoll, "rainbows/xepoll"
131 autoload :EventMachine, "rainbows/event_machine"
132 autoload :FiberSpawn, "rainbows/fiber_spawn"
133 autoload :FiberPool, "rainbows/fiber_pool"
134 autoload :ActorSpawn, "rainbows/actor_spawn"
135 autoload :NeverBlock, "rainbows/never_block"
136 autoload :XEpollThreadSpawn, "rainbows/xepoll_thread_spawn"
137 autoload :XEpollThreadPool, "rainbows/xepoll_thread_pool"
138 autoload :StreamResponseEpoll, "rainbows/stream_response_epoll"
140 autoload :Fiber, 'rainbows/fiber' # core class
141 autoload :StreamFile, 'rainbows/stream_file'
142 autoload :ThreadTimeout, 'rainbows/thread_timeout'
143 autoload :WorkerYield, 'rainbows/worker_yield'
144 autoload :SyncClose, 'rainbows/sync_close'
145 autoload :ReverseProxy, 'rainbows/reverse_proxy'
146 autoload :JoinThreads, 'rainbows/join_threads'
147 autoload :PoolSize, 'rainbows/pool_size'
150 require 'rainbows/error'
151 require 'rainbows/configurator'
154 # this interferes with Rainbows::Client creation with unicorn 5.3
157 TCPSrv = Kgio::TCPServer
158 rescue NameError # unicorn < 5.3.0