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