Skip to content

Commit 5801dc6

Browse files
committed
bugfix: prevent duplicate connections in pool
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
1 parent 3d16de6 commit 5801dc6

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

connection_pool/connection_pool.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func ConnectWithOpts(addrs []string, connOpts tarantool.Opts, opts OptsPool) (co
9797
anyPool := NewEmptyRoundRobin(size)
9898

9999
connPool = &ConnectionPool{
100-
addrs: make([]string, len(addrs)),
100+
addrs: make([]string, 0, len(addrs)),
101101
connOpts: connOpts,
102102
opts: opts,
103103
notify: notify,
@@ -107,7 +107,14 @@ func ConnectWithOpts(addrs []string, connOpts tarantool.Opts, opts OptsPool) (co
107107
roPool: roPool,
108108
anyPool: anyPool,
109109
}
110-
copy(connPool.addrs, addrs)
110+
111+
m := make(map[string]bool)
112+
for _, addr := range addrs {
113+
if _, ok := m[addr]; !ok {
114+
m[addr] = true
115+
connPool.addrs = append(connPool.addrs, addr)
116+
}
117+
}
111118

112119
somebodyAlive := connPool.fillPools()
113120
if !somebodyAlive {

connection_pool/connection_pool_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ func TestConnSuccessfully(t *testing.T) {
8080
require.Nil(t, err)
8181
}
8282

83+
func TestConnSuccessfullyDuplicates(t *testing.T) {
84+
server := servers[0]
85+
connPool, err := connection_pool.Connect([]string{server, server, server, server}, connOpts)
86+
require.Nilf(t, err, "failed to connect")
87+
require.NotNilf(t, connPool, "conn is nil after Connect")
88+
89+
defer connPool.Close()
90+
91+
args := test_helpers.CheckStatusesArgs{
92+
ConnPool: connPool,
93+
Mode: connection_pool.ANY,
94+
Servers: []string{server},
95+
ExpectedPoolStatus: true,
96+
ExpectedStatuses: map[string]bool{
97+
server: true,
98+
},
99+
}
100+
101+
err = test_helpers.CheckPoolStatuses(args)
102+
require.Nil(t, err)
103+
104+
addrs := connPool.GetAddrs()
105+
require.Equalf(t, []string{server}, addrs, "should be only one address")
106+
}
107+
83108
func TestReconnect(t *testing.T) {
84109
server := servers[0]
85110

0 commit comments

Comments
 (0)