-
Notifications
You must be signed in to change notification settings - Fork 18
chore: adds connection mode resolution and source factories #265
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
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
22787c6
feat: add polling and cache sources for FDv2
kinyoklion 07cd384
docs: clarify cache_initializer doc -- no retry, not synchronous
kinyoklion 6b6e824
refactor: address review feedback on SDK-2184
kinyoklion 34a5d87
chore: adds data system mode definitions
tanderson-ld 5fd29dc
Merge branch 'rlamb/sdk-2184/fdv2-polling-cache-sources', remote-trac…
tanderson-ld b6bd1de
chore: adds source factories
tanderson-ld be07346
adds ResolvedConnectionMode sealed class to contain details of resolu…
tanderson-ld 2f1cd6e
Merge remote-tracking branch 'origin' into ta/SDK-2187/connection-mod…
tanderson-ld 3d5e4c8
fixing a couple backwards compat issues with background mode defaults
tanderson-ld 1b53f95
making automatic handling platform specific
tanderson-ld d8bce6d
tweaking lambda to be function
tanderson-ld b3f3eeb
clarifying background mode offline reason
tanderson-ld a54441c
removing exported resolveMode for now
tanderson-ld d9b11ac
fixing some formats
tanderson-ld 62559fe
Merge branch 'main' into ta/SDK-2187/connection-mode-and-resolution
tanderson-ld 082b5a3
Merge branch 'main' into ta/SDK-2187/connection-mode-and-resolution
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
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
|
Member
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. I am not sure about changing this directly instead of this being parallel |
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
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
| import 'mode_definition.dart'; | ||
|
|
||
| /// Built-in [ModeDefinition] values. | ||
| abstract final class BuiltInModes { | ||
| BuiltInModes._(); | ||
|
|
||
| /// Default foreground poll interval. | ||
| static const Duration _foregroundPollInterval = Duration(seconds: 300); | ||
|
|
||
| static const Duration defaultBackgroundPollInterval = Duration(seconds: 3600); | ||
|
|
||
| /// Default streaming mode (mobile foreground / desktop). | ||
| 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: [], | ||
| ); | ||
|
|
||
| /// Mobile background: cache initializer, reduced-rate polling synchronizer (CSFDV2 §5.2.3). | ||
| static const ModeDefinition background = ModeDefinition( | ||
| initializers: [CacheInitializer()], | ||
| synchronizers: [ | ||
| PollingSynchronizer(pollInterval: defaultBackgroundPollInterval), | ||
| ], | ||
| fdv1Fallback: Fdv1FallbackConfig( | ||
| pollInterval: defaultBackgroundPollInterval, | ||
| ), | ||
| ); | ||
| } |
175 changes: 175 additions & 0 deletions
175
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,175 @@ | ||
| 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 optional 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, | ||
| ); | ||
| } | ||
|
|
||
| FDv2PollingBase _sharedPollingBase({ | ||
| 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 ([CONNMODE] / CSFDv2 cache-miss success rule). | ||
| final bool isCache; | ||
|
|
||
| final Initializer Function(SelectorGetter selectorGetter) _create; | ||
|
|
||
| InitializerFactory({ | ||
| required Initializer Function(SelectorGetter selectorGetter) create, | ||
| this.isCache = false, | ||
| }) : _create = create; | ||
|
|
||
| /// Returns a **new** [Initializer] bound to [selectorGetter] (or ignores it | ||
| /// for cache, matching JS). | ||
| 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: | ||
| final base = _sharedPollingBase( | ||
| endpoints: e.endpoints, | ||
| usePost: e.usePost, | ||
| ctx: ctx, | ||
| ); | ||
| return InitializerFactory( | ||
| create: (SelectorGetter selectorGetter) => 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 base = _sharedPollingBase( | ||
| endpoints: e.endpoints, | ||
| usePost: e.usePost, | ||
| ctx: ctx, | ||
| ); | ||
| final interval = e.pollInterval ?? ctx.defaultPollingInterval; | ||
| return SynchronizerFactory( | ||
| create: (SelectorGetter selectorGetter) => 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(); | ||
| } |
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.
Is this a safe change? Public enum.