From 00d38a68a8917d3e6ccfb53d9fa08f4d7cca6041 Mon Sep 17 00:00:00 2001 From: Anthony Regeda Date: Sun, 9 Sep 2018 23:22:19 +0300 Subject: [PATCH] pool-queue-vs-stack pool should work like a queue to traverse all possible connections --- conn_pool.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/conn_pool.go b/conn_pool.go index b97ccb28..27ca3531 100644 --- a/conn_pool.go +++ b/conn_pool.go @@ -116,10 +116,14 @@ func (p *ConnPool) acquire(deadline *time.Time) (*Conn, error) { } // A connection is available - if len(p.availableConnections) > 0 { - c := p.availableConnections[len(p.availableConnections)-1] + // The pool works like a queue. Available connection will be returned + // from the head. A new connection will be added to the tail. + numAvailable := len(p.availableConnections) + if numAvailable > 0 { + c := p.availableConnections[0] c.poolResetCount = p.resetCount - p.availableConnections = p.availableConnections[:len(p.availableConnections)-1] + copy(p.availableConnections, p.availableConnections[1:]) + p.availableConnections = p.availableConnections[:numAvailable-1] return c, nil }