-
Notifications
You must be signed in to change notification settings - Fork 4
fix: return io.ReadCloser instead of io.Reader #26
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
base: master
Are you sure you want to change the base?
Conversation
allowing our sdk clients to prevent memory leaks
Summary of ChangesHello @nstapelbroek, 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 a critical resource management issue by modifying the return types of several download-related functions. Previously, these functions returned Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. 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
|
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.
Pull request overview
This PR updates download-related APIs to return io.ReadCloser instead of io.Reader, allowing callers to properly close HTTP response bodies and avoid connection leaks.
Changes:
- Change
HttpAPIClient.DownloadFileto returnio.ReadCloserand propagate the underlyingClient.Getbody directly. - Change
HttpAPIClient.DownloadTranslationto returnio.ReadCloser. - Update the
APIClientandmt.Downloaderinterfaces (andhttpDownloader.Fileimplementation) to useio.ReadCloserfor downloaded content.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
client_download_translation.go |
Adjusts DownloadTranslation return type to io.ReadCloser to expose a closable response body. |
client_download_file.go |
Adjusts DownloadFile return type to io.ReadCloser to expose a closable response body. |
client.go |
Updates APIClient interface method signatures to match the new io.ReadCloser return types. |
api/mt/downloader.go |
Updates the Downloader interface and httpDownloader.File implementation to return io.ReadCloser for MT file downloads. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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 connection leak by changing the return type of download methods from io.Reader to io.ReadCloser. This allows callers to properly close the underlying HTTP response body. The changes are consistent across all modified interfaces and implementations. My review includes several suggestions to improve the documentation for these methods, making it explicit that the caller is responsible for closing the returned io.ReadCloser. This will improve the clarity of the API and help prevent future misuse.
| type Downloader interface { | ||
| File(accountUID AccountUID, fileUID FileUID, | ||
| mtUID MtUID, localeID string) (io.Reader, error) | ||
| mtUID MtUID, localeID string) (io.ReadCloser, error) |
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.
With the return type changed to io.ReadCloser, it's crucial to document that the caller is responsible for closing it. Please add a comment to the method signature to make this contract explicit and prevent resource leaks. For example: // File downloads a file. The caller must close the returned ReadCloser.
| // File downloads file | ||
| func (d httpDownloader) File(accountUID AccountUID, fileUID FileUID, | ||
| mtUID MtUID, localeID string) (io.Reader, error) { | ||
| mtUID MtUID, localeID string) (io.ReadCloser, error) { |
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.
| DownloadFile(projectID string, uri string) (io.ReadCloser, error) | ||
| DownloadTranslation(projectID string, localeID string, request FileDownloadRequest) (io.ReadCloser, error) |
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.
| projectID string, | ||
| uri string, | ||
| ) (io.Reader, error) { | ||
| ) (io.ReadCloser, error) { |
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.
| localeID string, | ||
| request FileDownloadRequest, | ||
| ) (io.Reader, error) { | ||
| ) (io.ReadCloser, error) { |
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.
Hi friends,
All download methods (DownloadFile, DownloadTranslation, and mt.Downloader.File) were returning io.Reader instead of io.ReadCloser, even though the underlying Client.Get() returns io.ReadCloser. This prevented callers from closing the HTTP response body, causing connection leaks.
I've updated the methods and their signatures. SDK clients should now be able to prevent memory leaks 🥳