feat: lock provider binding to a single API instance#1342
feat: lock provider binding to a single API instance#1342lukas-reining wants to merge 1 commit intomainfrom
Conversation
Summary of ChangesHello @lukas-reining, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a critical feature to enforce that an OpenFeature provider instance is exclusively bound to a single OpenFeature API instance at any given time. This change is essential for managing provider lifecycles effectively, especially in environments where multiple API instances might exist. The implementation uses a symbolic key for binding and includes robust error handling and cleanup procedures, all while remaining transparent to provider implementations. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
| /** | ||
| * Well-known Symbol used to track which {@link OpenFeatureCommonAPI} instance a provider is currently bound to. | ||
| * The symbol is shared across module boundaries and globally unique in the runtime. | ||
| * @internal |
There was a problem hiding this comment.
Do we have to move this to an extra internal file as we do with OpenFeatureClient implementations?
This is only exported for the tests.
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to lock a provider instance to a single OpenFeature API instance, which is an important step towards supporting non-singleton APIs. The implementation uses a well-known Symbol to bind providers to an API instance and correctly handles unbinding when providers are replaced or the API is shut down. The changes are well-contained within the shared OpenFeatureCommonAPI and are accompanied by a comprehensive set of tests for both server and web packages. The approach is transparent to provider implementations, which is great.
I have one suggestion to improve code clarity regarding some redundant optional chaining. Overall, this is a solid contribution.
| await wrapper?.provider.onClose?.(); | ||
| } catch (err) { | ||
| this.handleShutdownError(wrapper?.provider, err); | ||
| } finally { | ||
| this.unbindProvider(wrapper?.provider); | ||
| } |
There was a problem hiding this comment.
The optional chaining on wrapper seems unnecessary. wrapper is destructured from an array of _domainScopedProviders entries and should not be nullable. The provider property on ProviderWrapper is also not nullable. Using wrapper.provider directly would improve clarity and type safety.
| await wrapper?.provider.onClose?.(); | |
| } catch (err) { | |
| this.handleShutdownError(wrapper?.provider, err); | |
| } finally { | |
| this.unbindProvider(wrapper?.provider); | |
| } | |
| await wrapper.provider.onClose?.(); | |
| } catch (err) { | |
| this.handleShutdownError(wrapper.provider, err); | |
| } finally { | |
| this.unbindProvider(wrapper.provider); | |
| } |
| private bindProvider(provider: P): void { | ||
| Object.defineProperty(provider, BOUND_API_KEY, { | ||
| value: this, | ||
| configurable: true, | ||
| enumerable: false, | ||
| writable: false, | ||
| }); | ||
| } |
There was a problem hiding this comment.
This way is a transparent implementation to the providers.
We can not directly add it to the provider interface "non-breaking" as it only is an interface.
6fb07fc to
1aab336
Compare
Signed-off-by: Lukas Reining <[email protected]>
1aab336 to
f60c147
Compare
This PR
Once we implement non-singleton OpenFeature APIs as discussed in open-feature/spec#359, we need to make sure a single provider is only ever assigned to one API instance at a time. This is necessary due to the API instance managing the lifecycle of the provider.
This PR drafts a mechanism to track and enforce the association between provider instances and
OpenFeatureAPIinstances, preventing a provider from being registered with multiple APIs at the same time.It does this by using a symbol to "bind" a provider to an API instance and ensures proper cleanup (unbinding) when providers are replaced or the API is shut down.
This change is transparent to the provider implementations.
Related Issues
open-feature/spec#359