Sha256: e0abd749deb5f7bdc21175e089df468f6a810f4e11827fe5754e5b2d73a7c3e2

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

require "ldclient-rb/impl/repeating_task"

require "concurrent/atomics"
require "thread"

module LaunchDarkly
  # @private
  class PollingProcessor
    def initialize(config, requestor)
      @config = config
      @requestor = requestor
      @initialized = Concurrent::AtomicBoolean.new(false)
      @started = Concurrent::AtomicBoolean.new(false)
      @ready = Concurrent::Event.new
      @task = Impl::RepeatingTask.new(@config.poll_interval, 0, -> { self.poll }, @config.logger)
    end

    def initialized?
      @initialized.value
    end

    def start
      return @ready unless @started.make_true
      @config.logger.info { "[LDClient] Initializing polling connection" }
      @task.start
      @ready
    end

    def stop
      @task.stop
      @config.logger.info { "[LDClient] Polling connection stopped" }
    end

    def poll
      begin
        all_data = @requestor.request_all_data
        if all_data
          @config.feature_store.init(all_data)
          if @initialized.make_true
            @config.logger.info { "[LDClient] Polling connection initialized" }
            @ready.set
          end
        end
      rescue UnexpectedResponseError => e
        message = Util.http_error_message(e.status, "polling request", "will retry")
        @config.logger.error { "[LDClient] #{message}" }
        unless Util.http_error_recoverable?(e.status)
          @ready.set  # if client was waiting on us, make it stop waiting - has no effect if already set
          stop
        end
      rescue StandardError => e
        Util.log_exception(@config.logger, "Exception while polling", e)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
launchdarkly-server-sdk-7.1.0 lib/ldclient-rb/polling.rb
launchdarkly-server-sdk-7.0.4 lib/ldclient-rb/polling.rb
launchdarkly-server-sdk-7.0.3 lib/ldclient-rb/polling.rb
launchdarkly-server-sdk-7.0.2 lib/ldclient-rb/polling.rb
launchdarkly-server-sdk-7.0.1 lib/ldclient-rb/polling.rb
launchdarkly-server-sdk-7.0.0 lib/ldclient-rb/polling.rb