s/max_bytes/client_max_body_size/ for consistency
[rainbows.git] / lib / rainbows.rb
blobe151a32786ecad88976c7d7cbdeed39d44c0d789
1 # -*- encoding: binary -*-
2 require 'kgio'
3 require 'unicorn'
4 Unicorn::SocketHelper::DEFAULTS.merge!({
5   # the value passed to TCP_DEFER_ACCEPT actually matters in Linux 2.6.32+
6   :tcp_defer_accept => 60,
8   # keep-alive performance sucks without this due to
9   # write(headers)-write(body)-read
10   # because we always write headers and bodies with two calls
11   :tcp_nodelay => true,
13   # we always want to send our headers out ASAP since Rainbows!
14   # is designed for apps that could trickle out the body slowly
15   :tcp_nopush => false,
18 module Rainbows
20   O = {} # :nodoc:
22   # map of numeric file descriptors to IO objects to avoid using IO.new
23   # and potentially causing race conditions when using /dev/fd/
24   FD_MAP = {}
25   FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
27   # :startdoc:
29   require 'rainbows/const'
30   require 'rainbows/http_parser'
31   require 'rainbows/http_server'
32   autoload :Response, 'rainbows/response'
33   autoload :ProcessClient, 'rainbows/process_client'
34   autoload :Client, 'rainbows/client'
35   autoload :Base, 'rainbows/base'
36   autoload :Sendfile, 'rainbows/sendfile'
37   autoload :AppPool, 'rainbows/app_pool'
38   autoload :DevFdResponse, 'rainbows/dev_fd_response'
39   autoload :MaxBody, 'rainbows/max_body'
40   autoload :QueuePool, 'rainbows/queue_pool'
41   autoload :EvCore, 'rainbows/ev_core'
42   autoload :SocketProxy, 'rainbows/socket_proxy'
44   # Sleeps the current application dispatch.  This will pick the
45   # optimal method to sleep depending on the concurrency model chosen
46   # (which may still suck and block the entire process).  Using this
47   # with the basic :Coolio or :EventMachine models is not recommended.
48   # This should be used within your Rack application.
49   def self.sleep(nr)
50     case Rainbows.server.use
51     when :FiberPool, :FiberSpawn
52       Rainbows::Fiber.sleep(nr)
53     when :RevFiberSpawn, :CoolioFiberSpawn
54       Rainbows::Fiber::Coolio::Sleeper.new(nr)
55     when :Revactor
56       Actor.sleep(nr)
57     else
58       Kernel.sleep(nr)
59     end
60   end
62   # runs the Rainbows! HttpServer with +app+ and +options+ and does
63   # not return until the server has exited.
64   def self.run(app, options = {}) # :nodoc:
65     HttpServer.new(app, options).start.join
66   end
68   # :stopdoc:
69   class << self
70     attr_accessor :client_header_buffer_size
71     attr_accessor :client_max_body_size
72     attr_accessor :keepalive_timeout
73     attr_accessor :server
74     attr_accessor :cur # may not always be used
75     attr_reader :alive
76     attr_writer :tick_io
77   end
78   # :startdoc:
80   # the default max body size is 1 megabyte (1024 * 1024 bytes)
81   @client_max_body_size = 1024 * 1024
83   # the default keepalive_timeout is 5 seconds
84   @keepalive_timeout = 5
86   # 1024 bytes matches nginx, though Rails session cookies will typically
87   # need >= 1500...
88   @client_header_buffer_size = 1024
90   # :stopdoc:
91   @alive = true
92   @cur = 0
93   @tick_mod = 0
94   @expire = nil
96   def self.tick
97     @tick_io.chmod(@tick_mod = 0 == @tick_mod ? 1 : 0)
98     exit!(2) if @expire && Time.now >= @expire
99     @alive && @server.master_pid == Process.ppid or quit!
100   end
102   def self.cur_alive
103     @alive || @cur > 0
104   end
106   def self.quit!
107     unless @expire
108       @alive = false
109       Rainbows::HttpParser.quit
110       @expire = Time.now + (@server.timeout * 2.0)
111       Unicorn::HttpServer::LISTENERS.each { |s| s.close rescue nil }.clear
112     end
113     false
114   end
116   autoload :Base, "rainbows/base"
117   autoload :WriterThreadPool, "rainbows/writer_thread_pool"
118   autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"
119   autoload :Revactor, "rainbows/revactor"
120   autoload :ThreadSpawn, "rainbows/thread_spawn"
121   autoload :ThreadPool, "rainbows/thread_pool"
122   autoload :Rev, "rainbows/rev"
123   autoload :RevThreadSpawn, "rainbows/rev_thread_spawn"
124   autoload :RevThreadPool, "rainbows/rev_thread_pool"
125   autoload :RevFiberSpawn, "rainbows/rev_fiber_spawn"
126   autoload :Coolio, "rainbows/coolio"
127   autoload :CoolioThreadSpawn, "rainbows/coolio_thread_spawn"
128   autoload :CoolioThreadPool, "rainbows/coolio_thread_pool"
129   autoload :CoolioFiberSpawn, "rainbows/coolio_fiber_spawn"
130   autoload :Epoll, "rainbows/epoll"
131   autoload :XEpoll, "rainbows/xepoll"
132   autoload :EventMachine, "rainbows/event_machine"
133   autoload :FiberSpawn, "rainbows/fiber_spawn"
134   autoload :FiberPool, "rainbows/fiber_pool"
135   autoload :ActorSpawn, "rainbows/actor_spawn"
136   autoload :NeverBlock, "rainbows/never_block"
137   autoload :XEpollThreadSpawn, "rainbows/xepoll_thread_spawn"
139   # :startdoc:
140   autoload :Fiber, 'rainbows/fiber' # core class
141   autoload :StreamFile, 'rainbows/stream_file'
142   autoload :HttpResponse, 'rainbows/http_response' # deprecated
143   autoload :ThreadTimeout, 'rainbows/thread_timeout'
144   autoload :WorkerYield, 'rainbows/worker_yield'
145   autoload :SyncClose, 'rainbows/sync_close'
146   autoload :ReverseProxy, 'rainbows/reverse_proxy'
147   autoload :JoinThreads, 'rainbows/join_threads'
150 require 'rainbows/error'
151 require 'rainbows/configurator'