Class: ActiveSupport::Notifications::Fanout

Inherits:
Object
  • Object
show all
Includes:
FanoutIteration
Defined in:
lib/active_support/notifications/fanout.rb

Overview

This is a default queue implementation that ships with Notifications. It just pushes events to all registered log subscribers.

This class is thread safe. All methods are reentrant.

Defined Under Namespace

Modules: Subscribers Classes: BaseGroup, BaseTimeGroup, EventObjectGroup, EventedGroup, Handle, MonotonicTimedGroup, TimedGroup

Instance Method Summary collapse

Constructor Details

#initializeFanout

Returns a new instance of Fanout.



51
52
53
54
55
56
57
58
# File 'lib/active_support/notifications/fanout.rb', line 51

def initialize
  @mutex = Mutex.new
  @string_subscribers = Concurrent::Map.new { |h, k| h.compute_if_absent(k) { [] } }
  @other_subscribers = []
  @all_listeners_for = Concurrent::Map.new
  @groups_for = Concurrent::Map.new
  @silenceable_groups_for = Concurrent::Map.new
end

Instance Method Details

#all_listeners_for(name) ⇒ Object



298
299
300
301
302
303
304
305
# File 'lib/active_support/notifications/fanout.rb', line 298

def all_listeners_for(name)
  # this is correctly done double-checked locking (Concurrent::Map's lookups have volatile semantics)
  @all_listeners_for[name] || @mutex.synchronize do
    # use synchronisation when accessing @subscribers
    @all_listeners_for[name] ||=
      @string_subscribers[name] + @other_subscribers.select { |s| s.subscribed_to?(name) }
  end
end

#build_handle(name, id, payload) ⇒ Object



273
274
275
# File 'lib/active_support/notifications/fanout.rb', line 273

def build_handle(name, id, payload)
  Handle.new(self, name, id, payload)
end

#clear_cache(key = nil) ⇒ Object

:nodoc:



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/active_support/notifications/fanout.rb', line 102

def clear_cache(key = nil) # :nodoc:
  if key
    @all_listeners_for.delete(key)
    @groups_for.delete(key)
    @silenceable_groups_for.delete(key)
  else
    @all_listeners_for.clear
    @groups_for.clear
    @silenceable_groups_for.clear
  end
end

#finish(name, id, payload, listeners = nil) ⇒ Object



284
285
286
287
288
# File 'lib/active_support/notifications/fanout.rb', line 284

def finish(name, id, payload, listeners = nil)
  handle_stack = IsolatedExecutionState[:_fanout_handle_stack]
  handle = handle_stack.pop
  handle.finish_with_values(name, id, payload)
end

#groups_for(name) ⇒ Object

:nodoc:



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/active_support/notifications/fanout.rb', line 188

def groups_for(name) # :nodoc:
  groups = @groups_for.compute_if_absent(name) do
    all_listeners_for(name).reject(&:silenceable).group_by(&:group_class).transform_values do |s|
      s.map(&:delegate)
    end
  end

  silenceable_groups = @silenceable_groups_for.compute_if_absent(name) do
    all_listeners_for(name).select(&:silenceable).group_by(&:group_class).transform_values do |s|
      s.map(&:delegate)
    end
  end

  unless silenceable_groups.empty?
    groups = groups.dup
    silenceable_groups.each do |group_class, subscriptions|
      active_subscriptions = subscriptions.reject { |s| s.silenced?(name) }
      unless active_subscriptions.empty?
        groups[group_class] = (groups[group_class] || []) + active_subscriptions
      end
    end
  end

  groups
end

#inspectObject

:nodoc:



60
61
62
63
# File 'lib/active_support/notifications/fanout.rb', line 60

def inspect # :nodoc:
  total_patterns = @string_subscribers.size + @other_subscribers.size
  "#<#{self.class} (#{total_patterns} patterns)>"
end

#listeners_for(name) ⇒ Object



307
308
309
# File 'lib/active_support/notifications/fanout.rb', line 307

def listeners_for(name)
  all_listeners_for(name).reject { |s| s.silenced?(name) }
end

#listening?(name) ⇒ Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/active_support/notifications/fanout.rb', line 311

def listening?(name)
  all_listeners_for(name).any? { |s| !s.silenced?(name) }
end

#publish(name, *args) ⇒ Object



290
291
292
# File 'lib/active_support/notifications/fanout.rb', line 290

def publish(name, *args)
  iterate_guarding_exceptions(listeners_for(name)) { |s| s.publish(name, *args) }
end

#publish_event(event) ⇒ Object



294
295
296
# File 'lib/active_support/notifications/fanout.rb', line 294

def publish_event(event)
  iterate_guarding_exceptions(listeners_for(event.name)) { |s| s.publish_event(event) }
end

#start(name, id, payload) ⇒ Object



277
278
279
280
281
282
# File 'lib/active_support/notifications/fanout.rb', line 277

def start(name, id, payload)
  handle_stack = (IsolatedExecutionState[:_fanout_handle_stack] ||= [])
  handle = build_handle(name, id, payload)
  handle_stack << handle
  handle.start
end

#subscribe(pattern = nil, callable = nil, monotonic: false, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_support/notifications/fanout.rb', line 65

def subscribe(pattern = nil, callable = nil, monotonic: false, &block)
  subscriber = Subscribers.new(pattern, callable || block, monotonic)
  @mutex.synchronize do
    case pattern
    when String
      @string_subscribers[pattern] << subscriber
      clear_cache(pattern)
    when NilClass, Regexp
      @other_subscribers << subscriber
      clear_cache
    else
      raise ArgumentError,  "pattern must be specified as a String, Regexp or empty"
    end
  end
  subscriber
end

#unsubscribe(subscriber_or_name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_support/notifications/fanout.rb', line 82

def unsubscribe(subscriber_or_name)
  @mutex.synchronize do
    case subscriber_or_name
    when String
      @string_subscribers[subscriber_or_name].clear
      clear_cache(subscriber_or_name)
      @other_subscribers.each { |sub| sub.unsubscribe!(subscriber_or_name) }
    else
      pattern = subscriber_or_name.try(:pattern)
      if String === pattern
        @string_subscribers[pattern].delete(subscriber_or_name)
        clear_cache(pattern)
      else
        @other_subscribers.delete(subscriber_or_name)
        clear_cache
      end
    end
  end
end

#waitObject

This is a sync queue, so there is no waiting.



316
317
# File 'lib/active_support/notifications/fanout.rb', line 316

def wait
end