-
Notifications
You must be signed in to change notification settings - Fork 20
fix(core): Return new http client for each httputil call, avoid shared state #2662
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
Conversation
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.
Summary of Changes
Hello @elizabethhealy, 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 addresses potential shared state issues within the httputil
package by modifying how http.Client
instances are provided. Instead of returning a globally shared client, functions now return a new client instance for each call. This change ensures better isolation between HTTP requests and prevents unintended interactions that could arise from multiple callers using the same client instance, thereby improving the robustness and predictability of HTTP operations.
Highlights
- HTTP Client Instantiation: The
SafeHTTPClient
function now creates and returns a freshhttp.Client
instance for every invocation, ensuring no shared state between callers. - Global Client Removal: The previously shared
safeHTTPClient
global variable has been removed, eliminating a potential source of concurrency issues related to shared client state. - Consistent Client Provisioning: Functions like
SafeHTTPClientWithTLSConfig
andSafeHTTPClientWithTransport
have been updated to consistently use the newSafeHTTPClient
function when a default client is needed, reinforcing the "new instance per call" pattern.
Using Gemini Code Assist
The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
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 .gemini/
folder in the base of the repository. Detailed instructions can be found here.
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.
A client, once shared, Now new with every call, No state to be snared.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request correctly addresses a potential issue with shared mutable state by changing the http.Client
singleton to a factory pattern that returns a new client on each call. This is a good improvement. My review includes one suggestion to update the documentation to reflect this new behavior, which will help users of this SDK understand that they receive a new instance each time and can modify it without side effects. This clarification will improve the maintainability and usability of the package.
Benchmark results, click to expandBenchmark authorization.GetDecisions Results:
Benchmark authorization.v2.GetMultiResourceDecision Results:
Standard Benchmark Metrics Skipped or FailedBulk Benchmark Results
TDF3 Benchmark Results:
NANOTDF Benchmark Results:
|
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.
Can you explain the motivation for this change? Reusing the http.Client is the best way to reuse connections, reducing the latency for ssl handshakes.
It's also considered go best practices to reuse the http.Client.
@jentfoo im worried about different services/peps calling it to get a client, but one service decides they need a longer timeout so they edit the returned client affecting the rest of the services using it |
@elizabethhealy Modifying an already in use http.Client would be a concerning pattern, one we should avoid. If custom configurations are needed it's expected that instead a specific client will be constructed with one of the helpers (e.g. If the intention that the implementor handles the de-duplication of the http.Client we would need additional changes to support that. As is this will cause a performance and memory overhead regression. |
I thought about this API more over night. I think there is the potential for some improvement here. After more thought the fact that empty config is shared and unique configs are not shared could be a source of confusion. That said, I am still not convinced that anyone is (or should be) modifying the http.Client after returned. And do view this as a simpler option. Let me know your thoughts when you get a chance, or if you want to just put heads together and brainstorm this API some more. |
@jentfoo i agree i think modifying the client after return isnt the right flow a pep should be taking, maybe we opt for more documentation instead of this change to make sure peps dont take that route |
@elizabethhealy Let me know if you want me to expand the godocs or have other ideas on this API. Client reuse is important for both security and performance, I also care a lot about our API ergonomics to ensure this is easy to understand and adopt. |
Proposed Changes
Checklist
Testing Instructions