From c8559c448537969376623be9d352949b59907b0e Mon Sep 17 00:00:00 2001 From: Harish Shan <140232061+perhapsmaple@users.noreply.github.com> Date: Tue, 17 Oct 2023 02:12:01 +0530 Subject: [PATCH] caddyhttp: Use sync.Pool to reduce lengthReader allocations (#5848) * Use sync.Pool to reduce lengthReader allocations Signed-off-by: Harish Shan <140232061+perhapsmaple@users.noreply.github.com> * Add defer putLengthReader to prevent leak Signed-off-by: Harish Shan <140232061+perhapsmaple@users.noreply.github.com> * Cleanup in putLengthReader Co-authored-by: Francis Lavoie --------- Signed-off-by: Harish Shan <140232061+perhapsmaple@users.noreply.github.com> Co-authored-by: Francis Lavoie --- modules/caddyhttp/server.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index d060738f..b9861918 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -318,7 +318,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { // so we can track the number of bytes read from it var bodyReader *lengthReader if r.Body != nil { - bodyReader = &lengthReader{Source: r.Body} + bodyReader = getLengthReader(r.Body) + defer putLengthReader(bodyReader) r.Body = bodyReader } @@ -902,6 +903,24 @@ type lengthReader struct { Length int } +var lengthReaderPool = sync.Pool{ + New: func() interface{} { + return &lengthReader{} + }, +} + +func getLengthReader(source io.ReadCloser) *lengthReader { + reader := lengthReaderPool.Get().(*lengthReader) + reader.Source = source + return reader +} + +func putLengthReader(reader *lengthReader) { + reader.Source = nil + reader.Length = 0 + lengthReaderPool.Put(reader) +} + func (r *lengthReader) Read(b []byte) (int, error) { n, err := r.Source.Read(b) r.Length += n