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 self.expire ||= Time.now + (server.timeout * 2.0)
23 server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
27 G = State.new(true, 0, 0, 5)
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/
34 FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
38 require 'rainbows/const'
39 require 'rainbows/http_server'
40 require 'rainbows/response'
41 require 'rainbows/client'
42 require 'rainbows/process_client'
43 autoload :Base, 'rainbows/base'
44 autoload :Sendfile, 'rainbows/sendfile'
45 autoload :AppPool, 'rainbows/app_pool'
46 autoload :DevFdResponse, 'rainbows/dev_fd_response'
47 autoload :MaxBody, 'rainbows/max_body'
48 autoload :QueuePool, 'rainbows/queue_pool'
49 autoload :EvCore, 'rainbows/ev_core'
50 autoload :SocketProxy, 'rainbows/socket_proxy'
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.
61 when :FiberPool, :FiberSpawn
62 Rainbows::Fiber.sleep(nr)
64 Rainbows::Fiber::Rev::Sleeper.new(nr)
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
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
88 # maps models to default worker counts, default worker count numbers are
89 # pretty arbitrary and tuning them to your application and hardware is
91 MODEL_WORKER_CONNECTIONS = {
92 :Base => 1, # this one can't change
93 :WriterThreadPool => 20,
94 :WriterThreadSpawn => 20,
99 :RevThreadSpawn => 50,
100 :RevThreadPool => 50,
106 :RevFiberSpawn => 50,
108 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
109 autoload model, "rainbows/#{u.downcase!}"
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'
116 autoload :WorkerYield, 'rainbows/worker_yield'
119 require 'rainbows/error'
120 require 'rainbows/configurator'