Skip to content

Commit

Permalink
fix: close HTTP resp body to prevent resource leak (#4857)
Browse files Browse the repository at this point in the history
This PR fixes possible resource leaks by adding a statement that closes
a response body.

From the [documentation](https://pkg.go.dev/net/http):
> The caller must close the response body when finished with it

Also, see this
[article](https://golang50shad.es/index.html#close_http_resp_body) for a
deep dive into the problem.
  • Loading branch information
alexandear committed May 12, 2024
1 parent ba7e915 commit c5204df
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ linters:
enable:
- thelper
- gofumpt
- bodyclose
- tparallel
- unconvert
- unparam
Expand Down
8 changes: 6 additions & 2 deletions internal/pipe/blob/blob_minio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ func TestMain(m *testing.M) {
})
requireNoErr(err)
requireNoErr(pool.Retry(func() error {
_, err := http.Get(fmt.Sprintf("http://localhost:%s/minio/health/ready", resource.GetPort("9000/tcp")))
return err
resp, err := http.Get(fmt.Sprintf("http://localhost:%s/minio/health/ready", resource.GetPort("9000/tcp")))
if err != nil {
return err
}
resp.Body.Close()
return nil
}))
listen = "localhost:" + resource.GetPort("9000/tcp")

Expand Down
1 change: 1 addition & 0 deletions internal/pipe/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (p Pipe) Announce(ctx *context.Context) error {
if err != nil {
return fmt.Errorf("discord: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != 204 && resp.StatusCode != 200 {
return fmt.Errorf("discord: %s", resp.Status)
Expand Down

0 comments on commit c5204df

Please sign in to comment.