Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check all the addresses in pool initialization #146

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
Expand All @@ -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")
}
Expand Down
17 changes: 17 additions & 0 deletions connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func NewSslConnectionPool(addresses []HostAddress, conf PoolConfig, sslConfig *t

// initPool initializes the connection pool
func (pool *ConnectionPool) initPool() 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++ {
// Simple round-robin
newConn := newConnection(pool.addresses[i%len(pool.addresses)])
Expand Down Expand Up @@ -344,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
}