initial XAcceptEpoll concurrency model
[rainbows.git] / lib / rainbows.rb
blobf22a927502f60e352805c41622b1163f624971df
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,
14 module Rainbows
16   O = {} # :nodoc:
18   # map of numeric file descriptors to IO objects to avoid using IO.new
19   # and potentially causing race conditions when using /dev/fd/
20   FD_MAP = {}
21   FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
23   # :startdoc:
25   require 'rainbows/const'
26   require 'rainbows/http_parser'
27   require 'rainbows/http_server'
28   autoload :Response, 'rainbows/response'
29   autoload :ProcessClient, 'rainbows/process_client'
30   autoload :Client, 'rainbows/client'
31   autoload :Base, 'rainbows/base'
32   autoload :Sendfile, 'rainbows/sendfile'
33   autoload :AppPool, 'rainbows/app_pool'
34   autoload :DevFdResponse, 'rainbows/dev_fd_response'
35   autoload :MaxBody, 'rainbows/max_body'
36   autoload :QueuePool, 'rainbows/queue_pool'
37   autoload :EvCore, 'rainbows/ev_core'
38   autoload :SocketProxy, 'rainbows/socket_proxy'
40   # Sleeps the current application dispatch.  This will pick the
41   # optimal method to sleep depending on the concurrency model chosen
42   # (which may still suck and block the entire process).  Using this
43   # with the basic :Coolio or :EventMachine models is not recommended.
44   # This should be used within your Rack application.
45   def self.sleep(nr)
46     case Rainbows.server.use
47     when :FiberPool, :FiberSpawn
48       Rainbows::Fiber.sleep(nr)
49     when :RevFiberSpawn, :CoolioFiberSpawn
50       Rainbows::Fiber::Coolio::Sleeper.new(nr)
51     when :Revactor
52       Actor.sleep(nr)
53     else
54       Kernel.sleep(nr)
55     end
56   end
58   # runs the Rainbows! HttpServer with +app+ and +options+ and does
59   # not return until the server has exited.
60   def self.run(app, options = {}) # :nodoc:
61     HttpServer.new(app, options).start.join
62   end
64   # :stopdoc:
65   class << self
66     attr_accessor :max_bytes, :keepalive_timeout
67     attr_accessor :server
68     attr_accessor :cur # may not always be used
69     attr_reader :alive
70     attr_writer :tick_io
71   end
72   # :startdoc:
74   # the default max body size is 1 megabyte (1024 * 1024 bytes)
75   @max_bytes = 1024 * 1024
77   # the default keepalive_timeout is 5 seconds
78   @keepalive_timeout = 5
80   # :stopdoc:
81   @alive = true
82   @cur = 0
83   @tick_mod = 0
84   @expire = nil
86   def self.tick
87     @tick_io.chmod(@tick_mod = 0 == @tick_mod ? 1 : 0)
88     exit!(2) if @expire && Time.now >= @expire
89     @alive && @server.master_pid == Process.ppid or quit!
90   end
92   def self.cur_alive
93     @alive || @cur > 0
94   end
96   def self.quit!
97     @alive = false
98     Rainbows::HttpParser.quit
99     @expire ||= Time.now + (@server.timeout * 2.0)
100     @server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }.clear
101     false
102   end
104   # maps models to default worker counts, default worker count numbers are
105   # pretty arbitrary and tuning them to your application and hardware is
106   # highly recommended
107   MODEL_WORKER_CONNECTIONS = {
108     :Base => 1, # this one can't change
109     :WriterThreadPool => 20,
110     :WriterThreadSpawn => 20,
111     :Revactor => 50,
112     :ThreadSpawn => 30,
113     :ThreadPool => 20,
114     :Rev => 50,
115     :RevThreadSpawn => 50,
116     :RevThreadPool => 50,
117     :RevFiberSpawn => 50,
118     :Coolio => 50,
119     :CoolioThreadSpawn => 50,
120     :CoolioThreadPool => 50,
121     :CoolioFiberSpawn => 50,
122     :Epoll => 50,
123     :XAcceptEpoll => 50,
124     :EventMachine => 50,
125     :FiberSpawn => 50,
126     :FiberPool => 50,
127     :ActorSpawn => 50,
128     :NeverBlock => 50,
129   }.each do |model, _|
130     u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
131     autoload model, "rainbows/#{u.downcase!}"
132   end
133   # :startdoc:
134   autoload :Fiber, 'rainbows/fiber' # core class
135   autoload :StreamFile, 'rainbows/stream_file'
136   autoload :HttpResponse, 'rainbows/http_response' # deprecated
137   autoload :ThreadTimeout, 'rainbows/thread_timeout'
138   autoload :WorkerYield, 'rainbows/worker_yield'
139   autoload :SyncClose, 'rainbows/sync_close'
142 require 'rainbows/error'
143 require 'rainbows/configurator'