diff --git a/mempool/mempool.go b/mempool/mempool.go index 8b865062..4100c658 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -17,8 +17,6 @@ type MemPool struct { } // New . -// -//go:norace func New(bufSize, freeSize int) Allocator { if bufSize <= 0 { bufSize = 64 @@ -45,8 +43,6 @@ func New(bufSize, freeSize int) Allocator { } // Malloc . -// -//go:norace func (mp *MemPool) Malloc(size int) []byte { var ret []byte if size > mp.freeSize { @@ -65,8 +61,6 @@ func (mp *MemPool) Malloc(size int) []byte { } // Realloc . -// -//go:norace func (mp *MemPool) Realloc(buf []byte, size int) []byte { if size <= cap(buf) { return buf[:size] @@ -87,26 +81,22 @@ func (mp *MemPool) Realloc(buf []byte, size int) []byte { } // Append . -// -//go:norace func (mp *MemPool) Append(buf []byte, more ...byte) []byte { return append(buf, more...) } // AppendString . -// -//go:norace func (mp *MemPool) AppendString(buf []byte, more string) []byte { return append(buf, more...) } // Free . -// -//go:norace func (mp *MemPool) Free(buf []byte) { - mp.incrFree(buf) - if cap(buf) > mp.freeSize { - return + if buf != nil && cap(buf) > 0 { + mp.incrFree(buf) + if cap(buf) > mp.freeSize { + return + } + mp.pool.Put(&buf) } - mp.pool.Put(&buf) } diff --git a/nbhttp/body.go b/nbhttp/body.go index 72c7e615..c409dc4b 100644 --- a/nbhttp/body.go +++ b/nbhttp/body.go @@ -24,6 +24,7 @@ type BodyReader struct { left int // num of byte left buffers [][]byte // buffers that storage HTTP body engine *Engine // allocator that manages buffers + closed bool } // Read reads body bytes to p, returns the num of bytes read and error. @@ -56,13 +57,17 @@ func (br *BodyReader) Read(p []byte) (int, error) { // //go:norace func (br *BodyReader) Close() error { + if br.closed { + return nil + } + br.closed = true if br.buffers != nil { for _, b := range br.buffers { br.engine.BodyAllocator.Free(b) } } - *br = emptyBodyReader - bodyReaderPool.Put(br) + // *br = emptyBodyReader + // bodyReaderPool.Put(br) return nil } diff --git a/nbhttp/body_test.go b/nbhttp/body_test.go index 7d4ae59c..90d3ae45 100644 --- a/nbhttp/body_test.go +++ b/nbhttp/body_test.go @@ -60,6 +60,7 @@ func TestBodyReader(t *testing.T) { if !bytes.Equal(allBytes, body1) { t.Fatalf("!bytes.Equal(allBytes, body1)") } + br1.Close() br2 := newBR() body2 := make([]byte, len(allBytes)) @@ -72,4 +73,5 @@ func TestBodyReader(t *testing.T) { if !bytes.Equal(allBytes, body2) { t.Fatalf("!bytes.Equal(allBytes, body2)") } + br2.Close() } diff --git a/nbhttp/processor.go b/nbhttp/processor.go index f3064184..a4b19a8a 100644 --- a/nbhttp/processor.go +++ b/nbhttp/processor.go @@ -49,6 +49,8 @@ func releaseRequest(req *http.Request, retainHTTPBody bool) { // do not release the body } else { br.Close() + *br = emptyBodyReader + bodyReaderPool.Put(br) } } else if !retainHTTPBody { req.Body.Close() @@ -74,6 +76,8 @@ func releaseClientResponse(res *http.Response) { if res.Body != nil { br := res.Body.(*BodyReader) br.Close() + *br = emptyBodyReader + bodyReaderPool.Put(br) } *res = emptyClientResponse clientResponsePool.Put(res)