From 092a0a4e6f480ba357dfb74d263f8d0842d39af5 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 13 Feb 2025 11:58:27 +0100 Subject: [PATCH] review feedback; improve docs Signed-off-by: Vicent Marti --- go/pools/smartconnpool/pool.go | 2 +- go/pools/smartconnpool/stack.go | 25 +++---------------------- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/go/pools/smartconnpool/pool.go b/go/pools/smartconnpool/pool.go index c07da8b85a9..87a288bec3d 100644 --- a/go/pools/smartconnpool/pool.go +++ b/go/pools/smartconnpool/pool.go @@ -200,7 +200,7 @@ func (pool *ConnPool[C]) open() { // The expire worker takes care of removing from the waiter list any clients whose // context has been cancelled. - pool.runWorker(pool.close, 100*time.Millisecond, func(now time.Time) bool { + pool.runWorker(pool.close, 100*time.Millisecond, func(_ time.Time) bool { maybeStarving := pool.wait.expire(false) // Do not allow connections to starve; if there's waiters in the queue diff --git a/go/pools/smartconnpool/stack.go b/go/pools/smartconnpool/stack.go index cdf232b11e2..8d656ee4e8d 100644 --- a/go/pools/smartconnpool/stack.go +++ b/go/pools/smartconnpool/stack.go @@ -25,6 +25,9 @@ import ( // connStack is a lock-free stack for Connection objects. It is safe to // use from several goroutines. type connStack[C Connection] struct { + // top is a pointer to the top node on the stack and to an increasing + // counter of pop operations, to prevent A-B-A races. + // See: https://en.wikipedia.org/wiki/ABA_problem top atomic2.PointerAndUint64[Pooled[C]] } @@ -58,25 +61,3 @@ func (s *connStack[C]) Peek() *Pooled[C] { top, _ := s.top.Load() return top } - -func (s *connStack[C]) PopAll(out []*Pooled[C]) []*Pooled[C] { - var oldHead *Pooled[C] - - for { - var popCount uint64 - oldHead, popCount = s.top.Load() - if oldHead == nil { - return out - } - if s.top.CompareAndSwap(oldHead, popCount, nil, popCount+1) { - break - } - runtime.Gosched() - } - - for oldHead != nil { - out = append(out, oldHead) - oldHead = oldHead.next.Load() - } - return out -}