add XEpollThreadPool concurrency option
[rainbows.git] / lib / rainbows.rb
blob0b663ba3ac3b51fd12650ad786000c5170170fb3
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 module Rainbows
21   O = {} # :nodoc:
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   # :startdoc:
30   require 'rainbows/const'
31   require 'rainbows/http_parser'
32   require 'rainbows/http_server'
33   autoload :Response, 'rainbows/response'
34   autoload :ProcessClient, 'rainbows/process_client'
35   autoload :Client, 'rainbows/client'
36   autoload :Base, 'rainbows/base'
37   autoload :Sendfile, 'rainbows/sendfile'
38   autoload :AppPool, 'rainbows/app_pool'
39   autoload :DevFdResponse, 'rainbows/dev_fd_response'
40   autoload :MaxBody, 'rainbows/max_body'
41   autoload :QueuePool, 'rainbows/queue_pool'
42   autoload :EvCore, 'rainbows/ev_core'
43   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(nr)
51     case Rainbows.server.use
52     when :FiberPool, :FiberSpawn
53       Rainbows::Fiber.sleep(nr)
54     when :RevFiberSpawn, :CoolioFiberSpawn
55       Rainbows::Fiber::Coolio::Sleeper.new(nr)
56     when :Revactor
57       Actor.sleep(nr)
58     else
59       Kernel.sleep(nr)
60     end
61   end
63   # runs the Rainbows! HttpServer with +app+ and +options+ and does
64   # not return until the server has exited.
65   def self.run(app, options = {}) # :nodoc:
66     HttpServer.new(app, options).start.join
67   end
69   # :stopdoc:
70   class << self
71     attr_accessor :client_header_buffer_size
72     attr_accessor :client_max_body_size
73     attr_accessor :keepalive_timeout
74     attr_accessor :server
75     attr_accessor :cur # may not always be used
76     attr_reader :alive
77     attr_writer :tick_io
78   end
79   # :startdoc:
81   def self.defaults!
82     # the default max body size is 1 megabyte (1024 * 1024 bytes)
83     @client_max_body_size = 1024 * 1024
85     # the default keepalive_timeout is 5 seconds
86     @keepalive_timeout = 5
88     # 1024 bytes matches nginx, though Rails session cookies will typically
89     # need >= 1500...
90     @client_header_buffer_size = 1024
91   end
93   defaults!
95   # :stopdoc:
96   @alive = true
97   @cur = 0
98   @tick_mod = 0
99   @expire = nil
101   def self.tick
102     @tick_io.chmod(@tick_mod = 0 == @tick_mod ? 1 : 0)
103     exit!(2) if @expire && Time.now >= @expire
104     @alive && @server.master_pid == Process.ppid or quit!
105   end
107   def self.cur_alive
108     @alive || @cur > 0
109   end
111   def self.quit!
112     unless @expire
113       @alive = false
114       Rainbows::HttpParser.quit
115       @expire = Time.now + (@server.timeout * 2.0)
116       Unicorn::HttpServer::LISTENERS.each { |s| s.close rescue nil }.clear
117     end
118     false
119   end
121   autoload :Base, "rainbows/base"
122   autoload :WriterThreadPool, "rainbows/writer_thread_pool"
123   autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"
124   autoload :Revactor, "rainbows/revactor"
125   autoload :ThreadSpawn, "rainbows/thread_spawn"
126   autoload :ThreadPool, "rainbows/thread_pool"
127   autoload :Rev, "rainbows/rev"
128   autoload :RevThreadSpawn, "rainbows/rev_thread_spawn"
129   autoload :RevThreadPool, "rainbows/rev_thread_pool"
130   autoload :RevFiberSpawn, "rainbows/rev_fiber_spawn"
131   autoload :Coolio, "rainbows/coolio"
132   autoload :CoolioThreadSpawn, "rainbows/coolio_thread_spawn"
133   autoload :CoolioThreadPool, "rainbows/coolio_thread_pool"
134   autoload :CoolioFiberSpawn, "rainbows/coolio_fiber_spawn"
135   autoload :Epoll, "rainbows/epoll"
136   autoload :XEpoll, "rainbows/xepoll"
137   autoload :EventMachine, "rainbows/event_machine"
138   autoload :FiberSpawn, "rainbows/fiber_spawn"
139   autoload :FiberPool, "rainbows/fiber_pool"
140   autoload :ActorSpawn, "rainbows/actor_spawn"
141   autoload :NeverBlock, "rainbows/never_block"
142   autoload :XEpollThreadSpawn, "rainbows/xepoll_thread_spawn"
143   autoload :XEpollThreadPool, "rainbows/xepoll_thread_pool"
145   # :startdoc:
146   autoload :Fiber, 'rainbows/fiber' # core class
147   autoload :StreamFile, 'rainbows/stream_file'
148   autoload :HttpResponse, 'rainbows/http_response' # deprecated
149   autoload :ThreadTimeout, 'rainbows/thread_timeout'
150   autoload :WorkerYield, 'rainbows/worker_yield'
151   autoload :SyncClose, 'rainbows/sync_close'
152   autoload :ReverseProxy, 'rainbows/reverse_proxy'
153   autoload :JoinThreads, 'rainbows/join_threads'
156 require 'rainbows/error'
157 require 'rainbows/configurator'