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