From 3ffc76772a5fb5d4ab902a8a9b6dfe300cc31777 Mon Sep 17 00:00:00 2001 From: Murtaza Aliakbar Date: Sun, 20 Oct 2024 06:18:25 +0530 Subject: [PATCH] core/connpool: infinite scrub() loop reads from the chan are immediately written back in to it, and so the outer unconditional for-loop spins forever as the chan never runs out of elements. --- intra/core/connpool.go | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/intra/core/connpool.go b/intra/core/connpool.go index 232b9823..237b4eee 100644 --- a/intra/core/connpool.go +++ b/intra/core/connpool.go @@ -237,25 +237,38 @@ func (c *ConnPool[T]) clean() { } func (c *ConnPool[T]) scrub() { - for { - if c.closed.Load() { - return + if c.closed.Load() { + return + } + + staged := make([]timedconn, 0) + defer func() { + for _, tconn := range staged { + kept := false + select { + case <-c.ctx.Done(): // closed + default: + select { + case c.p <- tconn: // put it back in + kept = true + case <-c.ctx.Done(): // closed + default: // pool full + } + } + if !kept { + CloseConn(tconn.c) + } } + }() + for { select { case tconn := <-c.p: if fresh(tconn.dob) && readable(tconn.c) { - select { - case c.p <- tconn: // update dob only on Put() - case <-c.ctx.Done(): // stop - CloseConn(tconn.c) - return - default: // full - CloseConn(tconn.c) - } + staged = append(staged, tconn) } else { CloseConn(tconn.c) - } + } // next case <-c.ctx.Done(): return default: