Sha256: f0efa940d4d08d971120f3a79f5c34685b91a19c20a510766c76eae96e390d32

Contents?: true

Size: 1.95 KB

Versions: 19

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module LaunchDarkly
  module Impl

    #
    # A generic mechanism for registering event listeners and broadcasting
    # events to them.
    #
    # The SDK maintains an instance of this for each available type of listener
    # (flag change, data store status, etc.). They are all intended to share a
    # single executor service; notifications are submitted individually to this
    # service for each listener.
    #
    class Broadcaster
      def initialize(executor, logger)
        @listeners = Concurrent::Set.new
        @executor = executor
        @logger = logger
      end

      #
      # Register a listener to this broadcaster.
      #
      # @param listener [#update]
      #
      def add_listener(listener)
        unless listener.respond_to? :update
          logger.warn("listener (#{listener.class}) does not respond to :update method. ignoring as registered listener")
          return
        end

        listeners.add(listener)
      end

      #
      # Removes a registered listener from this broadcaster.
      #
      def remove_listener(listener)
        listeners.delete(listener)
      end

      def has_listeners?
        !listeners.empty?
      end

      #
      # Broadcast the provided event to all registered listeners.
      #
      # Each listener will be notified using the broadcasters executor. This
      # method is non-blocking.
      #
      def broadcast(event)
        listeners.each do |listener|
          executor.post do
            begin
              listener.update(event)
            rescue StandardError => e
              logger.error("listener (#{listener.class}) raised exception (#{e}) processing event (#{event.class})")
            end
          end
        end
      end


      private

      # @return [Concurrent::ThreadPoolExecutor]
      attr_reader :executor

      # @return [Logger]
      attr_reader :logger

      # @return [Concurrent::Set]
      attr_reader :listeners
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
launchdarkly-server-sdk-8.8.2 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.8.1 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.8.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.7.1 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.7.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.6.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.5.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.4.2 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.4.1 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.4.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.3.1 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-7.3.3 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.3.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.2.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.1.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-7.3.2 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-8.0.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-7.3.0 lib/ldclient-rb/impl/broadcaster.rb
launchdarkly-server-sdk-7.2.0 lib/ldclient-rb/impl/broadcaster.rb