From bb0aea74591be87fd377358515ea746bd9a34186 Mon Sep 17 00:00:00 2001 From: "maksim.konovalov" Date: Mon, 3 Feb 2025 18:13:52 +0300 Subject: [PATCH] pool: add GetInstances method, that returns current instances connections state The GetConnections method has been added, which is necessary to monitor the current state of the pool. This should help implement dynamic tracking of tarantool topology changes. --- CHANGELOG.md | 2 ++ pool/connection_pool.go | 18 ++++++++++++++++++ pool/connection_pool_test.go | 21 +++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82f9766f7..7196dac8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pool/connection_pool.go b/pool/connection_pool.go index 9be46665e..5255c377c 100644 --- a/pool/connection_pool.go +++ b/pool/connection_pool.go @@ -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 +} diff --git a/pool/connection_pool_test.go b/pool/connection_pool_test.go index c4e43e2d3..fb1039bcc 100644 --- a/pool/connection_pool_test.go +++ b/pool/connection_pool_test.go @@ -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