-
Notifications
You must be signed in to change notification settings - Fork 18
feat: Add FDv2 connection mode, resolved-mode, and resolution types #274
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
Merged
tanderson-ld
merged 8 commits into
main
from
ta/SDK-2187/connection-mode-and-resolution-additive
May 29, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b1fc341
feat(SDK-2187): Add FDv2 connection mode, resolved-mode, and resoluti…
tanderson-ld dd3875b
chore: dart format fdv2_connection_mode.dart
tanderson-ld 6881735
self review
tanderson-ld 4779d27
fix(SDK-2187): Configured offline background slot is OfflineSetOffline
tanderson-ld 4de8407
refactor(SDK-2187): FDv2ConnectionMode as sealed class
tanderson-ld d9a48f5
Merge branch 'main' into ta/SDK-2187/connection-mode-and-resolution-a…
tanderson-ld b012413
fix(SDK-2187): Polling factory builds requestor per create() call
tanderson-ld 8471c7d
Update packages/common_client/lib/src/data_sources/fdv2/mode_resoluti…
tanderson-ld 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
53 changes: 53 additions & 0 deletions
53
packages/common_client/lib/src/data_sources/fdv2/built_in_modes.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,53 @@ | ||
| import 'mode_definition.dart'; | ||
|
|
||
| /// Built-in [ModeDefinition] values. | ||
| abstract final class BuiltInModes { | ||
| BuiltInModes._(); | ||
|
|
||
| /// Default foreground poll interval. | ||
| static const Duration _foregroundPollInterval = Duration(seconds: 300); | ||
|
|
||
| /// Default background poll interval. | ||
| static const Duration defaultBackgroundPollInterval = Duration(seconds: 3600); | ||
|
|
||
| /// Streaming: combination of cache and polling initializers, and streaming and fallback polling synchronizer. | ||
| static const ModeDefinition streaming = ModeDefinition( | ||
| initializers: [ | ||
| CacheInitializer(), | ||
| PollingInitializer(), | ||
| ], | ||
| synchronizers: [ | ||
| StreamingSynchronizer(), | ||
| PollingSynchronizer(), | ||
| ], | ||
| fdv1Fallback: Fdv1FallbackConfig( | ||
| pollInterval: _foregroundPollInterval, | ||
| ), | ||
| ); | ||
|
|
||
| /// Polling-only mode. | ||
| static const ModeDefinition polling = ModeDefinition( | ||
| initializers: [CacheInitializer()], | ||
| synchronizers: [PollingSynchronizer()], | ||
| fdv1Fallback: Fdv1FallbackConfig( | ||
| pollInterval: _foregroundPollInterval, | ||
| ), | ||
| ); | ||
|
|
||
| /// Offline: cache initializer only; no synchronizers. | ||
| static const ModeDefinition offline = ModeDefinition( | ||
| initializers: [CacheInitializer()], | ||
| synchronizers: [], | ||
| ); | ||
|
|
||
| /// Background: cache initializer, reduced-rate polling synchronizer | ||
| static const ModeDefinition background = ModeDefinition( | ||
| initializers: [CacheInitializer()], | ||
| synchronizers: [ | ||
| PollingSynchronizer(pollInterval: defaultBackgroundPollInterval), | ||
| ], | ||
| fdv1Fallback: Fdv1FallbackConfig( | ||
| pollInterval: defaultBackgroundPollInterval, | ||
| ), | ||
| ); | ||
| } |
181 changes: 181 additions & 0 deletions
181
packages/common_client/lib/src/data_sources/fdv2/entry_factories.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,181 @@ | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:launchdarkly_dart_common/launchdarkly_dart_common.dart' | ||
| hide ServiceEndpoints; | ||
|
|
||
| import '../../config/service_endpoints.dart'; | ||
| import 'cache_initializer.dart' as cache_src; | ||
| import 'source_factory_context.dart'; | ||
| import 'mode_definition.dart' as mode; | ||
| import 'polling_base.dart'; | ||
| import 'polling_initializer.dart'; | ||
| import 'polling_synchronizer.dart'; | ||
| import 'requestor.dart'; | ||
| import 'selector.dart'; | ||
| import 'source.dart'; | ||
|
|
||
| /// Merges per-entry [mode.EndpointConfig] overrides into [base]. | ||
| ServiceEndpoints mergeServiceEndpoints( | ||
| ServiceEndpoints base, | ||
| mode.EndpointConfig? override, | ||
| ) { | ||
| if (override == null) { | ||
| return base; | ||
| } | ||
| if (override.pollingBaseUri == null && override.streamingBaseUri == null) { | ||
| return base; | ||
| } | ||
| return ServiceEndpoints.custom( | ||
| polling: override.pollingBaseUri?.toString() ?? base.polling, | ||
| streaming: override.streamingBaseUri?.toString() ?? base.streaming, | ||
| events: base.events, | ||
| ); | ||
| } | ||
|
|
||
| /// Builds a fresh [FDv2PollingBase] (and its underlying [FDv2Requestor]) per | ||
| /// call. Must be invoked inside the factory's `create` lambda so each | ||
| /// produced instance owns its own requestor; the requestor holds mutable | ||
| /// per-call state (e.g. ETag) and cannot be safely shared across instances. | ||
| FDv2PollingBase _buildPollingBase({ | ||
| required mode.EndpointConfig? endpoints, | ||
| required bool usePost, | ||
| required SourceFactoryContext ctx, | ||
| }) { | ||
| final endpointsResolved = | ||
| mergeServiceEndpoints(ctx.serviceEndpoints, endpoints); | ||
| final requestor = FDv2Requestor( | ||
| logger: ctx.logger, | ||
| endpoints: endpointsResolved, | ||
| contextEncoded: base64UrlEncode(utf8.encode(ctx.contextJson)), | ||
| contextJson: ctx.contextJson, | ||
| usePost: usePost, | ||
| withReasons: ctx.withReasons, | ||
| httpProperties: ctx.httpProperties, | ||
| httpClientFactory: ctx.httpClientFactory ?? _defaultHttpClientFactory, | ||
| ); | ||
| return FDv2PollingBase( | ||
| logger: ctx.logger, | ||
| requestor: requestor, | ||
| ); | ||
| } | ||
|
|
||
| HttpClient _defaultHttpClientFactory(HttpProperties httpProperties) { | ||
| return HttpClient(httpProperties: httpProperties); | ||
| } | ||
|
|
||
| /// A factory for creating [Initializer] instances. | ||
| final class InitializerFactory { | ||
| /// True for cache initializers. | ||
| final bool isCache; | ||
|
|
||
| final Initializer Function(SelectorGetter selectorGetter) _create; | ||
|
|
||
| InitializerFactory({ | ||
| required Initializer Function(SelectorGetter selectorGetter) create, | ||
| this.isCache = false, | ||
| }) : _create = create; | ||
|
|
||
| Initializer create(SelectorGetter selectorGetter) => _create(selectorGetter); | ||
| } | ||
|
|
||
| /// A factory for creating [Synchronizer] instances. | ||
| final class SynchronizerFactory { | ||
| final Synchronizer Function(SelectorGetter selectorGetter) _create; | ||
|
|
||
| SynchronizerFactory({ | ||
| required Synchronizer Function(SelectorGetter selectorGetter) create, | ||
| }) : _create = create; | ||
|
|
||
| Synchronizer create(SelectorGetter selectorGetter) => _create(selectorGetter); | ||
| } | ||
|
|
||
| /// Builds an [InitializerFactory] for a single [mode.InitializerEntry]. | ||
| /// | ||
| /// Throws [UnsupportedError] for unsupported entry types. | ||
| InitializerFactory createInitializerFactoryFromEntry( | ||
| mode.InitializerEntry entry, | ||
| SourceFactoryContext ctx, | ||
| ) { | ||
| switch (entry) { | ||
| case mode.CacheInitializer(): | ||
| return InitializerFactory( | ||
| isCache: true, | ||
| create: (_) => cache_src.CacheInitializer( | ||
| reader: ctx.cachedFlagsReader, | ||
| context: ctx.context, | ||
| logger: ctx.logger, | ||
| ), | ||
| ); | ||
| case final mode.PollingInitializer e: | ||
| return InitializerFactory( | ||
| create: (SelectorGetter selectorGetter) { | ||
| final base = _buildPollingBase( | ||
| endpoints: e.endpoints, | ||
| usePost: e.usePost, | ||
| ctx: ctx, | ||
| ); | ||
| return FDv2PollingInitializer( | ||
| poll: ({Selector basis = Selector.empty}) => | ||
| base.pollOnce(basis: basis), | ||
| selectorGetter: selectorGetter, | ||
| logger: ctx.logger, | ||
| ); | ||
| }, | ||
| ); | ||
| case mode.StreamingInitializer(): | ||
| throw UnsupportedError( | ||
| 'FDv2 StreamingInitializer factories are not implemented yet', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// Builds a [SynchronizerFactory] for a single [mode.SynchronizerEntry]. | ||
| /// | ||
| /// Throws [UnsupportedError] for unsupported entry types. | ||
| SynchronizerFactory createSynchronizerFactoryFromEntry( | ||
| mode.SynchronizerEntry entry, | ||
| SourceFactoryContext ctx, | ||
| ) { | ||
| switch (entry) { | ||
| case final mode.PollingSynchronizer e: | ||
| final interval = e.pollInterval ?? ctx.defaultPollingInterval; | ||
| return SynchronizerFactory( | ||
| create: (SelectorGetter selectorGetter) { | ||
| final base = _buildPollingBase( | ||
| endpoints: e.endpoints, | ||
| usePost: e.usePost, | ||
| ctx: ctx, | ||
| ); | ||
| return FDv2PollingSynchronizer( | ||
| poll: ({Selector basis = Selector.empty}) => | ||
| base.pollOnce(basis: basis), | ||
| selectorGetter: selectorGetter, | ||
| interval: interval, | ||
| logger: ctx.logger, | ||
| ); | ||
| }, | ||
| ); | ||
| case mode.StreamingSynchronizer(): | ||
| throw UnsupportedError( | ||
| 'FDv2 StreamingSynchronizer factories are not implemented yet', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// One factory per entry, in list order. | ||
| List<InitializerFactory> buildInitializerFactories( | ||
| List<mode.InitializerEntry> entries, | ||
| SourceFactoryContext ctx, | ||
| ) { | ||
| return entries.map((e) => createInitializerFactoryFromEntry(e, ctx)).toList(); | ||
| } | ||
|
|
||
| /// One factory per entry, in list order. | ||
| List<SynchronizerFactory> buildSynchronizerFactories( | ||
| List<mode.SynchronizerEntry> entries, | ||
| SourceFactoryContext ctx, | ||
| ) { | ||
| return entries | ||
| .map((e) => createSynchronizerFactoryFromEntry(e, ctx)) | ||
| .toList(); | ||
| } | ||
114 changes: 114 additions & 0 deletions
114
packages/common_client/lib/src/data_sources/fdv2/mode_definition.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,114 @@ | ||
| /// Per-source endpoint overrides. When fields are null, the client uses | ||
| /// the default [ServiceEndpoints] from config. | ||
| final class EndpointConfig { | ||
| final Uri? pollingBaseUri; | ||
| final Uri? streamingBaseUri; | ||
|
|
||
| const EndpointConfig({this.pollingBaseUri, this.streamingBaseUri}); | ||
| } | ||
|
|
||
| /// Marker class for separating initializers from other types of source entries. | ||
| sealed class InitializerEntry { | ||
| const InitializerEntry(); | ||
| } | ||
|
|
||
| /// Marker class for separating synchronizers from other types of source entries. | ||
| sealed class SynchronizerEntry { | ||
| const SynchronizerEntry(); | ||
| } | ||
|
|
||
| /// Initializer that will read data from cache. | ||
| final class CacheInitializer extends InitializerEntry { | ||
| const CacheInitializer(); | ||
| } | ||
|
|
||
| /// Initializer that will fetch data from polling endpoints. | ||
| final class PollingInitializer extends InitializerEntry { | ||
| /// Per-source endpoint overrides. | ||
| final EndpointConfig? endpoints; | ||
|
|
||
| /// Whether to use the POST semantics for this source. | ||
| final bool usePost; | ||
|
|
||
| const PollingInitializer({ | ||
| this.endpoints, | ||
| this.usePost = false, | ||
| }); | ||
| } | ||
|
|
||
| /// Streaming initializer (e.g. first payload from a stream). | ||
| final class StreamingInitializer extends InitializerEntry { | ||
| /// Initial reconnect delay for the streaming source. | ||
| final Duration? initialReconnectDelay; | ||
|
|
||
| /// Per-source endpoint overrides. | ||
| final EndpointConfig? endpoints; | ||
|
|
||
| /// Whether to use the POST semantics for this source. | ||
| final bool usePost; | ||
|
|
||
| const StreamingInitializer({ | ||
| this.initialReconnectDelay, | ||
| this.endpoints, | ||
| this.usePost = false, | ||
| }); | ||
| } | ||
|
|
||
| /// Long-lived polling synchronizer; [pollInterval] overrides client default when set. | ||
| final class PollingSynchronizer extends SynchronizerEntry { | ||
| /// Minimum polling interval for the synchronizer. | ||
| final Duration? pollInterval; | ||
|
|
||
| /// Per-source endpoint overrides. | ||
| final EndpointConfig? endpoints; | ||
|
|
||
| /// Whether to use the POST semantics for this source. | ||
| final bool usePost; | ||
|
|
||
| const PollingSynchronizer({ | ||
| this.pollInterval, | ||
| this.endpoints, | ||
| this.usePost = false, | ||
| }); | ||
| } | ||
|
|
||
| /// Long-lived streaming synchronizer. | ||
| final class StreamingSynchronizer extends SynchronizerEntry { | ||
| final Duration? initialReconnectDelay; | ||
|
|
||
| /// Per-source endpoint overrides. | ||
| final EndpointConfig? endpoints; | ||
|
|
||
| /// Whether to use the POST semantics for this source. | ||
| final bool usePost; | ||
|
|
||
| const StreamingSynchronizer({ | ||
| this.initialReconnectDelay, | ||
| this.endpoints, | ||
| this.usePost = false, | ||
| }); | ||
| } | ||
|
|
||
| /// Defines the initializers and synchronizers for a FDv2 connection mode. | ||
| final class ModeDefinition { | ||
| final List<InitializerEntry> initializers; | ||
| final List<SynchronizerEntry> synchronizers; | ||
| final Fdv1FallbackConfig? fdv1Fallback; | ||
|
|
||
| const ModeDefinition({ | ||
| required this.initializers, | ||
| required this.synchronizers, | ||
| this.fdv1Fallback, | ||
| }); | ||
| } | ||
|
|
||
| /// Configuration for the FDv1 fallback tier. | ||
| final class Fdv1FallbackConfig { | ||
| /// Minimum polling interval for the fallback synchronizer | ||
| final Duration? pollInterval; | ||
|
|
||
| /// Per-source endpoint overrides. | ||
| final EndpointConfig? endpoints; | ||
|
|
||
| const Fdv1FallbackConfig({this.pollInterval, this.endpoints}); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.