revactor: do not recommend, upstream is dormant
[rainbows.git] / lib / rainbows / revactor.rb
blob2dff75179f37976897b320b03a2578b3e4c4be71
1 # -*- encoding: binary -*-
2 require 'revactor'
3 require 'fcntl'
4 Revactor::VERSION >= '0.1.5' or abort 'revactor 0.1.5 is required'
6 # Enables use of the Actor model through {Revactor}[http://revactor.org]
7 # under Ruby 1.9.
9 # \Revactor dormant upstream, so the use of this is NOT recommended for
10 # new applications.
12 # It spawns one long-lived Actor for every listen socket in the process
13 # and spawns a new Actor for every client connection accept()-ed.
14 # +worker_connections+ will limit the number of client Actors we have
15 # running at any one time.
17 # Applications using this model are required to be reentrant, but do
18 # not have to worry about race conditions unless they use threads
19 # internally.  \Rainbows! does not spawn threads under this model.
20 # Multiple instances of the same app may run in the same address space
21 # sequentially (but at interleaved points).  Any network dependencies
22 # in the application using this model should be implemented using the
23 # \Revactor library as well, to take advantage of the networking
24 # concurrency features this model provides.
25 module Rainbows::Revactor
26   autoload :Client, 'rainbows/revactor/client'
27   autoload :Proxy, 'rainbows/revactor/proxy'
29   include Rainbows::Base
31   # runs inside each forked worker, this sits around and waits
32   # for connections and doesn't die until the parent dies (or is
33   # given a INT, QUIT, or TERM signal)
34   def worker_loop(worker) #:nodoc:
35     Client.setup
36     init_worker_process(worker)
37     nr = 0
38     limit = worker_connections
39     actor_exit = Case[:exit, Actor, Object]
41     revactorize_listeners.each do |l,close,accept|
42       Actor.spawn do
43         Actor.current.trap_exit = true
44         l.controller = l.instance_variable_set(:@receiver, Actor.current)
45         begin
46           while nr >= limit
47             l.disable if l.enabled?
48             logger.info "busy: clients=#{nr} >= limit=#{limit}"
49             Actor.receive do |f|
50               f.when(close) {}
51               f.when(actor_exit) { nr -= 1 }
52               f.after(0.01) {} # another listener could've gotten an exit
53             end
54           end
56           l.enable unless l.enabled?
57           Actor.receive do |f|
58             f.when(close) {}
59             f.when(actor_exit) { nr -= 1 }
60             f.when(accept) do |_, _, s|
61               nr += 1
62               Actor.spawn_link(s) { |c| Client.new(c).process_loop }
63             end
64           end
65         rescue => e
66           Rainbows::Error.listen_loop(e)
67         end while Rainbows.alive
68         Actor.receive do |f|
69           f.when(close) {}
70           f.when(actor_exit) { nr -= 1 }
71         end while nr > 0
72       end
73     end
75     Actor.sleep 1 while Rainbows.tick || nr > 0
76     rescue Errno::EMFILE
77       # ignore, let another worker process take it
78   end
80   def revactorize_listeners
81     LISTENERS.map do |s|
82       case s
83       when TCPServer
84         l = Revactor::TCP.listen(s, nil)
85         [ l, T[:tcp_closed, Revactor::TCP::Socket],
86           T[:tcp_connection, l, Revactor::TCP::Socket] ]
87       when UNIXServer
88         l = Revactor::UNIX.listen(s)
89         [ l, T[:unix_closed, Revactor::UNIX::Socket ],
90           T[:unix_connection, l, Revactor::UNIX::Socket] ]
91       end
92     end
93   end
94   # :startdoc:
95 end