Fix ineffective recycling (#18952)

Recycle would always be called on the dummy value `any(newRT())` instead of the actual value given to the recycle function.

Caught by race tests, but mostly harmless, except for reduced perf.

Other minor cleanups. Introduced in #18940 (unreleased)
This commit is contained in:
Klaus Post 2024-02-02 08:48:12 -08:00 committed by GitHub
parent d99d16e8c3
commit ce0cb913bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 8 deletions

View File

@ -348,9 +348,14 @@ type SingleHandler[Req, Resp RoundTripper] struct {
func recycleFunc[RT RoundTripper](newRT func() RT) (newFn func() RT, recycle func(r RT)) { func recycleFunc[RT RoundTripper](newRT func() RT) (newFn func() RT, recycle func(r RT)) {
rAny := any(newRT()) rAny := any(newRT())
if rc, ok := rAny.(Recycler); ok { var rZero RT
if _, ok := rAny.(Recycler); ok {
return newRT, func(r RT) { return newRT, func(r RT) {
rc.Recycle() if r != rZero {
if rc, ok := any(r).(Recycler); ok {
rc.Recycle()
}
}
} }
} }
pool := sync.Pool{ pool := sync.Pool{
@ -358,7 +363,6 @@ func recycleFunc[RT RoundTripper](newRT func() RT) (newFn func() RT, recycle fun
return newRT() return newRT()
}, },
} }
var rZero RT
return func() RT { return pool.Get().(RT) }, return func() RT { return pool.Get().(RT) },
func(r RT) { func(r RT) {
if r != rZero { if r != rZero {
@ -436,6 +440,7 @@ func (h *SingleHandler[Req, Resp]) Register(m *Manager, handle func(req Req) (re
} }
resp, rerr := handle(req) resp, rerr := handle(req)
h.recycleReq(req) h.recycleReq(req)
if rerr != nil { if rerr != nil {
PutByteBuffer(payload) PutByteBuffer(payload)
return nil, rerr return nil, rerr
@ -474,22 +479,20 @@ func (h *SingleHandler[Req, Resp]) Call(ctx context.Context, c Requester, req Re
ctx = context.WithValue(ctx, TraceParamsKey{}, fmt.Sprintf("type=%T", req)) ctx = context.WithValue(ctx, TraceParamsKey{}, fmt.Sprintf("type=%T", req))
} }
if h.callReuseReq { if h.callReuseReq {
defer func() { defer h.recycleReq(req)
h.recycleReq(req)
}()
} }
res, err := c.Request(ctx, h.id, payload) res, err := c.Request(ctx, h.id, payload)
PutByteBuffer(payload) PutByteBuffer(payload)
if err != nil { if err != nil {
return resp, err return resp, err
} }
defer PutByteBuffer(res)
r := h.NewResponse() r := h.NewResponse()
_, err = r.UnmarshalMsg(res) _, err = r.UnmarshalMsg(res)
if err != nil { if err != nil {
h.PutResponse(r) h.PutResponse(r)
return resp, err return resp, err
} }
PutByteBuffer(res)
return r, err return r, err
} }

View File

@ -235,7 +235,6 @@ func (b *Bytes) UnmarshalMsg(bytes []byte) ([]byte, error) {
copy(*b, val) copy(*b, val)
} else { } else {
if cap(*b) == 0 && len(val) <= maxBufferSize { if cap(*b) == 0 && len(val) <= maxBufferSize {
PutByteBuffer(*b)
*b = GetByteBuffer()[:0] *b = GetByteBuffer()[:0]
} else { } else {
PutByteBuffer(*b) PutByteBuffer(*b)