Skip to content

Commit

Permalink
bugfix: prevent duplicate connections in pool
Browse files Browse the repository at this point in the history
An user can specify duplicate addresses for a ConnectionPool. We
cannot support multiple connections to the same address due to
the ConnectionPool.GetPoolInfo() implementation without breaking
backward compatibility. So we need to skip duplicates.

Closes #208
  • Loading branch information
oleg-jukovec committed Aug 25, 2022
1 parent 4ea0006 commit 6dee596
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
11 changes: 9 additions & 2 deletions connection_pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func ConnectWithOpts(addrs []string, connOpts tarantool.Opts, opts OptsPool) (co
anyPool := NewEmptyRoundRobin(size)

connPool = &ConnectionPool{
addrs: make([]string, len(addrs)),
addrs: make([]string, 0, len(addrs)),
connOpts: connOpts,
opts: opts,
notify: notify,
Expand All @@ -107,7 +107,14 @@ func ConnectWithOpts(addrs []string, connOpts tarantool.Opts, opts OptsPool) (co
roPool: roPool,
anyPool: anyPool,
}
copy(connPool.addrs, addrs)

m := make(map[string]bool)
for _, addr := range addrs {
if _, ok := m[addr]; !ok {
m[addr] = true
connPool.addrs = append(connPool.addrs, addr)
}
}

somebodyAlive := connPool.fillPools()
if !somebodyAlive {
Expand Down
25 changes: 25 additions & 0 deletions connection_pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ func TestConnSuccessfully(t *testing.T) {
require.Nil(t, err)
}

func TestConnSuccessfullyDuplicates(t *testing.T) {
server := servers[0]
connPool, err := connection_pool.Connect([]string{server, server, server, server}, connOpts)
require.Nilf(t, err, "failed to connect")
require.NotNilf(t, connPool, "conn is nil after Connect")

defer connPool.Close()

args := test_helpers.CheckStatusesArgs{
ConnPool: connPool,
Mode: connection_pool.ANY,
Servers: []string{server},
ExpectedPoolStatus: true,
ExpectedStatuses: map[string]bool{
server: true,
},
}

err = test_helpers.CheckPoolStatuses(args)
require.Nil(t, err)

addrs := connPool.GetAddrs()
require.Equalf(t, []string{server}, addrs, "should be only one address")
}

func TestReconnect(t *testing.T) {
server := servers[0]

Expand Down

0 comments on commit 6dee596

Please sign in to comment.