Unicorn 4.x resync for ticker
[rainbows.git] / lib / rainbows.rb
blobef9e75f008eac1505d1df8b0944c389cda5e5f38
1 # -*- encoding: binary -*-
2 require 'kgio'
3 require 'unicorn'
4 require 'io/wait'
5 Unicorn::SocketHelper::DEFAULTS.merge!({
6   # the value passed to TCP_DEFER_ACCEPT actually matters in Linux 2.6.32+
7   :tcp_defer_accept => 60,
9   # keep-alive performance sucks without this due to
10   # write(headers)-write(body)-read
11   # because we always write headers and bodies with two calls
12   :tcp_nodelay => true,
14   # we always want to send our headers out ASAP since Rainbows!
15   # is designed for apps that could trickle out the body slowly
16   :tcp_nopush => false,
19 # See http://rainbows.rubyforge.org/ for documentation
20 module Rainbows
21   # :stopdoc:
22   O = {}
24   # map of numeric file descriptors to IO objects to avoid using IO.new
25   # and potentially causing race conditions when using /dev/fd/
26   FD_MAP = {}
27   FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
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   # :startdoc:
45   # Sleeps the current application dispatch.  This will pick the
46   # optimal method to sleep depending on the concurrency model chosen
47   # (which may still suck and block the entire process).  Using this
48   # with the basic :Coolio or :EventMachine models is not recommended.
49   # This should be used within your Rack application.
50   def self.sleep(seconds)
51     case Rainbows.server.use
52     when :FiberPool, :FiberSpawn
53       Rainbows::Fiber.sleep(seconds)
54     when :RevFiberSpawn, :CoolioFiberSpawn
55       Rainbows::Fiber::Coolio::Sleeper.new(seconds)
56     when :Revactor
57       Actor.sleep(seconds)
58     else
59       Kernel.sleep(seconds)
60     end
61   end
62   # :stopdoc:
64   class << self
65     attr_accessor :server
66     attr_accessor :cur # may not always be used
67     attr_reader :alive
68     attr_writer :worker
69     attr_writer :forked
70   end
72   def self.config!(mod, *opts)
73     @forked or abort "#{mod} should only be loaded in a worker process"
74     opts.each do |opt|
75       mod.const_set(opt.to_s.upcase, Rainbows.server.__send__(opt))
76     end
77   end
79   @alive = true
80   @cur = 0
81   @expire = nil
82   @at_quit = []
84   def self.at_quit(&block)
85     @at_quit << block
86   end
88   def self.tick
89     @worker.tick = Time.now.to_i
90     exit!(2) if @expire && Time.now >= @expire
91     @alive && @server.master_pid == Process.ppid or quit!
92   end
94   def self.cur_alive
95     @alive || @cur > 0
96   end
98   def self.quit!
99     unless @expire
100       @alive = false
101       Rainbows::HttpParser.quit
102       @expire = Time.now + (@server.timeout * 2.0)
103       Unicorn::HttpServer::LISTENERS.each { |s| s.close rescue nil }.clear
104       @at_quit.each { |task| task.call }
105     end
106     false
107   end
109   autoload :Base, "rainbows/base"
110   autoload :WriterThreadPool, "rainbows/writer_thread_pool"
111   autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"
112   autoload :Revactor, "rainbows/revactor"
113   autoload :ThreadSpawn, "rainbows/thread_spawn"
114   autoload :ThreadPool, "rainbows/thread_pool"
115   autoload :Rev, "rainbows/rev"
116   autoload :RevThreadSpawn, "rainbows/rev_thread_spawn"
117   autoload :RevThreadPool, "rainbows/rev_thread_pool"
118   autoload :RevFiberSpawn, "rainbows/rev_fiber_spawn"
119   autoload :Coolio, "rainbows/coolio"
120   autoload :CoolioThreadSpawn, "rainbows/coolio_thread_spawn"
121   autoload :CoolioThreadPool, "rainbows/coolio_thread_pool"
122   autoload :CoolioFiberSpawn, "rainbows/coolio_fiber_spawn"
123   autoload :Epoll, "rainbows/epoll"
124   autoload :XEpoll, "rainbows/xepoll"
125   autoload :EventMachine, "rainbows/event_machine"
126   autoload :FiberSpawn, "rainbows/fiber_spawn"
127   autoload :FiberPool, "rainbows/fiber_pool"
128   autoload :ActorSpawn, "rainbows/actor_spawn"
129   autoload :NeverBlock, "rainbows/never_block"
130   autoload :XEpollThreadSpawn, "rainbows/xepoll_thread_spawn"
131   autoload :XEpollThreadPool, "rainbows/xepoll_thread_pool"
132   autoload :StreamResponseEpoll, "rainbows/stream_response_epoll"
134   autoload :Fiber, 'rainbows/fiber' # core class
135   autoload :StreamFile, 'rainbows/stream_file'
136   autoload :HttpResponse, 'rainbows/http_response' # deprecated
137   autoload :ThreadTimeout, 'rainbows/thread_timeout'
138   autoload :WorkerYield, 'rainbows/worker_yield'
139   autoload :SyncClose, 'rainbows/sync_close'
140   autoload :ReverseProxy, 'rainbows/reverse_proxy'
141   autoload :JoinThreads, 'rainbows/join_threads'
142   autoload :PoolSize, 'rainbows/pool_size'
145 require 'rainbows/error'
146 require 'rainbows/configurator'