switch docs + website to olddoc
[rainbows.git] / lib / rainbows.rb
blob3bebf34dc0917ad477aa3f27568f56c9ee077857
1 # -*- encoding: binary -*-
2 require 'kgio'
3 require 'unicorn'
4 # the value passed to TCP_DEFER_ACCEPT actually matters in Linux 2.6.32+
5 Unicorn::SocketHelper::DEFAULTS[:tcp_defer_accept] = 60
7 # See http://rainbows.bogomips.org/ for documentation
8 module Rainbows
9   # :stopdoc:
10   O = {}
12   # map of numeric file descriptors to IO objects to avoid using IO.new
13   # and potentially causing race conditions when using /dev/fd/
14   FD_MAP = {}
15   FD_MAP.compare_by_identity if FD_MAP.respond_to?(:compare_by_identity)
17   require 'rainbows/const'
18   require 'rainbows/http_parser'
19   require 'rainbows/http_server'
20   autoload :Response, 'rainbows/response'
21   autoload :ProcessClient, 'rainbows/process_client'
22   autoload :Client, 'rainbows/client'
23   autoload :Base, 'rainbows/base'
24   autoload :Sendfile, 'rainbows/sendfile'
25   autoload :AppPool, 'rainbows/app_pool'
26   autoload :DevFdResponse, 'rainbows/dev_fd_response'
27   autoload :MaxBody, 'rainbows/max_body'
28   autoload :QueuePool, 'rainbows/queue_pool'
29   autoload :EvCore, 'rainbows/ev_core'
30   autoload :SocketProxy, 'rainbows/socket_proxy'
32   # :startdoc:
33   # Sleeps the current application dispatch.  This will pick the
34   # optimal method to sleep depending on the concurrency model chosen
35   # (which may still suck and block the entire process).  Using this
36   # with the basic :Coolio or :EventMachine models is not recommended.
37   # This should be used within your Rack application.
38   def self.sleep(seconds)
39     case Rainbows.server.use
40     when :FiberPool, :FiberSpawn
41       Rainbows::Fiber.sleep(seconds)
42     when :RevFiberSpawn, :CoolioFiberSpawn
43       Rainbows::Fiber::Coolio::Sleeper.new(seconds)
44     when :Revactor
45       Actor.sleep(seconds)
46     else
47       Kernel.sleep(seconds)
48     end
49   end
50   # :stopdoc:
52   class << self
53     attr_accessor :server
54     attr_accessor :cur # may not always be used
55     attr_reader :alive
56     attr_writer :worker
57     attr_writer :forked
58     attr_writer :readers
59   end
61   def self.config!(mod, *opts)
62     @forked or abort "#{mod} should only be loaded in a worker process"
63     opts.each do |opt|
64       mod.const_set(opt.to_s.upcase, Rainbows.server.__send__(opt))
65     end
66   end
68   @alive = true
69   @cur = 0
70   @expire = nil
71   @at_quit = []
73   def self.at_quit(&block)
74     @at_quit << block
75   end
77   def self.tick
78     @worker.tick = Time.now.to_i
79     exit!(2) if @expire && Time.now >= @expire
80     @alive && @server.master_pid == Process.ppid or quit!
81   end
83   def self.cur_alive
84     @alive || @cur > 0
85   end
87   def self.quit!
88     unless @expire
89       @alive = false
90       Rainbows::HttpParser.quit
91       @expire = Time.now + (@server.timeout * 2.0)
92       tmp = @readers.dup
93       @readers.clear
94       tmp.each { |s| s.close rescue nil }.clear
95       @at_quit.each { |task| task.call }
96     end
97     false
98   end
100   autoload :Base, "rainbows/base"
101   autoload :WriterThreadPool, "rainbows/writer_thread_pool"
102   autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"
103   autoload :Revactor, "rainbows/revactor"
104   autoload :ThreadSpawn, "rainbows/thread_spawn"
105   autoload :ThreadPool, "rainbows/thread_pool"
106   autoload :Rev, "rainbows/rev"
107   autoload :RevThreadSpawn, "rainbows/rev_thread_spawn"
108   autoload :RevThreadPool, "rainbows/rev_thread_pool"
109   autoload :RevFiberSpawn, "rainbows/rev_fiber_spawn"
110   autoload :Coolio, "rainbows/coolio"
111   autoload :CoolioThreadSpawn, "rainbows/coolio_thread_spawn"
112   autoload :CoolioThreadPool, "rainbows/coolio_thread_pool"
113   autoload :CoolioFiberSpawn, "rainbows/coolio_fiber_spawn"
114   autoload :Epoll, "rainbows/epoll"
115   autoload :XEpoll, "rainbows/xepoll"
116   autoload :EventMachine, "rainbows/event_machine"
117   autoload :FiberSpawn, "rainbows/fiber_spawn"
118   autoload :FiberPool, "rainbows/fiber_pool"
119   autoload :ActorSpawn, "rainbows/actor_spawn"
120   autoload :NeverBlock, "rainbows/never_block"
121   autoload :XEpollThreadSpawn, "rainbows/xepoll_thread_spawn"
122   autoload :XEpollThreadPool, "rainbows/xepoll_thread_pool"
123   autoload :StreamResponseEpoll, "rainbows/stream_response_epoll"
125   autoload :Fiber, 'rainbows/fiber' # core class
126   autoload :StreamFile, 'rainbows/stream_file'
127   autoload :ThreadTimeout, 'rainbows/thread_timeout'
128   autoload :WorkerYield, 'rainbows/worker_yield'
129   autoload :SyncClose, 'rainbows/sync_close'
130   autoload :ReverseProxy, 'rainbows/reverse_proxy'
131   autoload :JoinThreads, 'rainbows/join_threads'
132   autoload :PoolSize, 'rainbows/pool_size'
135 require 'rainbows/error'
136 require 'rainbows/configurator'