-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Adding sync.Pool to Decompress middleware #1699
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
Merged
lammel
merged 2 commits into
labstack:master
from
pafuent:improve_decompress_middleware
Dec 11, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,53 +3,115 @@ package middleware | |
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"github.com/labstack/echo/v4" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type ( | ||
// DecompressConfig defines the config for Decompress middleware. | ||
DecompressConfig struct { | ||
// Skipper defines a function to skip middleware. | ||
Skipper Skipper | ||
|
||
// GzipDecompressPool defines an interface to provide the sync.Pool used to create/store Gzip readers | ||
GzipDecompressPool Decompressor | ||
} | ||
) | ||
|
||
//GZIPEncoding content-encoding header if set to "gzip", decompress body contents. | ||
const GZIPEncoding string = "gzip" | ||
|
||
// Decompressor is used to get the sync.Pool used by the middleware to get Gzip readers | ||
type Decompressor interface { | ||
gzipDecompressPool() sync.Pool | ||
} | ||
|
||
var ( | ||
//DefaultDecompressConfig defines the config for decompress middleware | ||
DefaultDecompressConfig = DecompressConfig{Skipper: DefaultSkipper} | ||
DefaultDecompressConfig = DecompressConfig{ | ||
Skipper: DefaultSkipper, | ||
GzipDecompressPool: &DefaultGzipDecompressPool{}, | ||
} | ||
) | ||
|
||
// DefaultGzipDecompressPool is the default implementation of Decompressor interface | ||
type DefaultGzipDecompressPool struct { | ||
} | ||
|
||
func (d *DefaultGzipDecompressPool) gzipDecompressPool() sync.Pool { | ||
return sync.Pool{ | ||
New: func() interface{} { | ||
// create with an empty reader (but with GZIP header) | ||
w, err := gzip.NewWriterLevel(ioutil.Discard, gzip.BestSpeed) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b := new(bytes.Buffer) | ||
w.Reset(b) | ||
w.Flush() | ||
w.Close() | ||
|
||
r, err := gzip.NewReader(bytes.NewReader(b.Bytes())) | ||
if err != nil { | ||
return err | ||
} | ||
return r | ||
}, | ||
} | ||
} | ||
|
||
//Decompress decompresses request body based if content encoding type is set to "gzip" with default config | ||
func Decompress() echo.MiddlewareFunc { | ||
return DecompressWithConfig(DefaultDecompressConfig) | ||
} | ||
|
||
//DecompressWithConfig decompresses request body based if content encoding type is set to "gzip" with config | ||
func DecompressWithConfig(config DecompressConfig) echo.MiddlewareFunc { | ||
// Defaults | ||
if config.Skipper == nil { | ||
config.Skipper = DefaultGzipConfig.Skipper | ||
} | ||
if config.GzipDecompressPool == nil { | ||
config.GzipDecompressPool = DefaultDecompressConfig.GzipDecompressPool | ||
} | ||
|
||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
pool := config.GzipDecompressPool.gzipDecompressPool() | ||
return func(c echo.Context) error { | ||
if config.Skipper(c) { | ||
return next(c) | ||
} | ||
switch c.Request().Header.Get(echo.HeaderContentEncoding) { | ||
case GZIPEncoding: | ||
gr, err := gzip.NewReader(c.Request().Body) | ||
if err != nil { | ||
b := c.Request().Body | ||
|
||
i := pool.Get() | ||
gr, ok := i.(*gzip.Reader) | ||
if !ok { | ||
return echo.NewHTTPError(http.StatusInternalServerError, i.(error).Error()) | ||
} | ||
|
||
if err := gr.Reset(b); err != nil { | ||
pool.Put(gr) | ||
if err == io.EOF { //ignore if body is empty | ||
return next(c) | ||
} | ||
return err | ||
} | ||
defer gr.Close() | ||
var buf bytes.Buffer | ||
io.Copy(&buf, gr) | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. good catch! |
||
|
||
r := ioutil.NopCloser(&buf) | ||
defer r.Close() | ||
c.Request().Body = r | ||
} | ||
return next(c) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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