From 54dc431afc537bb794233e7dd7eb3018b6cf0ed9 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Mon, 13 Nov 2023 18:33:35 +0800 Subject: [PATCH 1/2] revert header.go --- pkg/protocol/http1/resp/writer.go | 37 ++++--------------------------- 1 file changed, 4 insertions(+), 33 deletions(-) diff --git a/pkg/protocol/http1/resp/writer.go b/pkg/protocol/http1/resp/writer.go index 7b50cd434..745ab4c5c 100644 --- a/pkg/protocol/http1/resp/writer.go +++ b/pkg/protocol/http1/resp/writer.go @@ -17,7 +17,6 @@ package resp import ( - "runtime" "sync" "github.com/cloudwego/hertz/pkg/network" @@ -25,16 +24,6 @@ import ( "github.com/cloudwego/hertz/pkg/protocol/http1/ext" ) -var chunkReaderPool sync.Pool - -func init() { - chunkReaderPool = sync.Pool{ - New: func() interface{} { - return &chunkedBodyWriter{} - }, - } -} - type chunkedBodyWriter struct { sync.Once finalizeErr error @@ -70,14 +59,6 @@ func (c *chunkedBodyWriter) Flush() error { // Warning: do not call this method by yourself, unless you know what you are doing. func (c *chunkedBodyWriter) Finalize() error { c.Do(func() { - // in case no actual data from user - if !c.wroteHeader { - c.r.Header.SetContentLength(-1) - if c.finalizeErr = WriteHeader(&c.r.Header, c.w); c.finalizeErr != nil { - return - } - c.wroteHeader = true - } c.finalizeErr = ext.WriteChunk(c.w, nil, true) if c.finalizeErr != nil { return @@ -87,19 +68,9 @@ func (c *chunkedBodyWriter) Finalize() error { return c.finalizeErr } -func (c *chunkedBodyWriter) release() { - c.r = nil - c.w = nil - c.finalizeErr = nil - c.wroteHeader = false - chunkReaderPool.Put(c) -} - func NewChunkedBodyWriter(r *protocol.Response, w network.Writer) network.ExtWriter { - extWriter := chunkReaderPool.Get().(*chunkedBodyWriter) - extWriter.r = r - extWriter.w = w - extWriter.Once = sync.Once{} - runtime.SetFinalizer(extWriter, (*chunkedBodyWriter).release) - return extWriter + return &chunkedBodyWriter{ + r: r, + w: w, + } } From 240dfd532587fca7dde306d6d477f6a48bfc17a7 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Tue, 14 Nov 2023 14:15:33 +0800 Subject: [PATCH 2/2] update header.go --- pkg/protocol/http1/resp/writer.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/protocol/http1/resp/writer.go b/pkg/protocol/http1/resp/writer.go index 745ab4c5c..c7c3d8fa2 100644 --- a/pkg/protocol/http1/resp/writer.go +++ b/pkg/protocol/http1/resp/writer.go @@ -59,6 +59,14 @@ func (c *chunkedBodyWriter) Flush() error { // Warning: do not call this method by yourself, unless you know what you are doing. func (c *chunkedBodyWriter) Finalize() error { c.Do(func() { + // in case no actual data from user + if !c.wroteHeader { + c.r.Header.SetContentLength(-1) + if c.finalizeErr = WriteHeader(&c.r.Header, c.w); c.finalizeErr != nil { + return + } + c.wroteHeader = true + } c.finalizeErr = ext.WriteChunk(c.w, nil, true) if c.finalizeErr != nil { return @@ -70,7 +78,8 @@ func (c *chunkedBodyWriter) Finalize() error { func NewChunkedBodyWriter(r *protocol.Response, w network.Writer) network.ExtWriter { return &chunkedBodyWriter{ - r: r, - w: w, + r: r, + w: w, + Once: sync.Once{}, } }