xepoll_thread_spawn: initial implementation
[rainbows.git] / lib / rainbows.rb
blobfccfe8ba2e7547baba1c135085f5d447e5f9a886
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 module Rainbows
20   O = {} # :nodoc:
22   # map of numeric file descriptors to IO objects to avoid using IO.new
23   # and potentially causing race conditions when using /dev/fd/
24   FD_MAP = {}
25   FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
27   # :startdoc:
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   # 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(nr)
50     case Rainbows.server.use
51     when :FiberPool, :FiberSpawn
52       Rainbows::Fiber.sleep(nr)
53     when :RevFiberSpawn, :CoolioFiberSpawn
54       Rainbows::Fiber::Coolio::Sleeper.new(nr)
55     when :Revactor
56       Actor.sleep(nr)
57     else
58       Kernel.sleep(nr)
59     end
60   end
62   # runs the Rainbows! HttpServer with +app+ and +options+ and does
63   # not return until the server has exited.
64   def self.run(app, options = {}) # :nodoc:
65     HttpServer.new(app, options).start.join
66   end
68   # :stopdoc:
69   class << self
70     attr_accessor :max_bytes, :keepalive_timeout
71     attr_accessor :server
72     attr_accessor :cur # may not always be used
73     attr_reader :alive
74     attr_writer :tick_io
75   end
76   # :startdoc:
78   # the default max body size is 1 megabyte (1024 * 1024 bytes)
79   @max_bytes = 1024 * 1024
81   # the default keepalive_timeout is 5 seconds
82   @keepalive_timeout = 5
84   # :stopdoc:
85   @alive = true
86   @cur = 0
87   @tick_mod = 0
88   @expire = nil
90   def self.tick
91     @tick_io.chmod(@tick_mod = 0 == @tick_mod ? 1 : 0)
92     exit!(2) if @expire && Time.now >= @expire
93     @alive && @server.master_pid == Process.ppid or quit!
94   end
96   def self.cur_alive
97     @alive || @cur > 0
98   end
100   def self.quit!
101     unless @expire
102       @alive = false
103       Rainbows::HttpParser.quit
104       @expire = Time.now + (@server.timeout * 2.0)
105       Unicorn::HttpServer::LISTENERS.each { |s| s.close rescue nil }.clear
106     end
107     false
108   end
110   # maps models to default worker counts, default worker count numbers are
111   # pretty arbitrary and tuning them to your application and hardware is
112   # highly recommended
113   MODEL_WORKER_CONNECTIONS = {
114     :Base => 1, # this one can't change
115     :WriterThreadPool => 50,
116     :WriterThreadSpawn => 50,
117     :Revactor => 50,
118     :ThreadSpawn => 50,
119     :ThreadPool => 50,
120     :Rev => 50,
121     :RevThreadSpawn => 50,
122     :RevThreadPool => 50,
123     :RevFiberSpawn => 50,
124     :Coolio => 50,
125     :CoolioThreadSpawn => 50,
126     :CoolioThreadPool => 50,
127     :CoolioFiberSpawn => 50,
128     :Epoll => 50,
129     :XEpoll => 50,
130     :EventMachine => 50,
131     :FiberSpawn => 50,
132     :FiberPool => 50,
133     :ActorSpawn => 50,
134     :NeverBlock => 50,
135     :XEpollThreadSpawn => 50,
136   }.each do |model, _|
137     u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
138     autoload model, "rainbows/#{u.downcase!}"
139   end
140   # :startdoc:
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'
151 require 'rainbows/error'
152 require 'rainbows/configurator'