add client_header_buffer_size tuning parameter
[rainbows.git] / lib / rainbows.rb
blob0e6ca3929bc7ac5a43e82d6374ed93278215ba7e
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 :max_bytes, :keepalive_timeout
72     attr_accessor :server
73     attr_accessor :cur # may not always be used
74     attr_reader :alive
75     attr_writer :tick_io
76   end
77   # :startdoc:
79   # the default max body size is 1 megabyte (1024 * 1024 bytes)
80   @max_bytes = 1024 * 1024
82   # the default keepalive_timeout is 5 seconds
83   @keepalive_timeout = 5
85   # 1024 bytes matches nginx, though Rails session cookies will typically
86   # need >= 1500...
87   @client_header_buffer_size = 1024
89   # :stopdoc:
90   @alive = true
91   @cur = 0
92   @tick_mod = 0
93   @expire = nil
95   def self.tick
96     @tick_io.chmod(@tick_mod = 0 == @tick_mod ? 1 : 0)
97     exit!(2) if @expire && Time.now >= @expire
98     @alive && @server.master_pid == Process.ppid or quit!
99   end
101   def self.cur_alive
102     @alive || @cur > 0
103   end
105   def self.quit!
106     unless @expire
107       @alive = false
108       Rainbows::HttpParser.quit
109       @expire = Time.now + (@server.timeout * 2.0)
110       Unicorn::HttpServer::LISTENERS.each { |s| s.close rescue nil }.clear
111     end
112     false
113   end
115   autoload :Base, "rainbows/base"
116   autoload :WriterThreadPool, "rainbows/writer_thread_pool"
117   autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"
118   autoload :Revactor, "rainbows/revactor"
119   autoload :ThreadSpawn, "rainbows/thread_spawn"
120   autoload :ThreadPool, "rainbows/thread_pool"
121   autoload :Rev, "rainbows/rev"
122   autoload :RevThreadSpawn, "rainbows/rev_thread_spawn"
123   autoload :RevThreadPool, "rainbows/rev_thread_pool"
124   autoload :RevFiberSpawn, "rainbows/rev_fiber_spawn"
125   autoload :Coolio, "rainbows/coolio"
126   autoload :CoolioThreadSpawn, "rainbows/coolio_thread_spawn"
127   autoload :CoolioThreadPool, "rainbows/coolio_thread_pool"
128   autoload :CoolioFiberSpawn, "rainbows/coolio_fiber_spawn"
129   autoload :Epoll, "rainbows/epoll"
130   autoload :XEpoll, "rainbows/xepoll"
131   autoload :EventMachine, "rainbows/event_machine"
132   autoload :FiberSpawn, "rainbows/fiber_spawn"
133   autoload :FiberPool, "rainbows/fiber_pool"
134   autoload :ActorSpawn, "rainbows/actor_spawn"
135   autoload :NeverBlock, "rainbows/never_block"
136   autoload :XEpollThreadSpawn, "rainbows/xepoll_thread_spawn"
138   # :startdoc:
139   autoload :Fiber, 'rainbows/fiber' # core class
140   autoload :StreamFile, 'rainbows/stream_file'
141   autoload :HttpResponse, 'rainbows/http_response' # deprecated
142   autoload :ThreadTimeout, 'rainbows/thread_timeout'
143   autoload :WorkerYield, 'rainbows/worker_yield'
144   autoload :SyncClose, 'rainbows/sync_close'
145   autoload :ReverseProxy, 'rainbows/reverse_proxy'
146   autoload :JoinThreads, 'rainbows/join_threads'
149 require 'rainbows/error'
150 require 'rainbows/configurator'