1 # -*- encoding: binary -*-
4 # the value passed to TCP_DEFER_ACCEPT actually matters in Linux 2.6.32+
5 Unicorn::SocketHelper::DEFAULTS[:tcp_defer_accept] = 60
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
13 class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp,:expire)
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!
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 }
28 G = State.new(true, 0, 0, 5)
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/
35 FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
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'
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.
65 when :FiberPool, :FiberSpawn
66 Rainbows::Fiber.sleep(nr)
67 when :RevFiberSpawn, :CoolioFiberSpawn
68 Rainbows::Fiber::Coolio::Sleeper.new(nr)
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
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
92 # maps models to default worker counts, default worker count numbers are
93 # pretty arbitrary and tuning them to your application and hardware is
95 MODEL_WORKER_CONNECTIONS = {
96 :Base => 1, # this one can't change
97 :WriterThreadPool => 20,
98 :WriterThreadSpawn => 20,
103 :RevThreadSpawn => 50,
104 :RevThreadPool => 50,
105 :RevFiberSpawn => 50,
107 :CoolioThreadSpawn => 50,
108 :CoolioThreadPool => 50,
109 :CoolioFiberSpawn => 50,
116 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
117 autoload model, "rainbows/#{u.downcase!}"
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'