unindent most files
[rainbows.git] / lib / rainbows.rb
blob7a2923257ffce884587b479c2d1700ac5e5954f7
1 # -*- encoding: binary -*-
2 require 'kgio'
3 require 'unicorn'
4 # the value passed to TCP_DEFER_ACCEPT actually matters in Linux 2.6.32+
5 Unicorn::SocketHelper::DEFAULTS[:tcp_defer_accept] = 60
7 module Rainbows
9   # global vars because class/instance variables are confusing me :<
10   # this struct is only accessed inside workers and thus private to each
11   # G.cur may not be used in the network concurrency model
12   # :stopdoc:
13   class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp,:expire)
14     def tick
15       tmp.chmod(self.m = m == 0 ? 1 : 0)
16       exit!(2) if expire && Time.now >= expire
17       alive && server.master_pid == Process.ppid or quit!
18     end
20     def quit!
21       self.alive = false
22       self.expire ||= Time.now + (server.timeout * 2.0)
23       server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
24       false
25     end
26   end
27   G = State.new(true, 0, 0, 5)
28   O = {}
29   class Response416 < RangeError; end
31   # map of numeric file descriptors to IO objects to avoid using IO.new
32   # and potentially causing race conditions when using /dev/fd/
33   FD_MAP = {}
34   FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
36   # :startdoc:
38   require 'rainbows/const'
39   require 'rainbows/http_server'
40   require 'rainbows/response'
41   require 'rainbows/client'
42   require 'rainbows/http_request'
43   require 'rainbows/tee_input'
44   require 'rainbows/process_client'
45   autoload :Base, 'rainbows/base'
46   autoload :Sendfile, 'rainbows/sendfile'
47   autoload :AppPool, 'rainbows/app_pool'
48   autoload :DevFdResponse, 'rainbows/dev_fd_response'
49   autoload :MaxBody, 'rainbows/max_body'
50   autoload :QueuePool, 'rainbows/queue_pool'
52   class << self
54     # Sleeps the current application dispatch.  This will pick the
55     # optimal method to sleep depending on the concurrency model chosen
56     # (which may still suck and block the entire process).  Using this
57     # with the basic :Rev or :EventMachine models is not recommended.
58     # This should be used within your Rack application.
59     def sleep(nr)
60       case G.server.use
61       when :FiberPool, :FiberSpawn
62         Rainbows::Fiber.sleep(nr)
63       when :RevFiberSpawn
64         Rainbows::Fiber::Rev::Sleeper.new(nr)
65       when :Revactor
66         Actor.sleep(nr)
67       else
68         Kernel.sleep(nr)
69       end
70     end
72     # runs the Rainbows! HttpServer with +app+ and +options+ and does
73     # not return until the server has exited.
74     def run(app, options = {}) # :nodoc:
75       HttpServer.new(app, options).start.join
76     end
78     # :stopdoc:
79     # the default max body size is 1 megabyte (1024 * 1024 bytes)
80     @@max_bytes = 1024 * 1024
82     def max_bytes; @@max_bytes; end
83     def max_bytes=(nr); @@max_bytes = nr; end
84     # :startdoc:
85   end
87   # :stopdoc:
88   # maps models to default worker counts, default worker count numbers are
89   # pretty arbitrary and tuning them to your application and hardware is
90   # highly recommended
91   MODEL_WORKER_CONNECTIONS = {
92     :Base => 1, # this one can't change
93     :WriterThreadPool => 20,
94     :WriterThreadSpawn => 20,
95     :Revactor => 50,
96     :ThreadSpawn => 30,
97     :ThreadPool => 20,
98     :Rev => 50,
99     :RevThreadSpawn => 50,
100     :RevThreadPool => 50,
101     :EventMachine => 50,
102     :FiberSpawn => 50,
103     :FiberPool => 50,
104     :ActorSpawn => 50,
105     :NeverBlock => 50,
106     :RevFiberSpawn => 50,
107   }.each do |model, _|
108     u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
109     autoload model, "rainbows/#{u.downcase!}"
110   end
111   # :startdoc:
112   autoload :Fiber, 'rainbows/fiber' # core class
113   autoload :StreamFile, 'rainbows/stream_file'
114   autoload :HttpResponse, 'rainbows/http_response' # deprecated
115   autoload :ThreadTimeout, 'rainbows/thread_timeout'
118 require 'rainbows/error'
119 require 'rainbows/configurator'