Sha256: c79cbc90ecd190f6a3827becd01f6e75d3292e52210a0a827d0904659d28ee90

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 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
      all_data = @requestor.request_all_data
      if all_data
        @config.feature_store.init({
          FEATURES => all_data[:flags],
          SEGMENTS => all_data[:segments]
        })
        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

3 entries across 3 versions & 1 rubygems

Version Path
ldclient-rb-4.0.0 lib/ldclient-rb/polling.rb
ldclient-rb-3.0.3 lib/ldclient-rb/polling.rb
ldclient-rb-3.0.2 lib/ldclient-rb/polling.rb