rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 0460137078b0d548df5d0d81ef77b4ae367aef50 1221 bytes (raw)
$ git show v0.6.0:lib/rainbows/rev/core.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
# -*- encoding: binary -*-
require 'rev'
Rev::VERSION >= '0.3.0' or abort 'rev >= 0.3.0 is required'
require 'rainbows/rev/heartbeat'

module Rainbows
  module Rev
    class Server < ::Rev::IO
      G = Rainbows::G
      LOOP = ::Rev::Loop.default
      # CL and MAX will be defined in the corresponding worker loop

      def on_readable
        return if G.cur >= MAX
        begin
          CL.new(@_io.accept_nonblock).attach(LOOP)
        rescue Errno::EAGAIN, Errno::ECONNABORTED
        end
      end
    end # class Server

    module Core
      include Base

      # runs inside each forked worker, this sits around and waits
      # for connections and doesn't die until the parent dies (or is
      # given a INT, QUIT, or TERM signal)
      def worker_loop(worker)
        init_worker_process(worker)
        mod = self.class.const_get(@use)
        Server.const_set(:MAX, @worker_connections)
        Server.const_set(:CL, mod.const_get(:Client))
        EvCore.setup(EvCore)
        rloop = ::Rev::Loop.default
        Heartbeat.new(1, true).attach(rloop)
        LISTENERS.map! { |s| Server.new(s).attach(rloop) }
        rloop.run
      end

    end # module Core
  end # module Rev
end # module Rainbows

git clone https://yhbt.net/rainbows.git