-
Notifications
You must be signed in to change notification settings - Fork 45
chore: Introducing Synchronizer protocol & streaming implementation #344
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
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,77 @@ | ||
| """ | ||
| This module houses FDv2 types and implementations of synchronizers and | ||
| initializers for the datasystem. | ||
|
|
||
| All types and implementations in this module are considered internal | ||
| and are not part of the public API of the LaunchDarkly Python SDK. | ||
| They are subject to change without notice and should not be used directly | ||
| by users of the SDK. | ||
|
|
||
| You have been warned. | ||
| """ | ||
|
|
||
| from abc import abstractmethod | ||
| from dataclasses import dataclass | ||
| from typing import Generator, Iterable, Mapping, Optional, Protocol, Tuple | ||
|
|
||
| from ldclient.impl.datasystem.protocolv2 import ChangeSet, Selector | ||
| from ldclient.impl.util import _Result | ||
| from ldclient.interfaces import DataSourceErrorInfo, DataSourceState | ||
|
|
||
| PollingResult = _Result[Tuple[ChangeSet, Mapping], str] | ||
|
|
||
|
|
||
| class PollingRequester(Protocol): # pylint: disable=too-few-public-methods | ||
| """ | ||
| PollingRequester allows PollingDataSource to delegate fetching data to | ||
| another component. | ||
|
|
||
| This is useful for testing the PollingDataSource without needing to set up | ||
| a test HTTP server. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def fetch(self, selector: Optional[Selector]) -> PollingResult: | ||
| """ | ||
| Fetches the data for the given selector. | ||
| Returns a Result containing a tuple of ChangeSet and any request headers, | ||
| or an error if the data could not be retrieved. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Update: | ||
| """ | ||
| Update represents the results of a synchronizer's ongoing sync | ||
| method. | ||
| """ | ||
|
|
||
| state: DataSourceState | ||
| change_set: Optional[ChangeSet] = None | ||
| error: Optional[DataSourceErrorInfo] = None | ||
| revert_to_fdv1: bool = False | ||
| environment_id: Optional[str] = None | ||
|
|
||
|
|
||
| class Synchronizer(Protocol): # pylint: disable=too-few-public-methods | ||
| """ | ||
| Synchronizer represents a component capable of synchronizing data from an external | ||
| data source, such as a streaming or polling API. | ||
|
|
||
| It is responsible for yielding Update objects that represent the current state | ||
| of the data source, including any changes that have occurred since the last | ||
| synchronization. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def sync(self) -> Generator[Update, None, None]: | ||
| """ | ||
| sync should begin the synchronization process for the data source, yielding | ||
| Update objects until the connection is closed or an unrecoverable error | ||
| occurs. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| __all__: list[str] = [] | ||
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.
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.
Moving the shared types to this top level. In my next bit of work, I'm going to be re-arranging the polling stuff, so just focus on streaming for now.