Sha256: 8e508e9abe5476439b00b971d6457c68aba52396c1d07fa31b55267c75a23027

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 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

2 entries across 2 versions & 1 rubygems

Version Path
ldclient-rb-3.0.1 lib/ldclient-rb/polling.rb
ldclient-rb-3.0.0 lib/ldclient-rb/polling.rb