remove unnecessary io/wait require
[rainbows.git] / lib / rainbows.rb
blob84604b2a70e25adfb65538f1b7373aa7a0b6f19b
1 # -*- encoding: binary -*-
2 require 'kgio'
3 require 'unicorn'
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
11   :tcp_nodelay => true,
13   # we always want to send our headers out ASAP since Rainbows!
14   # is designed for apps that could trickle out the body slowly
15   :tcp_nopush => false,
18 # See http://rainbows.rubyforge.org/ for documentation
19 module Rainbows
20   # :stopdoc:
21   O = {}
23   # map of numeric file descriptors to IO objects to avoid using IO.new
24   # and potentially causing race conditions when using /dev/fd/
25   FD_MAP = {}
26   FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
28   require 'rainbows/const'
29   require 'rainbows/http_parser'
30   require 'rainbows/http_server'
31   autoload :Response, 'rainbows/response'
32   autoload :ProcessClient, 'rainbows/process_client'
33   autoload :Client, 'rainbows/client'
34   autoload :Base, 'rainbows/base'
35   autoload :Sendfile, 'rainbows/sendfile'
36   autoload :AppPool, 'rainbows/app_pool'
37   autoload :DevFdResponse, 'rainbows/dev_fd_response'
38   autoload :MaxBody, 'rainbows/max_body'
39   autoload :QueuePool, 'rainbows/queue_pool'
40   autoload :EvCore, 'rainbows/ev_core'
41   autoload :SocketProxy, 'rainbows/socket_proxy'
43   # :startdoc:
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.
49   def self.sleep(seconds)
50     case Rainbows.server.use
51     when :FiberPool, :FiberSpawn
52       Rainbows::Fiber.sleep(seconds)
53     when :RevFiberSpawn, :CoolioFiberSpawn
54       Rainbows::Fiber::Coolio::Sleeper.new(seconds)
55     when :Revactor
56       Actor.sleep(seconds)
57     else
58       Kernel.sleep(seconds)
59     end
60   end
61   # :stopdoc:
63   class << self
64     attr_accessor :server
65     attr_accessor :cur # may not always be used
66     attr_reader :alive
67     attr_writer :worker
68     attr_writer :forked
69   end
71   def self.config!(mod, *opts)
72     @forked or abort "#{mod} should only be loaded in a worker process"
73     opts.each do |opt|
74       mod.const_set(opt.to_s.upcase, Rainbows.server.__send__(opt))
75     end
76   end
78   @alive = true
79   @cur = 0
80   @expire = nil
81   @at_quit = []
83   def self.at_quit(&block)
84     @at_quit << block
85   end
87   def self.tick
88     @worker.tick = Time.now.to_i
89     exit!(2) if @expire && Time.now >= @expire
90     @alive && @server.master_pid == Process.ppid or quit!
91   end
93   def self.cur_alive
94     @alive || @cur > 0
95   end
97   def self.quit!
98     unless @expire
99       @alive = false
100       Rainbows::HttpParser.quit
101       @expire = Time.now + (@server.timeout * 2.0)
102       Unicorn::HttpServer::LISTENERS.each { |s| s.close rescue nil }.clear
103       @at_quit.each { |task| task.call }
104     end
105     false
106   end
108   autoload :Base, "rainbows/base"
109   autoload :WriterThreadPool, "rainbows/writer_thread_pool"
110   autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"
111   autoload :Revactor, "rainbows/revactor"
112   autoload :ThreadSpawn, "rainbows/thread_spawn"
113   autoload :ThreadPool, "rainbows/thread_pool"
114   autoload :Rev, "rainbows/rev"
115   autoload :RevThreadSpawn, "rainbows/rev_thread_spawn"
116   autoload :RevThreadPool, "rainbows/rev_thread_pool"
117   autoload :RevFiberSpawn, "rainbows/rev_fiber_spawn"
118   autoload :Coolio, "rainbows/coolio"
119   autoload :CoolioThreadSpawn, "rainbows/coolio_thread_spawn"
120   autoload :CoolioThreadPool, "rainbows/coolio_thread_pool"
121   autoload :CoolioFiberSpawn, "rainbows/coolio_fiber_spawn"
122   autoload :Epoll, "rainbows/epoll"
123   autoload :XEpoll, "rainbows/xepoll"
124   autoload :EventMachine, "rainbows/event_machine"
125   autoload :FiberSpawn, "rainbows/fiber_spawn"
126   autoload :FiberPool, "rainbows/fiber_pool"
127   autoload :ActorSpawn, "rainbows/actor_spawn"
128   autoload :NeverBlock, "rainbows/never_block"
129   autoload :XEpollThreadSpawn, "rainbows/xepoll_thread_spawn"
130   autoload :XEpollThreadPool, "rainbows/xepoll_thread_pool"
131   autoload :StreamResponseEpoll, "rainbows/stream_response_epoll"
133   autoload :Fiber, 'rainbows/fiber' # core class
134   autoload :StreamFile, 'rainbows/stream_file'
135   autoload :HttpResponse, 'rainbows/http_response' # deprecated
136   autoload :ThreadTimeout, 'rainbows/thread_timeout'
137   autoload :WorkerYield, 'rainbows/worker_yield'
138   autoload :SyncClose, 'rainbows/sync_close'
139   autoload :ReverseProxy, 'rainbows/reverse_proxy'
140   autoload :JoinThreads, 'rainbows/join_threads'
141   autoload :PoolSize, 'rainbows/pool_size'
144 require 'rainbows/error'
145 require 'rainbows/configurator'