-
Notifications
You must be signed in to change notification settings - Fork 18
feat(SDK-2185): FDv2 streaming base, initializer, and synchronizer #267
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
Draft
kinyoklion
wants to merge
3
commits into
main
Choose a base branch
from
rlamb/sdk-2185/fdv2-streaming
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.
Draft
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
231 changes: 231 additions & 0 deletions
231
packages/common_client/lib/src/data_sources/fdv2/streaming_base.dart
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 |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:launchdarkly_dart_common/launchdarkly_dart_common.dart'; | ||
| import 'package:launchdarkly_event_source_client/launchdarkly_event_source_client.dart'; | ||
|
|
||
| import 'flag_eval_mapper.dart'; | ||
| import 'protocol_handler.dart'; | ||
| import 'protocol_types.dart'; | ||
| import 'source.dart'; | ||
| import 'source_result.dart'; | ||
|
|
||
| /// Long-lived streaming data source over SSE. | ||
| /// | ||
| /// Wraps an [SSEClient] with FDv2 protocol semantics. Each named SSE | ||
| /// event is parsed as JSON, wrapped in an [FDv2Event], and fed to a | ||
| /// fresh [FDv2ProtocolHandler]. The first emitted [ProtocolAction] | ||
| /// per event is translated into an [FDv2SourceResult]: | ||
| /// | ||
| /// - [ActionPayload] --> [ChangeSetResult] with `persist: true`. | ||
| /// - [ActionGoodbye] --> goodbye [StatusResult]; the SSE connection is | ||
| /// closed. | ||
| /// - [ActionServerError] / [ActionError] --> interrupted | ||
| /// [StatusResult]; the SSE client's built-in retry handles the | ||
| /// reconnect. | ||
| /// - [ActionNone] --> no emission (waiting for more events). | ||
| /// | ||
| /// Legacy `ping` events are routed to the injected [PingHandler] (which | ||
| /// performs a one-shot poll) and the result is forwarded to the | ||
| /// stream. This is the streaming-to-polling bridge for older servers | ||
| /// that pre-date FDv2. | ||
| /// | ||
| /// The `x-ld-fd-fallback` header on the initial connection's response | ||
| /// is detected and produces a terminal-error result with | ||
| /// `fdv1Fallback: true`. The connection is closed. | ||
| /// | ||
| /// Lifecycle: a single-subscription stream. [results] starts the SSE | ||
| /// connection on subscribe; cancelling the subscription tears it down | ||
| /// without emitting a shutdown. [close] both stops the source and | ||
| /// emits a shutdown [StatusResult] before closing the stream. Both | ||
| /// paths funnel through a `Completer<void> _stoppedSignal` so async | ||
| /// callbacks short-circuit safely. | ||
| final class FDv2StreamingBase { | ||
| final SSEClient _sseClient; | ||
| final PingHandler _pingHandler; | ||
| final DateTime Function() _now; | ||
| final LDLogger _logger; | ||
|
|
||
| late final StreamController<FDv2SourceResult> _controller; | ||
| final Completer<void> _stoppedSignal = Completer<void>(); | ||
| StreamSubscription<Event>? _sseSubscription; | ||
| FDv2ProtocolHandler? _handler; | ||
| String? _environmentId; | ||
|
|
||
| FDv2StreamingBase({ | ||
| required SSEClient sseClient, | ||
| required PingHandler pingHandler, | ||
| required LDLogger logger, | ||
| DateTime Function()? now, | ||
| }) : _sseClient = sseClient, | ||
| _pingHandler = pingHandler, | ||
| _logger = logger.subLogger('FDv2StreamingBase'), | ||
| _now = now ?? DateTime.now { | ||
| _controller = StreamController<FDv2SourceResult>( | ||
| onListen: _onListen, | ||
| onCancel: _onCancel, | ||
| ); | ||
| } | ||
|
|
||
| /// Single-subscription stream of results. The SSE connection is | ||
| /// established lazily on the first [Stream.listen] call. | ||
| Stream<FDv2SourceResult> get results => _controller.stream; | ||
|
|
||
| /// Stops the source, emits a shutdown [StatusResult], and closes the | ||
| /// stream. Idempotent. | ||
| void close() { | ||
| if (_stoppedSignal.isCompleted) return; | ||
| _stoppedSignal.complete(); | ||
| _tearDownConnection(); | ||
| _controller | ||
| .add(FDv2SourceResults.shutdown(message: 'Streaming source closed')); | ||
| _controller.close(); | ||
| } | ||
|
|
||
| void _onListen() { | ||
| // Build the protocol handler fresh for each connection so a | ||
| // partial transfer from a previous connection cannot bleed into | ||
| // the new one. | ||
| _handler = FDv2ProtocolHandler( | ||
| objProcessors: {flagEvalKind: processFlagEval}, | ||
| logger: _logger, | ||
| ); | ||
| _sseSubscription = _sseClient.stream.listen( | ||
| _handleEvent, | ||
| onError: _handleSseError, | ||
| ); | ||
| } | ||
|
|
||
| Future<void> _onCancel() async { | ||
| if (_stoppedSignal.isCompleted) return; | ||
| _stoppedSignal.complete(); | ||
| _tearDownConnection(); | ||
| // No shutdown emission -- the subscriber asked us to stop. | ||
| } | ||
|
|
||
| void _tearDownConnection() { | ||
| _sseSubscription?.cancel(); | ||
| _sseSubscription = null; | ||
| // Best-effort close. The SSE client may already be closed if it | ||
| // emitted an error; that's fine -- the operation is documented as | ||
| // safe in any state. | ||
| _sseClient.close(); | ||
| } | ||
|
|
||
| void _handleEvent(Event event) { | ||
| if (_stoppedSignal.isCompleted) return; | ||
| switch (event) { | ||
| case OpenEvent open: | ||
| _handleOpen(open); | ||
| case MessageEvent message: | ||
| _handleMessage(message); | ||
| } | ||
| } | ||
|
|
||
| void _handleOpen(OpenEvent event) { | ||
| final headers = event.headers; | ||
| if (headers == null) return; | ||
|
|
||
| final envId = headers['x-ld-envid']; | ||
| if (envId != null) { | ||
| _environmentId = envId; | ||
| } | ||
|
|
||
| final fallback = headers['x-ld-fd-fallback']?.toLowerCase() == 'true'; | ||
| if (fallback) { | ||
| _emit(FDv2SourceResults.terminalError( | ||
| message: 'Server requested FDv1 fallback', | ||
| fdv1Fallback: true, | ||
| )); | ||
| // Server told us to fall back; don't keep the connection open. | ||
| _tearDownConnection(); | ||
| _controller.close(); | ||
| } | ||
| } | ||
|
|
||
| Future<void> _handleMessage(MessageEvent event) async { | ||
| if (event.type == 'ping') { | ||
| // Legacy bridge: older servers may still send `ping` instead of | ||
| // FDv2 events. Defer to the injected handler for a one-shot poll. | ||
| await _handlePing(); | ||
| return; | ||
| } | ||
|
|
||
| final Map<String, dynamic> data; | ||
| try { | ||
| final decoded = jsonDecode(event.data); | ||
| if (decoded is! Map<String, dynamic>) { | ||
| _logger.warn('Ignoring SSE event with non-object data: ' | ||
| 'event=${event.type}'); | ||
| _emit(FDv2SourceResults.interrupted( | ||
| message: 'Streaming event payload was not a JSON object')); | ||
| return; | ||
| } | ||
| data = decoded; | ||
| } catch (err) { | ||
| _logger | ||
| .warn('Failed to parse SSE event data as JSON (${err.runtimeType})'); | ||
| _emit(FDv2SourceResults.interrupted( | ||
| message: 'Streaming event payload was not valid JSON')); | ||
| return; | ||
| } | ||
|
|
||
| final action = | ||
| _handler!.processEvent(FDv2Event(event: event.type, data: data)); | ||
| if (_stoppedSignal.isCompleted) return; | ||
|
|
||
| switch (action) { | ||
| case ActionPayload(:final payload): | ||
| _emit(ChangeSetResult( | ||
| payload: payload, | ||
| environmentId: _environmentId, | ||
| freshness: _now(), | ||
| persist: true, | ||
| )); | ||
| case ActionGoodbye(:final reason): | ||
| _emit(FDv2SourceResults.goodbyeResult(message: reason)); | ||
| // Server told us to disconnect; close instead of letting the | ||
| // SSE client retry into a closed channel. | ||
| _tearDownConnection(); | ||
| _controller.close(); | ||
| case ActionServerError(:final reason): | ||
| _emit(FDv2SourceResults.interrupted(message: reason)); | ||
| case ActionError(:final message): | ||
| _emit(FDv2SourceResults.interrupted(message: message)); | ||
| case ActionNone(): | ||
| // No emission; continue accumulating events until the handler | ||
| // reaches a terminal action. | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| Future<void> _handlePing() async { | ||
| final FDv2SourceResult result; | ||
| try { | ||
| result = await _pingHandler(); | ||
| } catch (err) { | ||
| _logger.warn('Ping handler threw unexpectedly: ${err.runtimeType}'); | ||
| _emit(FDv2SourceResults.interrupted( | ||
| message: 'Ping handler raised error unexpectedly')); | ||
| return; | ||
| } | ||
| if (_stoppedSignal.isCompleted) return; | ||
| _emit(result); | ||
| } | ||
|
|
||
| void _handleSseError(Object err, StackTrace stack) { | ||
| if (_stoppedSignal.isCompleted) return; | ||
| // The SSE client's built-in backoff handles reconnection. Surface | ||
| // the disruption as interrupted; the orchestrator decides whether | ||
| // to fall through to a different source after enough time. | ||
| _logger.warn('SSE error (${err.runtimeType}); will retry'); | ||
| _logger.debug('SSE error detail: $err\n$stack'); | ||
| _emit(FDv2SourceResults.interrupted(message: 'Streaming connection error')); | ||
| } | ||
|
|
||
| void _emit(FDv2SourceResult result) { | ||
| if (_stoppedSignal.isCompleted) return; | ||
| if (_controller.isClosed) return; | ||
| _controller.add(result); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
packages/common_client/lib/src/data_sources/fdv2/streaming_initializer.dart
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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'source.dart'; | ||
| import 'source_result.dart'; | ||
| import 'streaming_base.dart'; | ||
|
|
||
| /// One-shot streaming initializer. | ||
| /// | ||
| /// Subscribes to the underlying [FDv2StreamingBase], returns the first | ||
| /// emitted [FDv2SourceResult], and tears the connection down. Used at | ||
| /// SDK init time to bring the SDK to a usable state from the streaming | ||
| /// path before handing off to the long-lived synchronizer. | ||
| /// | ||
| /// Calling [close] before the first emission resolves the pending | ||
| /// [run] future with a [SourceState.shutdown] result. | ||
| final class FDv2StreamingInitializer implements Initializer { | ||
| final FDv2StreamingBase _base; | ||
| final Completer<FDv2SourceResult> _completer = Completer<FDv2SourceResult>(); | ||
| StreamSubscription<FDv2SourceResult>? _subscription; | ||
| bool _closed = false; | ||
|
|
||
| FDv2StreamingInitializer({required FDv2StreamingBase base}) : _base = base; | ||
|
|
||
| @override | ||
| Future<FDv2SourceResult> run() { | ||
| if (_closed) { | ||
| return Future.value(_shutdownResult()); | ||
| } | ||
| _subscription = _base.results.listen((result) { | ||
| if (_completer.isCompleted) return; | ||
| _completer.complete(result); | ||
| // First emission received; tear down. | ||
| _subscription?.cancel(); | ||
| _subscription = null; | ||
| _base.close(); | ||
| }, onDone: () { | ||
| if (_completer.isCompleted) return; | ||
| // The base closed before producing a result. Surface as shutdown. | ||
| _completer.complete(_shutdownResult()); | ||
| }); | ||
| return _completer.future; | ||
| } | ||
|
|
||
| @override | ||
| void close() { | ||
| if (_closed) return; | ||
| _closed = true; | ||
| _subscription?.cancel(); | ||
| _subscription = null; | ||
| _base.close(); | ||
| if (!_completer.isCompleted) { | ||
| _completer.complete(_shutdownResult()); | ||
| } | ||
| } | ||
|
|
||
| StatusResult _shutdownResult() => FDv2SourceResults.shutdown( | ||
| message: 'Streaming initializer closed before first emission', | ||
| ); | ||
| } |
22 changes: 22 additions & 0 deletions
22
packages/common_client/lib/src/data_sources/fdv2/streaming_synchronizer.dart
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import 'source.dart'; | ||
| import 'source_result.dart'; | ||
| import 'streaming_base.dart'; | ||
|
|
||
| /// Long-lived streaming synchronizer. | ||
| /// | ||
| /// A thin adapter that exposes [FDv2StreamingBase.results] as a | ||
| /// [Synchronizer]. The base class already implements all of the | ||
| /// connection lifecycle, protocol parsing, and error handling; this | ||
| /// wrapper exists only to satisfy the [Synchronizer] interface so the | ||
| /// orchestrator can treat polling and streaming uniformly. | ||
| final class FDv2StreamingSynchronizer implements Synchronizer { | ||
| final FDv2StreamingBase _base; | ||
|
|
||
| FDv2StreamingSynchronizer({required FDv2StreamingBase base}) : _base = base; | ||
|
|
||
| @override | ||
| Stream<FDv2SourceResult> get results => _base.results; | ||
|
|
||
| @override | ||
| void close() => _base.close(); | ||
| } |
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.
I am undecided if we should support this. If we don't then we can combine the base into the synchronizer and discard it.
Flutter does run on web, but it is inherently a single page app. So I don't think one-shot mode is nearly as important as it is for js.