From 5b0790c57aeb045a80076b33817b5c8284ef6d8d Mon Sep 17 00:00:00 2001 From: Rennbon <343688972@qq.com> Date: Tue, 15 Feb 2022 16:25:09 +0800 Subject: [PATCH 1/8] feature: add request and response body size limit, it prevents the large body from slowly stretching the memory of the entire service --- http.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/http.go b/http.go index d61eb622be..0f0be11686 100644 --- a/http.go +++ b/http.go @@ -17,6 +17,18 @@ import ( "github.com/valyala/bytebufferpool" ) +var ( + requestBodyMaxLimit = 0 + responseBodyMaxLimit = 0 +) + +// 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) { + requestBodyMaxLimit = reqBodyMaxLimit + responseBodyMaxLimit = respBodyMaxLimit +} + // Request represents HTTP request. // // It is forbidden copying Request instances. Create new instances @@ -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 { + req.ReleaseBody(requestBodyMaxLimit) + } req.Header.Reset() req.resetSkipHeader() req.timeout = 0 @@ -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 @@ -994,6 +1012,8 @@ func (resp *Response) Reset() { resp.ImmediateHeaderFlush = false } + + func (resp *Response) resetSkipHeader() { resp.ResetBody() } From a802e7e53ce3081347744b9845b29cccac62a4ae Mon Sep 17 00:00:00 2001 From: Rennbon <343688972@qq.com> Date: Tue, 15 Feb 2022 18:37:43 +0800 Subject: [PATCH 2/8] fix: http.go fmt --- http.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/http.go b/http.go index 0f0be11686..f9ea386c42 100644 --- a/http.go +++ b/http.go @@ -1012,8 +1012,6 @@ func (resp *Response) Reset() { resp.ImmediateHeaderFlush = false } - - func (resp *Response) resetSkipHeader() { resp.ResetBody() } From 2ec424d6732a60d84e409e5bd279f9c996b44292 Mon Sep 17 00:00:00 2001 From: Rennbon <343688972@qq.com> Date: Fri, 25 Feb 2022 11:17:45 +0800 Subject: [PATCH 3/8] refact: optimize code naming --- http.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/http.go b/http.go index f9ea386c42..a300b5e2b8 100644 --- a/http.go +++ b/http.go @@ -18,13 +18,13 @@ import ( ) var ( - requestBodyMaxLimit = 0 - responseBodyMaxLimit = 0 + requestBodyMaxLimit = -1 + responseBodyMaxLimit = -1 ) -// SetBodyLimit set the max body limit in request and response +// SetBodySizePoolLimit 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) { +func SetBodySizePoolLimit(reqBodyMaxLimit, respBodyMaxLimit int) { requestBodyMaxLimit = reqBodyMaxLimit responseBodyMaxLimit = respBodyMaxLimit } @@ -969,7 +969,7 @@ func readMultipartForm(r io.Reader, boundary string, size, maxInMemoryFileSize i // Reset clears request contents. func (req *Request) Reset() { - if requestBodyMaxLimit > 0 && req.body != nil { + if requestBodyMaxLimit >= 0 && req.body != nil { req.ReleaseBody(requestBodyMaxLimit) } req.Header.Reset() @@ -1001,7 +1001,7 @@ func (req *Request) RemoveMultipartFormFiles() { // Reset clears response contents. func (resp *Response) Reset() { - if responseBodyMaxLimit > 0 && resp.body != nil { + if responseBodyMaxLimit >= 0 && resp.body != nil { resp.ReleaseBody(responseBodyMaxLimit) } resp.Header.Reset() From d9c8598f9bf6c6a30a1b61dbec724aa6f484839c Mon Sep 17 00:00:00 2001 From: Rennbon <343688972@qq.com> Date: Tue, 1 Mar 2022 18:25:56 +0800 Subject: [PATCH 4/8] Update http.go Co-authored-by: Erik Dubbelboer --- http.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/http.go b/http.go index a300b5e2b8..a18044c3b5 100644 --- a/http.go +++ b/http.go @@ -24,9 +24,9 @@ var ( // SetBodySizePoolLimit set the max body limit in request and response // if the body size larger than the limit,it will be released -func SetBodySizePoolLimit(reqBodyMaxLimit, respBodyMaxLimit int) { - requestBodyMaxLimit = reqBodyMaxLimit - responseBodyMaxLimit = respBodyMaxLimit +func SetBodySizePoolLimit(reqBodyLimit, respBodyLimit int) { + requestBodyPoolSizeLimit = reqBodyLimit + responseBodyPoolSizeLimit = respBodyLimit } // Request represents HTTP request. From d8ef6222f5d78e6faf4bb36f3cdb6834f111c94d Mon Sep 17 00:00:00 2001 From: Rennbon <343688972@qq.com> Date: Tue, 1 Mar 2022 18:26:02 +0800 Subject: [PATCH 5/8] Update http.go Co-authored-by: Erik Dubbelboer --- http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index a18044c3b5..157bacf96a 100644 --- a/http.go +++ b/http.go @@ -18,8 +18,8 @@ import ( ) var ( - requestBodyMaxLimit = -1 - responseBodyMaxLimit = -1 + requestBodyPoolSizeLimit = -1 + responseBodyPoolSizeLimit = -1 ) // SetBodySizePoolLimit set the max body limit in request and response From 31e16a406212a6d42dff4322c827e5b9d7e7bbaf Mon Sep 17 00:00:00 2001 From: Rennbon <343688972@qq.com> Date: Tue, 1 Mar 2022 18:26:09 +0800 Subject: [PATCH 6/8] Update http.go Co-authored-by: Erik Dubbelboer --- http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index 157bacf96a..590afb003d 100644 --- a/http.go +++ b/http.go @@ -969,8 +969,8 @@ func readMultipartForm(r io.Reader, boundary string, size, maxInMemoryFileSize i // Reset clears request contents. func (req *Request) Reset() { - if requestBodyMaxLimit >= 0 && req.body != nil { - req.ReleaseBody(requestBodyMaxLimit) + if requestBodyPoolSizeLimit >= 0 && req.body != nil { + req.ReleaseBody(requestBodyPoolSizeLimit) } req.Header.Reset() req.resetSkipHeader() From fc15912511897213c14dc36164fce24590cfd4f1 Mon Sep 17 00:00:00 2001 From: Rennbon <343688972@qq.com> Date: Tue, 1 Mar 2022 18:26:14 +0800 Subject: [PATCH 7/8] Update http.go Co-authored-by: Erik Dubbelboer --- http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index 590afb003d..b39273d5dd 100644 --- a/http.go +++ b/http.go @@ -1001,8 +1001,8 @@ func (req *Request) RemoveMultipartFormFiles() { // Reset clears response contents. func (resp *Response) Reset() { - if responseBodyMaxLimit >= 0 && resp.body != nil { - resp.ReleaseBody(responseBodyMaxLimit) + if responseBodyPoolSizeLimit >= 0 && resp.body != nil { + resp.ReleaseBody(responseBodyPoolSizeLimit) } resp.Header.Reset() resp.resetSkipHeader() From d87b7fd91697aa94b09b0cdc0f14833d3b118c88 Mon Sep 17 00:00:00 2001 From: Rennbon <343688972@qq.com> Date: Tue, 1 Mar 2022 18:26:55 +0800 Subject: [PATCH 8/8] Update http.go Co-authored-by: Erik Dubbelboer --- http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index b39273d5dd..47431cdcd8 100644 --- a/http.go +++ b/http.go @@ -22,8 +22,8 @@ var ( responseBodyPoolSizeLimit = -1 ) -// SetBodySizePoolLimit set the max body limit in request and response -// if the body size larger than the limit,it will be released +// 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