rev/core: set IO::Buffer.default_node_size if possible
[rainbows.git] / lib / rainbows / rev / core.rb
blobeaff2c6fc1e86b2864a556c86668b394b8a4b461
1 # -*- encoding: binary -*-
2 require 'rev'
3 Rev::VERSION >= '0.3.0' or abort 'rev >= 0.3.0 is required'
4 if IO::Buffer.respond_to?(:default_node_size=)
5   IO::Buffer.default_node_size = Rev::IO::INPUT_SIZE
6 require 'rainbows/rev/heartbeat'
8 module Rainbows
9   module Rev
10     class Server < ::Rev::IO
11       G = Rainbows::G
12       LOOP = ::Rev::Loop.default
13       # CL and MAX will be defined in the corresponding worker loop
15       def on_readable
16         return if CONN.size >= MAX
17         begin
18           CL.new(@_io.accept_nonblock).attach(LOOP)
19         rescue Errno::EAGAIN, Errno::ECONNABORTED
20         end
21       end
22     end # class Server
24     module Core
25       include Base
27       # runs inside each forked worker, this sits around and waits
28       # for connections and doesn't die until the parent dies (or is
29       # given a INT, QUIT, or TERM signal)
30       def worker_loop(worker)
31         init_worker_process(worker)
32         mod = self.class.const_get(@use)
33         Server.const_set(:MAX, @worker_connections)
34         Server.const_set(:CL, mod.const_get(:Client))
35         EvCore.setup(EvCore)
36         rloop = ::Rev::Loop.default
37         Heartbeat.new(1, true).attach(rloop)
38         LISTENERS.map! { |s| Server.new(s).attach(rloop) }
39         rloop.run
40       end
42     end # module Core
43   end # module Rev
44 end # module Rainbows