Sha256: bbc7ce4eb4b2a62efc3a7b69e3713f588e413e583d3bc799abec159cdf149653

Contents?: true

Size: 1.81 KB

Versions: 5

Compression:

Stored size: 1.81 KB

Contents

require "concurrent/atomics"
require "thread"

module LaunchDarkly
  class PollingProcessor
    def initialize(config, requestor)
      @config = config
      @requestor = requestor
      @initialized = Concurrent::AtomicBoolean.new(false)
      @started = Concurrent::AtomicBoolean.new(false)
      @stopped = Concurrent::AtomicBoolean.new(false)
    end

    def initialized?
      @initialized.value
    end

    def start
      return unless @started.make_true
      @config.logger.info("[LDClient] Initializing polling connection")
      create_worker
    end

    def stop
      if @stopped.make_true
        if @worker && @worker.alive?
          @worker.raise "shutting down client"
        end
        @config.logger.info("[LDClient] Polling connection stopped")
      end
    end

    def poll
      flags = @requestor.request_all_flags
      if flags
        @config.feature_store.init(flags)
        if @initialized.make_true
          @config.logger.info("[LDClient] Polling connection initialized")
        end
      end
    end

    def create_worker
      @worker = Thread.new do
        @config.logger.debug("[LDClient] Starting polling worker")
        while !@stopped.value do
          begin
            started_at = Time.now
            poll
            delta = @config.poll_interval - (Time.now - started_at)
            if delta > 0
              sleep(delta)
            end
          rescue InvalidSDKKeyError
            @config.logger.error("[LDClient] Received 401 error, no further polling requests will be made since SDK key is invalid");
            stop
          rescue StandardError => exn
            @config.logger.error("[LDClient] Exception while polling: #{exn.inspect}")
            # TODO: log_exception(__method__.to_s, exn)
          end
        end
      end
    end

    private :poll, :create_worker
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ldclient-rb-2.5.0 lib/ldclient-rb/polling.rb
ldclient-rb-2.4.1 lib/ldclient-rb/polling.rb
ldclient-rb-2.4.0 lib/ldclient-rb/polling.rb
ldclient-rb-2.3.2 lib/ldclient-rb/polling.rb
ldclient-rb-2.3.1 lib/ldclient-rb/polling.rb