Skip to content

pool: add GetInstances method, that returns current instances connections state #428

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
### Added

- Extend box with replication information (#427).
- The GetConnections method has been added,
which is necessary to monitor the current state of the pool (#428).

### Changed

Expand Down
18 changes: 18 additions & 0 deletions pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,3 +1506,21 @@ func isFeatureInSlice(expected iproto.Feature, actualSlice []iproto.Feature) boo
}
return false
}

// GetInstances retrieves a slice of Instance objects representing the connections in the pool.
func (p *ConnectionPool) GetInstances() []Instance {
p.endsMutex.Lock()
defer p.endsMutex.Unlock()

res := make([]Instance, 0, len(p.ends))

for _, end := range p.ends {
res = append(res, Instance{
Name: end.name,
Dialer: end.dialer,
Opts: end.opts,
})
}

return res
}
21 changes: 21 additions & 0 deletions pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3449,6 +3449,27 @@ func TestWatcher_Unregister_concurrent(t *testing.T) {
wg.Wait()
}

func TestConnectionPool_GetInstances(t *testing.T) {
var tCases [][]pool.Instance

tCases = append(tCases, makeInstances([]string{servers[0], servers[1]}, connOpts))
tCases = append(tCases, makeInstances([]string{servers[0], servers[1], servers[3]}, connOpts))
tCases = append(tCases, makeInstances([]string{servers[0]}, connOpts))

for _, tc := range tCases {
ctx, cancel := test_helpers.GetPoolConnectContext()
connPool, err := pool.Connect(ctx, tc)
cancel()
require.Nilf(t, err, "failed to connect")
require.NotNilf(t, connPool, "conn is nil after Connect")

poolInstns := connPool.GetInstances()
require.ElementsMatch(t, tc, poolInstns)
connPool.Close()
}

}

// runTestMain is a body of TestMain function
// (see https://pkg.go.dev/testing#hdr-Main).
// Using defer + os.Exit is not works so TestMain body
Expand Down
Loading