aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/notifications/fanout.rb
blob: 412d977b259ffe7ed2f794f2cfdb1eadff5b7c70 (plain) (blame)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'thread'

module ActiveSupport
  module Notifications
    # This is a default queue implementation that ships with Notifications. It
    # consumes events in a thread and publish them to all registered subscribers.
    #
    class Fanout
      def initialize
        @subscribers = []
      end

      def bind(pattern)
        Binding.new(self, pattern)
      end

      def subscribe(pattern = nil, &block)
        @subscribers << Subscriber.new(pattern, &block)
      end

      def publish(*args)
        @subscribers.each { |s| s.publish(*args) }
      end

      def wait
        sleep(0.05) until @subscribers.all?(&:drained?)
      end

      # Used for internal implementation only.
      class Binding #:nodoc:
        def initialize(queue, pattern)
          @queue, @pattern = queue, pattern
        end

        def subscribe(&block)
          @queue.subscribe(@pattern, &block)
        end
      end

      # Used for internal implementation only.
      class Subscriber #:nodoc:
        def initialize(pattern, &block)
          @pattern =
            case pattern
            when Regexp, NilClass
              pattern
            else
              /^#{Regexp.escape(pattern.to_s)}/
            end
          @block = block
          @events = Queue.new
          start_consumer
        end

        def publish(name, *args)
          push(name, args) if matches?(name)
        end

        def consume
          while args = @events.shift
            @block.call(*args)
          end
        end

        def drained?
          @events.size.zero?
        end

        private
          def start_consumer
            Thread.new { consume }
          end

          def matches?(name)
            !@pattern || @pattern =~ name.to_s
          end

          def push(name, args)
            @events << args.unshift(name)
          end
      end
    end
  end
end