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