From 6fb8cb86fed44b40290782aadb4e9eededa1260e Mon Sep 17 00:00:00 2001 From: HarrisChu <1726587+HarrisChu@users.noreply.github.com> Date: Tue, 26 Oct 2021 15:39:33 +0800 Subject: [PATCH 1/2] check all the addresses in pool initialization --- client_test.go | 9 ++++++--- connection_pool.go | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/client_test.go b/client_test.go index e5673b56..c5285098 100644 --- a/client_test.go +++ b/client_test.go @@ -195,7 +195,10 @@ func TestAuthentication(t *testing.T) { } func TestInvalidHostTimeout(t *testing.T) { - hostList := []HostAddress{ + hostAdress := HostAddress{Host: address, Port: port} + hostList := []HostAddress{hostAdress} + + invalidHostList := []HostAddress{ {Host: "192.168.100.125", Port: 3699}, // Invalid host {Host: "127.0.0.1", Port: 3699}, } @@ -207,9 +210,9 @@ func TestInvalidHostTimeout(t *testing.T) { } // close all connections in the pool defer pool.Close() - err = pool.Ping(hostList[0], 1000*time.Millisecond) + err = pool.Ping(invalidHostList[0], 1000*time.Millisecond) assert.EqualError(t, err, "failed to open transport, error: dial tcp 192.168.100.125:3699: i/o timeout") - err = pool.Ping(hostList[1], 1000*time.Millisecond) + err = pool.Ping(invalidHostList[1], 1000*time.Millisecond) if err != nil { t.Error("failed to ping 127.0.0.1") } diff --git a/connection_pool.go b/connection_pool.go index 96bb14e5..3716c673 100644 --- a/connection_pool.go +++ b/connection_pool.go @@ -68,6 +68,16 @@ func NewSslConnectionPool(addresses []HostAddress, conf PoolConfig, sslConfig *t // initPool initializes the connection pool func (pool *ConnectionPool) initPool() error { + var timeout = 3 * time.Second + if pool.conf.TimeOut != 0 && pool.conf.TimeOut < timeout { + timeout = pool.conf.TimeOut + } + for _, address := range pool.addresses { + if err := pool.Ping(address, timeout); err != nil { + return fmt.Errorf("failed to open connection, error: %s ", err.Error()) + } + } + for i := 0; i < pool.conf.MinConnPoolSize; i++ { // Simple round-robin newConn := newConnection(pool.addresses[i%len(pool.addresses)]) From 69076a33830e995b125a8e9e5c04cf012df0844b Mon Sep 17 00:00:00 2001 From: HarrisChu <1726587+HarrisChu@users.noreply.github.com> Date: Tue, 26 Oct 2021 16:22:50 +0800 Subject: [PATCH 2/2] format code --- connection_pool.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/connection_pool.go b/connection_pool.go index 3716c673..fdd0f290 100644 --- a/connection_pool.go +++ b/connection_pool.go @@ -68,14 +68,8 @@ func NewSslConnectionPool(addresses []HostAddress, conf PoolConfig, sslConfig *t // initPool initializes the connection pool func (pool *ConnectionPool) initPool() error { - var timeout = 3 * time.Second - if pool.conf.TimeOut != 0 && pool.conf.TimeOut < timeout { - timeout = pool.conf.TimeOut - } - for _, address := range pool.addresses { - if err := pool.Ping(address, timeout); err != nil { - return fmt.Errorf("failed to open connection, error: %s ", err.Error()) - } + if err := pool.checkAddresses(); err != nil { + return fmt.Errorf("failed to open connection, error: %s ", err.Error()) } for i := 0; i < pool.conf.MinConnPoolSize; i++ { @@ -354,3 +348,16 @@ func (pool *ConnectionPool) timeoutConnectionList() (closing []*connection) { } return } + +func (pool *ConnectionPool) checkAddresses() error { + var timeout = 3 * time.Second + if pool.conf.TimeOut != 0 && pool.conf.TimeOut < timeout { + timeout = pool.conf.TimeOut + } + for _, address := range pool.addresses { + if err := pool.Ping(address, timeout); err != nil { + return err + } + } + return nil +}