Skip to content
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

Exhaust response body on file store #154

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 0.1.5

* Fixed a bug where the response input stream from S3 may not be fully exhausted when
storing a file.
* Upgraded from go 1.12 to 1.16, which is now required
* Changed build base image from FROM `golang:1.12.5-alpine3.9` to `golang:1.16.15-alpine3.15`

Expand Down
1 change: 1 addition & 0 deletions filestore/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (fs *S3FileStore) StoreFile(le *logrus.Entry, p *StoreFileParams) (out *Fil
return nil, errors.New("s3 store request: " + errstr)
}
defer resp.Body.Close()
defer io.Copy(io.Discard, resp.Body)
Copy link
Contributor

Choose a reason for hiding this comment

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

The order in which you defer the functions in Go is critical because defer statements are executed in last-in, first-out order when the function returns.

Given the code:

go

defer resp.Body.Close()
defer io.Copy(io.Discard, resp.Body)

These statements will be executed in reverse order when the function returns, meaning io.Copy(io.Discard, resp.Body) will execute before resp.Body.Close().

Very cool!

if resp.StatusCode > 399 { // don't worry about 100s, shouldn't happen
buffer := make([]byte, 1000)
n, err := resp.Body.Read(buffer)
Expand Down
Loading