lib/ldclient-rb/polling.rb in ldclient-rb-4.0.0 vs lib/ldclient-rb/polling.rb in ldclient-rb-5.0.0

- old
+ new

@@ -7,20 +7,22 @@ @config = config @requestor = requestor @initialized = Concurrent::AtomicBoolean.new(false) @started = Concurrent::AtomicBoolean.new(false) @stopped = Concurrent::AtomicBoolean.new(false) + @ready = Concurrent::Event.new end def initialized? @initialized.value end def start - return unless @started.make_true + return @ready unless @started.make_true @config.logger.info { "[LDClient] Initializing polling connection" } create_worker + @ready end def stop if @stopped.make_true if @worker && @worker.alive? @@ -37,30 +39,35 @@ FEATURES => all_data[:flags], SEGMENTS => all_data[:segments] }) if @initialized.make_true @config.logger.info { "[LDClient] Polling connection initialized" } + @ready.set end end end def create_worker @worker = Thread.new do @config.logger.debug { "[LDClient] Starting polling worker" } while !@stopped.value do + started_at = Time.now begin - started_at = Time.now poll - delta = @config.poll_interval - (Time.now - started_at) - if delta > 0 - sleep(delta) + rescue UnexpectedResponseError => e + message = Util.http_error_message(e.status, "polling request", "will retry") + @config.logger.error { "[LDClient] #{message}" }; + if !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 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 + delta = @config.poll_interval - (Time.now - started_at) + if delta > 0 + sleep(delta) end end end end