Skip to content

pool: fix notify on remove a connection #385

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

Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.

### Fixed

- `ConnectionPool.Remove()` does not notify a `ConnectionHandler` after
an instance is already removed from the pool (#385)

## [2.0.0] - 2024-02-12

There are a lot of changes in the new major version. The main ones:
Expand Down
1 change: 1 addition & 0 deletions pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ func (p *ConnectionPool) controller(ctx context.Context, e *endpoint) {
// we need to start an another one for the shutdown.
go func() {
e.closeErr = e.conn.CloseGraceful()
p.handlerDeactivated(e.name, e.conn, e.role)
close(e.closed)
}()
} else {
Expand Down
71 changes: 71 additions & 0 deletions pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,77 @@ func TestConnectionHandlerUpdateError(t *testing.T) {
require.Nilf(t, err, "failed to get ConnectedNow()")
}

type testDeactivatedErrorHandler struct {
mut sync.Mutex
deactivated []string
}

func (h *testDeactivatedErrorHandler) Discovered(name string, conn *tarantool.Connection,
role pool.Role) error {
return nil
}

func (h *testDeactivatedErrorHandler) Deactivated(name string, conn *tarantool.Connection,
role pool.Role) error {
h.mut.Lock()
defer h.mut.Unlock()

h.deactivated = append(h.deactivated, name)
return nil
}

func TestConnectionHandlerDeactivated_on_remove(t *testing.T) {
poolServers := []string{servers[0], servers[1]}
poolInstances := makeInstances(poolServers, connOpts)
roles := []bool{false, false}

err := test_helpers.SetClusterRO(makeDialers(poolServers), connOpts, roles)
require.Nilf(t, err, "fail to set roles for cluster")

h := &testDeactivatedErrorHandler{}
poolOpts := pool.Opts{
CheckTimeout: 100 * time.Microsecond,
ConnectionHandler: h,
}
ctx, cancel := test_helpers.GetPoolConnectContext()
defer cancel()
connPool, err := pool.ConnectWithOpts(ctx, poolInstances, poolOpts)
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: pool.ANY,
Servers: servers,
ExpectedPoolStatus: true,
ExpectedStatuses: map[string]bool{
servers[0]: true,
servers[1]: true,
},
}
err = test_helpers.CheckPoolStatuses(args)
require.Nil(t, err)

for _, server := range poolServers {
connPool.Remove(server)
connPool.Remove(server)
}

args = test_helpers.CheckStatusesArgs{
ConnPool: connPool,
Mode: pool.ANY,
Servers: servers,
ExpectedPoolStatus: false,
}
err = test_helpers.CheckPoolStatuses(args)
require.Nil(t, err)

h.mut.Lock()
defer h.mut.Unlock()
require.ElementsMatch(t, poolServers, h.deactivated)
}

func TestRequestOnClosed(t *testing.T) {
server1 := servers[0]
server2 := servers[1]
Expand Down