Skip to content

Response: Remove odd response body discard reader#563

Open
roosterfish wants to merge 1 commit intocanonical:v3from
roosterfish:fix_response_parser
Open

Response: Remove odd response body discard reader#563
roosterfish wants to merge 1 commit intocanonical:v3from
roosterfish:fix_response_parser

Conversation

@roosterfish
Copy link
Contributor

The ParseResponse function always reads from the response which invalidates the purpose of the body discard reader.
It's good Go practice to always read the response and then close the body but not anymore required in this case.

As a neat side effect we remove the logging statement from the client func which is odd anyway as such an error should rather be returned directly and not logged as this func is not only used as part of the daemon (which has an initialized logger) but can also be called by clients directly.

Copy link

Copilot AI left a 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 simplifies the ParseResponse function by removing unnecessary response body draining logic and associated error logging. Since all HTTP clients in the codebase have DisableKeepAlives: true (preventing connection reuse), draining the response body before closing is redundant. The json.NewDecoder already reads the necessary data, and the defer resp.Body.Close() call (now moved to the beginning) ensures proper cleanup.

Key changes:

  • Moved defer resp.Body.Close() to the beginning of ParseResponse function for earlier resource cleanup
  • Removed redundant io.Copy(io.Discard, resp.Body) call since connection reuse is disabled
  • Removed inappropriate error logging from a library function (logging should be handled by callers)
  • Cleaned up unused imports (io, log/slog, and internal log package)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@roosterfish roosterfish marked this pull request as ready for review December 9, 2025 13:38
The ParseResponse function always reads from the response which invalidates the purpose of the body discard reader.

Signed-off-by: Julian Pelizäus <julian.pelizaeus@canonical.com>
Copy link
Contributor

@tugbataluy tugbataluy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. As fat as I understand, this is a focused cleanup that eliminates unnecessary logic, improves architectural separation, and ensures errors are handled properly at the caller level rather than being logged internally.

@roosterfish
Copy link
Contributor Author

roosterfish commented Dec 15, 2025

I'll leave it open as I want to verify it further. Also I want to investigate why this was added in the first place.
As it's not causing any issues rn there is no pressure right now to get it merged. Just something I found along the way.

Copy link
Contributor

@markylaing markylaing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to double check where this came from though I suspect it was just copied from LXD or elsewhere.

Good to get rid of this cruft though, any errors that result from reading the response body would be caught during the JSON decoding.

Comment on lines 175 to 178
if err != nil {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Failed to fetch %q: %q", resp.Request.URL.String(), resp.Status)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also remove this? Or at least allow a range of response codes 200-299? It seems weird that if someone adds a microcluster endpoint like an operation returning a 202 then this function will behave differently in the error scenario

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants