From 9e110fc07d70e3be0cd13ea27cee896d9a4dd3e3 Mon Sep 17 00:00:00 2001 From: Robsdedude Date: Wed, 13 Dec 2023 17:05:27 +0100 Subject: [PATCH] Pool: notify fewer waiters on returned connection --- neo4j/internal/pool/pool.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/neo4j/internal/pool/pool.go b/neo4j/internal/pool/pool.go index f7f8757d..c1c2efca 100644 --- a/neo4j/internal/pool/pool.go +++ b/neo4j/internal/pool/pool.go @@ -272,6 +272,17 @@ func (p *Pool) Borrow(ctx context.Context, getServerNames func() []string, wait case <-ctx.Done(): p.queueMut.Lock() p.queue.Remove(e) + if len(q.wakeup) == 1 { + // We got notified, but are no longer interested. + // Ask the next waiter. + if e := p.queue.Front(); e != nil { + queuedRequest := e.Value.(*qitem) + p.queue.Remove(e) + queuedRequest.wakeup <- true + } + p.queueMut.Unlock() + continue + } p.queueMut.Unlock() p.log.Warnf(log.Pool, p.logId, "Borrow time-out") return nil, &errorutil.PoolTimeout{Err: ctx.Err(), Servers: serverNames} @@ -436,7 +447,8 @@ func (p *Pool) Return(ctx context.Context, c idb.Connection) { // Check if there is anyone in the queue waiting for a connection to this server. p.queueMut.Lock() - for e := p.queue.Front(); e != nil; e = e.Next() { + + if e := p.queue.Front(); e != nil { queuedRequest := e.Value.(*qitem) p.queue.Remove(e) queuedRequest.wakeup <- true