-
Notifications
You must be signed in to change notification settings - Fork 56
feat: Retry indefinitely after a data source failure instead of stopping permanently #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jsonbailey
wants to merge
3
commits into
main
Choose a base branch
from
jb/sdk-2793/retry-conformance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| require "ldclient-rb/impl/data_source" | ||
| require "ldclient-rb/impl/model/serialization" | ||
| require "ldclient-rb/impl/retry_state" | ||
| require "ldclient-rb/impl/util" | ||
| require "ldclient-rb/in_memory_store" | ||
|
|
||
|
|
@@ -31,6 +32,8 @@ def initialize(sdk_key, config, diagnostic_accumulator = nil) | |
| @started = Concurrent::AtomicBoolean.new(false) | ||
| @stopped = Concurrent::AtomicBoolean.new(false) | ||
| @ready = Concurrent::Event.new | ||
| @stop_event = Concurrent::Event.new | ||
| @retry_state = Impl::RetryState.for_streaming(@config.initial_reconnect_delay, @config.logger) | ||
| @connection_attempt_start_time = 0 | ||
| end | ||
|
|
||
|
|
@@ -49,46 +52,17 @@ def start | |
| read_timeout: READ_TIMEOUT_SECONDS, | ||
| logger: @config.logger, | ||
| socket_factory: @config.socket_factory, | ||
| reconnect_time: @config.initial_reconnect_delay, | ||
| # The SDK waits in the failure handlers instead. This must be an Integer: the SSE client | ||
| # multiplies it by a power of two, and a Float 0.0 becomes NaN once that power overflows. | ||
| reconnect_time: 0, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mixture of floats and ints throughout the logic is a tad concerning if we anticipate edge cases like this comment describes. |
||
| } | ||
| log_connection_started | ||
|
|
||
| uri = Impl::Util.add_payload_filter_key(@config.stream_uri + "/all", @config) | ||
| @es = SSE::Client.new(uri, **opts) do |conn| | ||
| conn.on_connect { |response_headers| DataSource.record_environment_id(@data_source_update_sink, response_headers) } | ||
| conn.on_event { |event| process_message(event) } | ||
| conn.on_error { |err| | ||
| log_connection_result(false) | ||
| case err | ||
| when SSE::Errors::HTTPStatusError | ||
| status = err.status | ||
| error_info = LaunchDarkly::Interfaces::DataSource::ErrorInfo.new( | ||
| LaunchDarkly::Interfaces::DataSource::ErrorInfo::ERROR_RESPONSE, status, nil, Time.now) | ||
| message = Util.http_error_message(status, "streaming connection", "will retry") | ||
| @config.logger.error { "[LDClient] #{message}" } | ||
|
|
||
| if Util.http_error_recoverable?(status) | ||
| @data_source_update_sink&.update_status( | ||
| LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED, | ||
| error_info | ||
| ) | ||
| else | ||
| @ready.set # if client was waiting on us, make it stop waiting - has no effect if already set | ||
| stop_with_error_info error_info | ||
| end | ||
| when SSE::Errors::HTTPContentTypeError, SSE::Errors::HTTPProxyError, SSE::Errors::ReadTimeoutError | ||
| @data_source_update_sink&.update_status( | ||
| LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED, | ||
| LaunchDarkly::Interfaces::DataSource::ErrorInfo.new(LaunchDarkly::Interfaces::DataSource::ErrorInfo::NETWORK_ERROR, 0, err.to_s, Time.now) | ||
| ) | ||
|
|
||
| else | ||
| @data_source_update_sink&.update_status( | ||
| LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED, | ||
| LaunchDarkly::Interfaces::DataSource::ErrorInfo.new(LaunchDarkly::Interfaces::DataSource::ErrorInfo::UNKNOWN, 0, err.to_s, Time.now) | ||
| ) | ||
| end | ||
| } | ||
| conn.on_error { |err| handle_error(err) } | ||
| end | ||
|
|
||
| @ready | ||
|
|
@@ -106,11 +80,65 @@ def stop | |
| def stop_with_error_info(error_info = nil) | ||
| if @stopped.make_true | ||
| @es.close | ||
| @stop_event.set | ||
| @data_source_update_sink&.update_status(LaunchDarkly::Interfaces::DataSource::Status::OFF, error_info) | ||
| @config.logger.info { "[LDClient] Stream connection stopped" } | ||
| end | ||
| end | ||
|
|
||
| def handle_error(err) | ||
| if err.is_a?(SSE::Errors::HTTPStatusError) | ||
| status = err.status | ||
| error_info = LaunchDarkly::Interfaces::DataSource::ErrorInfo.new( | ||
| LaunchDarkly::Interfaces::DataSource::ErrorInfo::ERROR_RESPONSE, status, nil, Time.now) | ||
| handle_failure(Impl::RetryState.classify_http_status(status), error_info) do |delay| | ||
| @config.logger.error { "[LDClient] #{Util.http_error_retry_message(status, 'streaming connection', delay)}" } | ||
| end | ||
| return | ||
| end | ||
|
|
||
| if err.is_a?(SSE::Errors::StreamClosedByServerError) | ||
| error_info = LaunchDarkly::Interfaces::DataSource::ErrorInfo.new( | ||
| LaunchDarkly::Interfaces::DataSource::ErrorInfo::NETWORK_ERROR, 0, err.to_s, Time.now) | ||
| handle_failure(:normal, error_info) do |delay| | ||
| @config.logger.warn { "[LDClient] The server closed the streaming connection - #{Util.retry_message(delay)}" } | ||
| end | ||
| return | ||
| end | ||
|
|
||
| error_kind = case err | ||
| when SSE::Errors::HTTPContentTypeError, SSE::Errors::HTTPProxyError, SSE::Errors::ReadTimeoutError | ||
| LaunchDarkly::Interfaces::DataSource::ErrorInfo::NETWORK_ERROR | ||
| else | ||
| LaunchDarkly::Interfaces::DataSource::ErrorInfo::UNKNOWN | ||
| end | ||
| error_info = LaunchDarkly::Interfaces::DataSource::ErrorInfo.new(error_kind, 0, err.to_s, Time.now) | ||
| handle_failure(:normal, error_info) do |delay| | ||
| @config.logger.warn { "[LDClient] Error on streaming connection: #{err} - #{Util.retry_message(delay)}" } | ||
| end | ||
| end | ||
|
|
||
| # | ||
| # Records a failure, reports it, and waits before the next attempt. The SSE client calls this on its | ||
| # worker thread before it reconnects, so the wait is the reconnect delay, and {#stop} ends it at once. | ||
| # | ||
| # @param kind [Symbol] `:normal` or `:unexpected` | ||
| # @param error_info [LaunchDarkly::Interfaces::DataSource::ErrorInfo] | ||
| # @yieldparam delay [Numeric] seconds until the next attempt, for the log message | ||
| # | ||
| def handle_failure(kind, error_info) | ||
| return if @stopped.value | ||
|
|
||
| log_connection_result(false) | ||
| @retry_state.record_failure(kind) | ||
| delay = @retry_state.next_delay | ||
| yield delay | ||
| @data_source_update_sink&.update_status(LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED, error_info) | ||
|
|
||
| @stop_event.wait([delay, Impl::RetryState::MAX_WAIT].min) | ||
| log_connection_started | ||
| end | ||
|
|
||
| # | ||
| # The original implementation of this class relied on the feature store | ||
| # directly, which we are trying to move away from. Customers who might have | ||
|
|
@@ -162,6 +190,7 @@ def process_message(message) | |
| @config.logger.warn { "[LDClient] Unknown message received: #{method}" } | ||
| end | ||
|
|
||
| @retry_state.record_success | ||
| @data_source_update_sink&.update_status(LaunchDarkly::Interfaces::DataSource::Status::VALID, nil) | ||
| rescue JSON::ParserError => e | ||
| @config.logger.error { "[LDClient] JSON parsing failed for method #{method}. Ignoring event." } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is true, but also perhaps too specific. It doesn't have to be in flight or stopped, just that nothing else can be reported after Off.