stream_response_epoll: our most "special" concurrency option yet
[rainbows.git] / lib / rainbows.rb
blobbd2c1064ba62c780f17a52d6292e3d6e396915a8
1 # -*- encoding: binary -*-
2 require 'kgio'
3 require 'unicorn'
4 require 'io/wait'
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
12   :tcp_nodelay => true,
14   # we always want to send our headers out ASAP since Rainbows!
15   # is designed for apps that could trickle out the body slowly
16   :tcp_nopush => false,
19 # See http://rainbows.rubyforge.org/ for documentation
20 module Rainbows
21   # :stopdoc:
22   O = {}
24   # map of numeric file descriptors to IO objects to avoid using IO.new
25   # and potentially causing race conditions when using /dev/fd/
26   FD_MAP = {}
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'
44   # :startdoc:
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)
56     when :Revactor
57       Actor.sleep(seconds)
58     else
59       Kernel.sleep(seconds)
60     end
61   end
62   # :stopdoc:
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
68   end
70   class << self
71     attr_accessor :server
72     attr_accessor :cur # may not always be used
73     attr_reader :alive
74     attr_writer :tick_io
75     attr_writer :forked
76   end
78   def self.config!(mod, *opts)
79     @forked or abort "#{mod} should only be loaded in a worker process"
80     opts.each do |opt|
81       mod.const_set(opt.to_s.upcase, Rainbows.server.__send__(opt))
82     end
83   end
85   @alive = true
86   @cur = 0
87   @tick_mod = 0
88   @expire = nil
89   @at_quit = []
91   def self.at_quit(&block)
92     @at_quit << block
93   end
95   def self.tick
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!
99   end
101   def self.cur_alive
102     @alive || @cur > 0
103   end
105   def self.quit!
106     unless @expire
107       @alive = false
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 }
112     end
113     false
114   end
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'