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 require 'rainbows/response'
43 require 'rainbows/client'
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'
51 autoload :EvCore, 'rainbows/ev_core'
52 autoload :SocketProxy, 'rainbows/socket_proxy'
56 # Sleeps the current application dispatch. This will pick the
57 # optimal method to sleep depending on the concurrency model chosen
58 # (which may still suck and block the entire process). Using this
59 # with the basic :Coolio or :EventMachine models is not recommended.
60 # This should be used within your Rack application.
63 when :FiberPool, :FiberSpawn
64 Rainbows::Fiber.sleep(nr)
65 when :RevFiberSpawn, :CoolioFiberSpawn
66 Rainbows::Fiber::Coolio::Sleeper.new(nr)
74 # runs the Rainbows! HttpServer with +app+ and +options+ and does
75 # not return until the server has exited.
76 def run(app, options = {}) # :nodoc:
77 HttpServer.new(app, options).start.join
81 # the default max body size is 1 megabyte (1024 * 1024 bytes)
82 @@max_bytes = 1024 * 1024
84 def max_bytes; @@max_bytes; end
85 def max_bytes=(nr); @@max_bytes = nr; end
90 # maps models to default worker counts, default worker count numbers are
91 # pretty arbitrary and tuning them to your application and hardware is
93 MODEL_WORKER_CONNECTIONS = {
94 :Base => 1, # this one can't change
95 :WriterThreadPool => 20,
96 :WriterThreadSpawn => 20,
101 :RevThreadSpawn => 50,
102 :RevThreadPool => 50,
103 :RevFiberSpawn => 50,
105 :CoolioThreadSpawn => 50,
106 :CoolioThreadPool => 50,
107 :CoolioFiberSpawn => 50,
114 u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
115 autoload model, "rainbows/#{u.downcase!}"
118 autoload :Fiber, 'rainbows/fiber' # core class
119 autoload :StreamFile, 'rainbows/stream_file'
120 autoload :HttpResponse, 'rainbows/http_response' # deprecated
121 autoload :ThreadTimeout, 'rainbows/thread_timeout'
122 autoload :WorkerYield, 'rainbows/worker_yield'
123 autoload :SyncClose, 'rainbows/sync_close'
126 require 'rainbows/error'
127 require 'rainbows/configurator'