xepoll_thread_*/client: EPOLLONESHOT implies EPOLLET
[rainbows.git] / lib / rainbows.rb
blobbfa1ba02e1b9c367912b8531bf0f9da6b6c10f54
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.rubyforge.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   end
60   def self.config!(mod, *opts)
61     @forked or abort "#{mod} should only be loaded in a worker process"
62     opts.each do |opt|
63       mod.const_set(opt.to_s.upcase, Rainbows.server.__send__(opt))
64     end
65   end
67   @alive = true
68   @cur = 0
69   @expire = nil
70   @at_quit = []
72   def self.at_quit(&block)
73     @at_quit << block
74   end
76   def self.tick
77     @worker.tick = Time.now.to_i
78     exit!(2) if @expire && Time.now >= @expire
79     @alive && @server.master_pid == Process.ppid or quit!
80   end
82   def self.cur_alive
83     @alive || @cur > 0
84   end
86   def self.quit!
87     unless @expire
88       @alive = false
89       Rainbows::HttpParser.quit
90       @expire = Time.now + (@server.timeout * 2.0)
91       Unicorn::HttpServer::LISTENERS.each { |s| s.close rescue nil }.clear
92       @at_quit.each { |task| task.call }
93     end
94     false
95   end
97   autoload :Base, "rainbows/base"
98   autoload :WriterThreadPool, "rainbows/writer_thread_pool"
99   autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"
100   autoload :Revactor, "rainbows/revactor"
101   autoload :ThreadSpawn, "rainbows/thread_spawn"
102   autoload :ThreadPool, "rainbows/thread_pool"
103   autoload :Rev, "rainbows/rev"
104   autoload :RevThreadSpawn, "rainbows/rev_thread_spawn"
105   autoload :RevThreadPool, "rainbows/rev_thread_pool"
106   autoload :RevFiberSpawn, "rainbows/rev_fiber_spawn"
107   autoload :Coolio, "rainbows/coolio"
108   autoload :CoolioThreadSpawn, "rainbows/coolio_thread_spawn"
109   autoload :CoolioThreadPool, "rainbows/coolio_thread_pool"
110   autoload :CoolioFiberSpawn, "rainbows/coolio_fiber_spawn"
111   autoload :Epoll, "rainbows/epoll"
112   autoload :XEpoll, "rainbows/xepoll"
113   autoload :EventMachine, "rainbows/event_machine"
114   autoload :FiberSpawn, "rainbows/fiber_spawn"
115   autoload :FiberPool, "rainbows/fiber_pool"
116   autoload :ActorSpawn, "rainbows/actor_spawn"
117   autoload :NeverBlock, "rainbows/never_block"
118   autoload :XEpollThreadSpawn, "rainbows/xepoll_thread_spawn"
119   autoload :XEpollThreadPool, "rainbows/xepoll_thread_pool"
120   autoload :StreamResponseEpoll, "rainbows/stream_response_epoll"
122   autoload :Fiber, 'rainbows/fiber' # core class
123   autoload :StreamFile, 'rainbows/stream_file'
124   autoload :ThreadTimeout, 'rainbows/thread_timeout'
125   autoload :WorkerYield, 'rainbows/worker_yield'
126   autoload :SyncClose, 'rainbows/sync_close'
127   autoload :ReverseProxy, 'rainbows/reverse_proxy'
128   autoload :JoinThreads, 'rainbows/join_threads'
129   autoload :PoolSize, 'rainbows/pool_size'
132 require 'rainbows/error'
133 require 'rainbows/configurator'