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