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

Conversation

MrCreosote
Copy link
Member

No description provided.

Copy link

codecov bot commented Aug 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.83%. Comparing base (f7af00b) to head (f28b856).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #154   +/-   ##
========================================
  Coverage    90.83%   90.83%           
========================================
  Files           14       14           
  Lines         2073     2074    +1     
========================================
+ Hits          1883     1884    +1     
  Misses         157      157           
  Partials        33       33           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@bio-boris bio-boris left a comment

Choose a reason for hiding this comment

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

LGTM

This simply defers the closing of the response body until the surrounding function returns. If there is unread data left in the body, it will not be read or discarded, which may prevent connection reuse in HTTP/1.1.

@@ -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!

@MrCreosote MrCreosote merged commit dd1ad42 into develop Aug 3, 2024
12 checks passed
@MrCreosote MrCreosote deleted the dev-exhaust_reader branch August 5, 2024 20:34
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.

2 participants