Class: LaunchDarkly::Impl::DataSystem::StreamingDataSource Private
- Inherits:
-
Object
- Object
- LaunchDarkly::Impl::DataSystem::StreamingDataSource
- Defined in:
- lib/ldclient-rb/impl/data_system/streaming.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
StreamingDataSource is a Synchronizer that uses Server-Sent Events (SSE) to receive real-time updates from LaunchDarkly’s Flag Delivery services.
Instance Attribute Summary collapse
- #name ⇒ Object readonly private
Instance Method Summary collapse
-
#initialize(sdk_key, http_config, initial_reconnect_delay, config) ⇒ StreamingDataSource
constructor
private
A new instance of StreamingDataSource.
-
#set_diagnostic_accumulator(diagnostic_accumulator) ⇒ Object
private
Sets the diagnostic accumulator for streaming initialization metrics.
-
#stop ⇒ Object
private
Stops the streaming synchronizer.
-
#sync(ss) {|update| ... } ⇒ Object
private
sync begins the synchronization process for the data source, yielding Update objects until the connection is closed or an unrecoverable error occurs.
Constructor Details
#initialize(sdk_key, http_config, initial_reconnect_delay, config) ⇒ StreamingDataSource
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of StreamingDataSource.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ldclient-rb/impl/data_system/streaming.rb', line 39 def initialize(sdk_key, http_config, initial_reconnect_delay, config) @sdk_key = sdk_key @http_config = http_config @initial_reconnect_delay = initial_reconnect_delay @config = config @logger = config.logger @name = "StreamingDataSourceV2" @sse = nil @stopped = Concurrent::Event.new @diagnostic_accumulator = nil @connection_attempt_start_time = 0 end |
Instance Attribute Details
#name ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
31 32 33 |
# File 'lib/ldclient-rb/impl/data_system/streaming.rb', line 31 def name @name end |
Instance Method Details
#set_diagnostic_accumulator(diagnostic_accumulator) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the diagnostic accumulator for streaming initialization metrics.
57 58 59 |
# File 'lib/ldclient-rb/impl/data_system/streaming.rb', line 57 def set_diagnostic_accumulator(diagnostic_accumulator) @diagnostic_accumulator = diagnostic_accumulator end |
#stop ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Stops the streaming synchronizer.
201 202 203 204 205 |
# File 'lib/ldclient-rb/impl/data_system/streaming.rb', line 201 def stop @logger.info { "[LDClient] Stopping StreamingDataSourceV2 synchronizer" } @sse&.close @stopped.set end |
#sync(ss) {|update| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
sync begins the synchronization process for the data source, yielding Update objects until the connection is closed or an unrecoverable error occurs.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ldclient-rb/impl/data_system/streaming.rb', line 69 def sync(ss) @logger.info { "[LDClient] Starting StreamingDataSourceV2 synchronizer" } log_connection_started change_set_builder = LaunchDarkly::Interfaces::DataSystem::ChangeSetBuilder.new envid = nil # The FDv1 Fallback Directive is one-way and terminal: once any # connect handshake within this sync invocation carries it, the SDK # is committed to engaging FDv1 as soon as the next full payload # has been applied. We therefore latch this flag to true on first # observation and never reset it -- a mid-sync reconnect whose # response no longer carries the directive does NOT cancel a # directive seen earlier. This matches the Go and Python SDK # implementations, both of which use the same latch pattern. # # The flag has to bridge two callbacks: on_connect sees the response # headers but on_event does not. A local closed over by both blocks # is correct because: # 1. Scope -- bound to a single sync invocation, so a future sync # starts fresh. (Within this invocation, persistence across # reconnects is the intended semantics, per above.) # 2. Thread safety -- ld-eventsource dispatches on_connect, # on_event, and on_error on the same SSE worker thread, so # reads and writes here are single-threaded by construction. fdv1_fallback_pending = false base_uri = @http_config.base_uri + FDV2_STREAMING_ENDPOINT headers = Impl::Util.default_http_headers(@sdk_key, @config) opts = { headers: headers, read_timeout: STREAM_READ_TIMEOUT, logger: @logger, socket_factory: @http_config.socket_factory, reconnect_time: @initial_reconnect_delay, } @sse = SSE::Client.new(base_uri, **opts) do |client| client.on_connect do |headers| if headers # Per-environment identifier: server sends it on every connect, # but it never changes once known so only assign once. envid ||= LaunchDarkly::Impl::DataSystem.lookup_header(headers, LD_ENVID_HEADER) fdv1_fallback_pending = true if LaunchDarkly::Impl::DataSystem.fdv1_fallback_requested?(headers) end end client.on_event do |event| begin update = (event, change_set_builder, envid, fdv1_fallback_pending: fdv1_fallback_pending) next unless update log_connection_result(true) @connection_attempt_start_time = 0 yield update # When the FDv1 Fallback Directive rode along on a Valid update, close # the stream so the primary synchronizer is stopped once the directive # engages. process_message marks the Update with fallback_to_fdv1 only # on payloads that complete a transfer, so the consumer has already # applied the ChangeSet by the time we get here. if update.fallback_to_fdv1 fdv1_fallback_pending = false stop end rescue JSON::ParserError => e @logger.info { "[LDClient] Error parsing stream event; will restart stream: #{e}" } yield LaunchDarkly::Interfaces::DataSystem::Update.new( state: LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED, error: LaunchDarkly::Interfaces::DataSource::ErrorInfo.new( LaunchDarkly::Interfaces::DataSource::ErrorInfo::INVALID_DATA, 0, e.to_s, Time.now ), environment_id: envid ) # Re-raise the exception so the SSE implementation can catch it and restart the stream. raise rescue => e @logger.info { "[LDClient] Error while handling stream event; will restart stream: #{e}" } yield LaunchDarkly::Interfaces::DataSystem::Update.new( state: LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED, error: LaunchDarkly::Interfaces::DataSource::ErrorInfo.new( LaunchDarkly::Interfaces::DataSource::ErrorInfo::UNKNOWN, 0, e.to_s, Time.now ), environment_id: envid ) # Re-raise the exception so the SSE implementation can catch it and restart the stream. raise end end client.on_error do |error| log_connection_result(false) fallback = false if error.respond_to?(:headers) && error.headers envid ||= LaunchDarkly::Impl::DataSystem.lookup_header(error.headers, LD_ENVID_HEADER) fallback = true if LaunchDarkly::Impl::DataSystem.fdv1_fallback_requested?(error.headers) end update = handle_error(error, envid, fallback) yield update if update end client.query_params do selector = ss.selector { "filter" => @config.payload_filter_key, "basis" => (selector.state if selector&.defined?), }.compact end end unless @sse @logger.error { "[LDClient] Failed to create SSE client for streaming updates" } return end # Client auto-starts in background thread. Wait here until stop() is called. @stopped.wait end |