Skip to content

Commit c070b26

Browse files
committed
pool: fix notify on remove a connection
ConnectionPool.Remove() does not notify a ConnectionHandler on an instance removing from the pool.
1 parent 1046f29 commit c070b26

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1414

1515
### Fixed
1616

17+
- `ConnectionPool.Remove()` does not notify a `ConnectionHandler` after
18+
an instance is already removed from the pool (#385)
19+
1720
## [2.0.0] - 2024-02-12
1821

1922
There are a lot of changes in the new major version. The main ones:

pool/connection_pool.go

+1
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,7 @@ func (p *ConnectionPool) controller(ctx context.Context, e *endpoint) {
13741374
// we need to start an another one for the shutdown.
13751375
go func() {
13761376
e.closeErr = e.conn.CloseGraceful()
1377+
p.handlerDeactivated(e.name, e.conn, e.role)
13771378
close(e.closed)
13781379
}()
13791380
} else {

pool/connection_pool_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,77 @@ func TestConnectionHandlerUpdateError(t *testing.T) {
11661166
require.Nilf(t, err, "failed to get ConnectedNow()")
11671167
}
11681168

1169+
type testDeactivatedErrorHandler struct {
1170+
mut sync.Mutex
1171+
deactivated []string
1172+
}
1173+
1174+
func (h *testDeactivatedErrorHandler) Discovered(name string, conn *tarantool.Connection,
1175+
role pool.Role) error {
1176+
return nil
1177+
}
1178+
1179+
func (h *testDeactivatedErrorHandler) Deactivated(name string, conn *tarantool.Connection,
1180+
role pool.Role) error {
1181+
h.mut.Lock()
1182+
defer h.mut.Unlock()
1183+
1184+
h.deactivated = append(h.deactivated, name)
1185+
return nil
1186+
}
1187+
1188+
func TestConnectionHandlerDeactivated_on_remove(t *testing.T) {
1189+
poolServers := []string{servers[0], servers[1]}
1190+
poolInstances := makeInstances(poolServers, connOpts)
1191+
roles := []bool{false, false}
1192+
1193+
err := test_helpers.SetClusterRO(makeDialers(poolServers), connOpts, roles)
1194+
require.Nilf(t, err, "fail to set roles for cluster")
1195+
1196+
h := &testDeactivatedErrorHandler{}
1197+
poolOpts := pool.Opts{
1198+
CheckTimeout: 100 * time.Microsecond,
1199+
ConnectionHandler: h,
1200+
}
1201+
ctx, cancel := test_helpers.GetPoolConnectContext()
1202+
defer cancel()
1203+
connPool, err := pool.ConnectWithOpts(ctx, poolInstances, poolOpts)
1204+
require.Nilf(t, err, "failed to connect")
1205+
require.NotNilf(t, connPool, "conn is nil after Connect")
1206+
defer connPool.Close()
1207+
1208+
args := test_helpers.CheckStatusesArgs{
1209+
ConnPool: connPool,
1210+
Mode: pool.ANY,
1211+
Servers: servers,
1212+
ExpectedPoolStatus: true,
1213+
ExpectedStatuses: map[string]bool{
1214+
servers[0]: true,
1215+
servers[1]: true,
1216+
},
1217+
}
1218+
err = test_helpers.CheckPoolStatuses(args)
1219+
require.Nil(t, err)
1220+
1221+
for _, server := range poolServers {
1222+
connPool.Remove(server)
1223+
connPool.Remove(server)
1224+
}
1225+
1226+
args = test_helpers.CheckStatusesArgs{
1227+
ConnPool: connPool,
1228+
Mode: pool.ANY,
1229+
Servers: servers,
1230+
ExpectedPoolStatus: false,
1231+
}
1232+
err = test_helpers.CheckPoolStatuses(args)
1233+
require.Nil(t, err)
1234+
1235+
h.mut.Lock()
1236+
defer h.mut.Unlock()
1237+
require.ElementsMatch(t, poolServers, h.deactivated)
1238+
}
1239+
11691240
func TestRequestOnClosed(t *testing.T) {
11701241
server1 := servers[0]
11711242
server2 := servers[1]

0 commit comments

Comments
 (0)