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 2 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 (
requestBodyMaxLimit = 0
responseBodyMaxLimit = 0
Rennbon marked this conversation as resolved.
Show resolved Hide resolved
)

// SetBodyLimit set the max body limit in request and response
// if the body size larger than the limit,it will be released
func SetBodyLimit(reqBodyMaxLimit, respBodyMaxLimit int) {
Rennbon marked this conversation as resolved.
Show resolved Hide resolved
requestBodyMaxLimit = reqBodyMaxLimit
responseBodyMaxLimit = respBodyMaxLimit
}

// 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 requestBodyMaxLimit > 0 && req.body != nil {
Rennbon marked this conversation as resolved.
Show resolved Hide resolved
req.ReleaseBody(requestBodyMaxLimit)
}
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 responseBodyMaxLimit > 0 && resp.body != nil {
resp.ReleaseBody(responseBodyMaxLimit)
}
resp.Header.Reset()
resp.resetSkipHeader()
resp.SkipBody = false
Expand Down