From d7b0e43f72bb4680638606ff2ba46d11f41590cd Mon Sep 17 00:00:00 2001 From: Oleg Jukovec Date: Wed, 17 Aug 2022 10:14:15 +0300 Subject: [PATCH] bugfix: close `UnknownRole` connections from a pool We add all connections into ConnectionPool.anyPool, but `MasterRole` connection only in ConnectionPool.rwPool and `ReplicaRole` connections only in ConnectionPool.roPool. As a result `UnknownRole` connections appears only in the `anyPool`. See `setConnectionToPool` implementation. So we need to close connections from the `anyPool` instead of `roPool` + `rwPool`. Part of #208 --- CHANGELOG.md | 1 + connection_pool/connection_pool.go | 15 +++++++++++---- connection_pool/round_robin.go | 13 ------------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ed7967b3..6f3be2de8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release. - Mode type description in the connection_pool subpackage (#208) - Missed Role type constants in the connection_pool subpackage (#208) +- ConnectionPool does not close UnknownRole connections (#208) ## [1.8.0] - 2022-08-17 diff --git a/connection_pool/connection_pool.go b/connection_pool/connection_pool.go index 28aca1989..e424fe85e 100644 --- a/connection_pool/connection_pool.go +++ b/connection_pool/connection_pool.go @@ -154,12 +154,19 @@ func (connPool *ConnectionPool) Close() []error { close(connPool.control) connPool.state = connClosed - rwErrs := connPool.rwPool.CloseConns() - roErrs := connPool.roPool.CloseConns() + errs := make([]error, 0, len(connPool.addrs)) - allErrs := append(rwErrs, roErrs...) + for _, addr := range connPool.addrs { + if conn := connPool.anyPool.DeleteConnByAddr(addr); conn != nil { + if err := conn.Close(); err != nil { + errs = append(errs, err) + } + } + connPool.rwPool.DeleteConnByAddr(addr) + connPool.roPool.DeleteConnByAddr(addr) + } - return allErrs + return errs } // GetAddrs gets addresses of connections in pool. diff --git a/connection_pool/round_robin.go b/connection_pool/round_robin.go index 7f3f0d098..63afaeb12 100644 --- a/connection_pool/round_robin.go +++ b/connection_pool/round_robin.go @@ -60,19 +60,6 @@ func (r *RoundRobinStrategy) IsEmpty() bool { return r.size == 0 } -func (r *RoundRobinStrategy) CloseConns() []error { - r.mutex.Lock() - defer r.mutex.Unlock() - - errs := make([]error, len(r.conns)) - - for i, conn := range r.conns { - errs[i] = conn.Close() - } - - return errs -} - func (r *RoundRobinStrategy) GetNextConnection() *tarantool.Connection { r.mutex.RLock() defer r.mutex.RUnlock()