rev/deferred_response: cleanups
[rainbows.git] / lib / rainbows / rev / core.rb
blob9f7a1f0275734fc016873726f41bcc9453975fbe
1 # -*- encoding: binary -*-
2 # :enddoc:
3 require 'rev'
4 Rev::VERSION >= '0.3.0' or abort 'rev >= 0.3.0 is required'
5 require 'rainbows/rev/heartbeat'
7 module Rainbows
8   module Rev
9     class Server < ::Rev::IO
10       G = Rainbows::G
11       # CL and MAX will be defined in the corresponding worker loop
13       def on_readable
14         return if CONN.size >= MAX
15         io = Rainbows.accept(@_io) and CL.new(io).attach(LOOP)
16       end
17     end # class Server
19     module Core
20       include Base
22       # runs inside each forked worker, this sits around and waits
23       # for connections and doesn't die until the parent dies (or is
24       # given a INT, QUIT, or TERM signal)
25       def worker_loop(worker)
26         Rainbows::Response.setup(Rainbows::Rev::Client)
27         require 'rainbows/rev/sendfile'
28         Rainbows::Rev::Client.__send__(:include, Rainbows::Rev::Sendfile)
29         init_worker_process(worker)
30         mod = self.class.const_get(@use)
31         rloop = Server.const_set(:LOOP, ::Rev::Loop.default)
32         Server.const_set(:MAX, @worker_connections)
33         Server.const_set(:CL, mod.const_get(:Client))
34         EvCore.const_set(:APP, G.server.app)
35         Heartbeat.new(1, true).attach(rloop)
36         LISTENERS.map! { |s| Server.new(s).attach(rloop) }
37         rloop.run
38       end
40     end # module Core
41   end # module Rev
42 end # module Rainbows