From 2a112b53a579502d8d56872019b8fc47780c176e Mon Sep 17 00:00:00 2001 From: Trim21 Date: Sat, 20 Aug 2022 15:48:51 +0800 Subject: [PATCH 1/3] fix --- client.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 97762dcdaf..a9f4b7bf2b 100644 --- a/client.go +++ b/client.go @@ -856,8 +856,12 @@ func (a *Agent) reset() { } var ( - clientPool sync.Pool - agentPool sync.Pool + clientPool sync.Pool + agentPool = sync.Pool{ + New: func() any { + return &Agent{req: &Request{}} + }, + } responsePool sync.Pool argsPool sync.Pool formFilePool sync.Pool @@ -895,11 +899,7 @@ func ReleaseClient(c *Client) { // no longer needed. This allows Agent recycling, reduces GC pressure // and usually improves performance. func AcquireAgent() *Agent { - v := agentPool.Get() - if v == nil { - return &Agent{req: &Request{}} - } - return v.(*Agent) + return agentPool.Get().(*Agent) } // ReleaseAgent returns a acquired via AcquireAgent to Agent pool. From 30c40a1da094daf907c886c9f7824a6da96e9d3a Mon Sep 17 00:00:00 2001 From: Trim21 Date: Sat, 20 Aug 2022 16:22:30 +0800 Subject: [PATCH 2/3] fix --- client.go | 17 ++++++++++++++--- client_test.go | 5 +++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index a9f4b7bf2b..deedc0f56c 100644 --- a/client.go +++ b/client.go @@ -741,9 +741,14 @@ func (a *Agent) RetryIf(retryIf RetryIfFunc) *Agent { /************************** End Agent Setting **************************/ // Bytes returns the status code, bytes body and errors of url. +// +// it's not safe to use Agent after calling [Agent.Bytes] func (a *Agent) Bytes() (code int, body []byte, errs []error) { defer a.release() + return a.bytes() +} +func (a *Agent) bytes() (code int, body []byte, errs []error) { if errs = append(errs, a.errs...); len(errs) > 0 { return } @@ -802,16 +807,22 @@ func printDebugInfo(req *Request, resp *Response, w io.Writer) { } // String returns the status code, string body and errors of url. +// +// it's not safe to use Agent after calling [Agent.String] func (a *Agent) String() (int, string, []error) { - code, body, errs := a.Bytes() + defer a.release() + code, body, errs := a.bytes() return code, utils.UnsafeString(body), errs } // Struct returns the status code, bytes body and errors of url. // And bytes body will be unmarshalled to given v. +// +// it's not safe to use Agent after calling [Agent.Struct] func (a *Agent) Struct(v interface{}) (code int, body []byte, errs []error) { - if code, body, errs = a.Bytes(); len(errs) > 0 { + defer a.release() + if code, body, errs = a.bytes(); len(errs) > 0 { return } @@ -858,7 +869,7 @@ func (a *Agent) reset() { var ( clientPool sync.Pool agentPool = sync.Pool{ - New: func() any { + New: func() interface{} { return &Agent{req: &Request{}} }, } diff --git a/client_test.go b/client_test.go index f9de9367de..1a3e4c5b7b 100644 --- a/client_test.go +++ b/client_test.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/tls" "encoding/base64" + "encoding/json" "errors" "fmt" "io" @@ -16,10 +17,10 @@ import ( "testing" "time" - "encoding/json" - "github.com/gofiber/fiber/v2/internal/tlstest" "github.com/gofiber/fiber/v2/utils" + "github.com/gofiber/fiber/v3/internal/tlstest" + "github.com/gofiber/fiber/v3/utils" "github.com/valyala/fasthttp/fasthttputil" ) From 7d77bf59ad9f28219c4dd8d91e563cfb81993140 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Sun, 21 Aug 2022 06:04:01 +0800 Subject: [PATCH 3/3] fix --- client_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/client_test.go b/client_test.go index 1a3e4c5b7b..980ac1f955 100644 --- a/client_test.go +++ b/client_test.go @@ -19,8 +19,6 @@ import ( "github.com/gofiber/fiber/v2/internal/tlstest" "github.com/gofiber/fiber/v2/utils" - "github.com/gofiber/fiber/v3/internal/tlstest" - "github.com/gofiber/fiber/v3/utils" "github.com/valyala/fasthttp/fasthttputil" )