-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Adding sync.Pool to Decompress middleware #1699
Adding sync.Pool to Decompress middleware #1699
Conversation
Fixing a http.Request.Body leak on the decompress middleware that were not properly Close Removing the defer on the call to gzip.Reader, because that reader is already exausted after the call to io.Copy
Codecov Report
@@ Coverage Diff @@
## master #1699 +/- ##
==========================================
- Coverage 84.88% 84.86% -0.02%
==========================================
Files 29 29
Lines 1945 1969 +24
==========================================
+ Hits 1651 1671 +20
- Misses 187 189 +2
- Partials 107 109 +2
Continue to review full report at Codecov.
|
@arun0009 Could you please review this PR? |
Codecov is failing because I'm no testing some error code branches, that sadly I don't know how to test. If someone have any suggestion, I'll be glad to implement it. Here are the benchmarks comparison for this change. In this case I did 10 iterations instead of the 5 performed on our GitHub action
|
gr.Close() | ||
pool.Put(gr) | ||
|
||
b.Close() // http.Request.Body is closed by the Server, but because we are replacing it, it must be closed here |
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.
good catch!
One way of testing InternalServerError or pool error handling is by using interfaces. Here's an example that would cover 91.9% of code base https://gist.github.com/arun0009/f8a19659cf956b82e3abadc2211e3be0 |
Thanks for the advice @arun0009 |
var buf bytes.Buffer | ||
io.Copy(&buf, gr) | ||
|
||
gr.Close() | ||
pool.Put(gr) |
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.
@pafuent - you are putting gr back to pool twice? On line 100 and 110? Also, whats resetting gr before putting it back in pool? of is it the usage of gr.Reset
making sure that it's reset before reading new request body?
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.
Line 100 because the call to Reset returned an error, so the middleware is skipped (for empty bodies) or returns the error (all the rest of the errors). And the line 110 is the happy path 😉
I'm not using a defer just to return it to the Pool as fast as is possible to avoid an allocation if a new request find the Pool empty.
Reset() requires the io.Reader, so I need to perform that after getting one instance from the Pool
Looks like you also added the missing test for the error path. |
@lammel, yes it's ready to be merged |
Fixing a http.Request.Body leak on the decompress middleware that were not properly Close
Removing the defer on the call to gzip.Reader, because that reader is already exhausted after the call to io.Copy