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

feature: Keep the memory usage of the service at a stable level #1216

Merged
merged 8 commits into from Mar 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions http.go
Expand Up @@ -17,6 +17,18 @@ import (
"github.com/valyala/bytebufferpool"
)

var (
requestBodyPoolSizeLimit = -1
responseBodyPoolSizeLimit = -1
)

// SetBodySizePoolLimit set the max body size for bodies to be returned to the pool.
// If the body size is larger it will be released instead of put back into the pool for reuse.
func SetBodySizePoolLimit(reqBodyLimit, respBodyLimit int) {
requestBodyPoolSizeLimit = reqBodyLimit
responseBodyPoolSizeLimit = respBodyLimit
}

// Request represents HTTP request.
//
// It is forbidden copying Request instances. Create new instances
Expand Down Expand Up @@ -957,6 +969,9 @@ func readMultipartForm(r io.Reader, boundary string, size, maxInMemoryFileSize i

// Reset clears request contents.
func (req *Request) Reset() {
if requestBodyPoolSizeLimit >= 0 && req.body != nil {
req.ReleaseBody(requestBodyPoolSizeLimit)
}
req.Header.Reset()
req.resetSkipHeader()
req.timeout = 0
Expand Down Expand Up @@ -986,6 +1001,9 @@ func (req *Request) RemoveMultipartFormFiles() {

// Reset clears response contents.
func (resp *Response) Reset() {
if responseBodyPoolSizeLimit >= 0 && resp.body != nil {
resp.ReleaseBody(responseBodyPoolSizeLimit)
}
resp.Header.Reset()
resp.resetSkipHeader()
resp.SkipBody = false
Expand Down