Skip to content

Commit 16617df

Browse files
authored
check all the addresses in pool initialization (#146)
* check all the addresses in pool initialization * format code
1 parent 9e9519f commit 16617df

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

client_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ func TestAuthentication(t *testing.T) {
195195
}
196196

197197
func TestInvalidHostTimeout(t *testing.T) {
198-
hostList := []HostAddress{
198+
hostAdress := HostAddress{Host: address, Port: port}
199+
hostList := []HostAddress{hostAdress}
200+
201+
invalidHostList := []HostAddress{
199202
{Host: "192.168.100.125", Port: 3699}, // Invalid host
200203
{Host: "127.0.0.1", Port: 3699},
201204
}
@@ -207,9 +210,9 @@ func TestInvalidHostTimeout(t *testing.T) {
207210
}
208211
// close all connections in the pool
209212
defer pool.Close()
210-
err = pool.Ping(hostList[0], 1000*time.Millisecond)
213+
err = pool.Ping(invalidHostList[0], 1000*time.Millisecond)
211214
assert.EqualError(t, err, "failed to open transport, error: dial tcp 192.168.100.125:3699: i/o timeout")
212-
err = pool.Ping(hostList[1], 1000*time.Millisecond)
215+
err = pool.Ping(invalidHostList[1], 1000*time.Millisecond)
213216
if err != nil {
214217
t.Error("failed to ping 127.0.0.1")
215218
}

connection_pool.go

+17
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ func NewSslConnectionPool(addresses []HostAddress, conf PoolConfig, sslConfig *t
6868

6969
// initPool initializes the connection pool
7070
func (pool *ConnectionPool) initPool() error {
71+
if err := pool.checkAddresses(); err != nil {
72+
return fmt.Errorf("failed to open connection, error: %s ", err.Error())
73+
}
74+
7175
for i := 0; i < pool.conf.MinConnPoolSize; i++ {
7276
// Simple round-robin
7377
newConn := newConnection(pool.addresses[i%len(pool.addresses)])
@@ -344,3 +348,16 @@ func (pool *ConnectionPool) timeoutConnectionList() (closing []*connection) {
344348
}
345349
return
346350
}
351+
352+
func (pool *ConnectionPool) checkAddresses() error {
353+
var timeout = 3 * time.Second
354+
if pool.conf.TimeOut != 0 && pool.conf.TimeOut < timeout {
355+
timeout = pool.conf.TimeOut
356+
}
357+
for _, address := range pool.addresses {
358+
if err := pool.Ping(address, timeout); err != nil {
359+
return err
360+
}
361+
}
362+
return nil
363+
}

0 commit comments

Comments
 (0)