-
Notifications
You must be signed in to change notification settings - Fork 43
feat!: add isolated API instances, remove duped global funcs #591
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
Open
marcozabel
wants to merge
5
commits into
open-feature:main
Choose a base branch
from
marcozabel:feat/isolated-api-instances-clean
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5deba25
refactor: to support isolated instances
marcozabel 7ad838b
feat(isolated): add factory module for isolated API instances
marcozabel 556afed
fixup: remove lazy imports, shims, add check
toddbaert 0ba9546
fixup: pr feedback
toddbaert 5b46606
fixup: pr feedback
toddbaert 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from openfeature._event_support import EventSupport | ||
| from openfeature.client import OpenFeatureClient | ||
| from openfeature.evaluation_context import EvaluationContext | ||
| from openfeature.event import EventHandler, ProviderEvent | ||
| from openfeature.exception import GeneralError | ||
| from openfeature.hook import Hook | ||
| from openfeature.provider import FeatureProvider, ProviderStatus | ||
| from openfeature.provider._registry import ProviderRegistry | ||
| from openfeature.provider.metadata import Metadata | ||
| from openfeature.transaction_context import TransactionContextPropagator | ||
| from openfeature.transaction_context.no_op_transaction_context_propagator import ( | ||
| NoOpTransactionContextPropagator, | ||
| ) | ||
|
|
||
|
|
||
| class OpenFeatureAPI: | ||
| """An independent OpenFeature API instance with its own isolated state. | ||
|
|
||
| Each instance maintains its own providers, evaluation context, hooks, | ||
| event handlers, and transaction context propagator; fully separate from | ||
| the global singleton and from other instances. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| self._hooks: list[Hook] = [] | ||
|
toddbaert marked this conversation as resolved.
|
||
| self._evaluation_context = EvaluationContext() | ||
| self._transaction_context_propagator: TransactionContextPropagator = ( | ||
| NoOpTransactionContextPropagator() | ||
| ) | ||
| self._event_support = EventSupport() | ||
| self._provider_registry = ProviderRegistry( | ||
| event_support=self._event_support, | ||
| evaluation_context_getter=self.get_evaluation_context, | ||
| ) | ||
|
|
||
| # --- Client creation --- | ||
|
|
||
| def get_client( | ||
| self, domain: str | None = None, version: str | None = None | ||
| ) -> OpenFeatureClient: | ||
| return OpenFeatureClient(domain=domain, version=version, api=self) | ||
|
|
||
| # --- Provider management --- | ||
|
|
||
| def set_provider( | ||
| self, provider: FeatureProvider, domain: str | None = None | ||
| ) -> None: | ||
| if domain is None: | ||
| self._provider_registry.set_default_provider(provider) | ||
| else: | ||
| self._provider_registry.set_provider(domain, provider) | ||
|
|
||
| def set_provider_and_wait( | ||
| self, provider: FeatureProvider, domain: str | None = None | ||
| ) -> None: | ||
| if domain is None: | ||
| self._provider_registry.set_default_provider(provider, wait_for_init=True) | ||
| else: | ||
| self._provider_registry.set_provider(domain, provider, wait_for_init=True) | ||
|
|
||
| def get_provider_metadata(self, domain: str | None = None) -> Metadata: | ||
| return self._provider_registry.get_provider(domain).get_metadata() | ||
|
|
||
| def get_provider(self, domain: str | None = None) -> FeatureProvider: | ||
| return self._provider_registry.get_provider(domain) | ||
|
|
||
| def get_provider_status(self, provider: FeatureProvider) -> ProviderStatus: | ||
| return self._provider_registry.get_provider_status(provider) | ||
|
|
||
| def clear_providers(self) -> None: | ||
| self._provider_registry.clear_providers() | ||
| self._event_support.clear() | ||
|
|
||
| def shutdown(self) -> None: | ||
| # shutdown -> remove providers -> set default provider to NoOp -> remove event handlers | ||
| self.clear_providers() | ||
| # remove hooks | ||
| self.clear_hooks() | ||
| # set evaluation context to default | ||
| self._evaluation_context = EvaluationContext() | ||
| # set propagator to NoOp | ||
| self._transaction_context_propagator = NoOpTransactionContextPropagator() | ||
|
|
||
| # --- Hooks --- | ||
|
|
||
| def add_hooks(self, hooks: list[Hook]) -> None: | ||
| self._hooks = self._hooks + hooks | ||
|
|
||
| def clear_hooks(self) -> None: | ||
| self._hooks = [] | ||
|
|
||
| def get_hooks(self) -> list[Hook]: | ||
| return self._hooks | ||
|
toddbaert marked this conversation as resolved.
|
||
|
|
||
| # --- Evaluation context --- | ||
|
|
||
| def get_evaluation_context(self) -> EvaluationContext: | ||
| return self._evaluation_context | ||
|
|
||
| def set_evaluation_context(self, evaluation_context: EvaluationContext) -> None: | ||
| if evaluation_context is None: | ||
| raise GeneralError(error_message="No api level evaluation context") | ||
| self._evaluation_context = evaluation_context | ||
|
toddbaert marked this conversation as resolved.
|
||
|
|
||
| def clear_evaluation_context(self) -> None: | ||
| self.set_evaluation_context(EvaluationContext()) | ||
|
|
||
| # --- Transaction context --- | ||
|
|
||
| def set_transaction_context_propagator( | ||
| self, transaction_context_propagator: TransactionContextPropagator | ||
| ) -> None: | ||
| self._transaction_context_propagator = transaction_context_propagator | ||
|
toddbaert marked this conversation as resolved.
|
||
|
|
||
| def clear_transaction_context_propagator(self) -> None: | ||
| self.set_transaction_context_propagator(NoOpTransactionContextPropagator()) | ||
|
|
||
| def get_transaction_context(self) -> EvaluationContext: | ||
| return self._transaction_context_propagator.get_transaction_context() | ||
|
|
||
| def set_transaction_context(self, evaluation_context: EvaluationContext) -> None: | ||
| self._transaction_context_propagator.set_transaction_context(evaluation_context) | ||
|
|
||
| # --- Event handlers --- | ||
|
|
||
| def add_handler(self, event: ProviderEvent, handler: EventHandler) -> None: | ||
| self._event_support.add_global_handler(event, handler, self.get_client) | ||
|
|
||
| def remove_handler(self, event: ProviderEvent, handler: EventHandler) -> None: | ||
| self._event_support.remove_global_handler(event, handler) | ||
|
|
||
|
|
||
| _default_api = OpenFeatureAPI() | ||
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.
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.