From 8c696dfc24624dea4e671e8a17db46640f6e4a4e Mon Sep 17 00:00:00 2001 From: Hashim1999164 <64767361+Hashim1999164@users.noreply.github.com> Date: Sun, 23 Aug 2026 02:23:43 +0500 Subject: [PATCH] Default MaxIdleConnections to 2 when unset The Go zero value left the idle pool empty, so every Get dialed a new connection. Unset now uses a small idle size so connections can be reused. --- net2/base_connection_pool.go | 10 +++++++++- net2/base_connection_pool_test.go | 24 ++++++++++++++++++++++++ net2/connection_pool.go | 3 ++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/net2/base_connection_pool.go b/net2/base_connection_pool.go index cf3f67b..3b619e8 100644 --- a/net2/base_connection_pool.go +++ b/net2/base_connection_pool.go @@ -10,6 +10,9 @@ import ( const defaultDialTimeout = 1 * time.Second +// Default idle pool size when ConnectionOptions.MaxIdleConnections is unset. +const defaultMaxIdleConnections uint32 = 2 + func defaultDialFunc(network string, address string) (net.Conn, error) { return net.DialTimeout(network, address, defaultDialTimeout) } @@ -53,9 +56,14 @@ func newBaseConnectionPool( return handle.(net.Conn).Close() } + maxIdle := options.MaxIdleConnections + if maxIdle == 0 { + maxIdle = defaultMaxIdleConnections + } + poolOptions := rp.Options{ MaxActiveHandles: options.MaxActiveConnections, - MaxIdleHandles: options.MaxIdleConnections, + MaxIdleHandles: maxIdle, MaxIdleTime: options.MaxIdleTime, OpenMaxConcurrency: options.DialMaxConcurrency, Open: openFunc, diff --git a/net2/base_connection_pool_test.go b/net2/base_connection_pool_test.go index 4d9710a..dfaae98 100644 --- a/net2/base_connection_pool_test.go +++ b/net2/base_connection_pool_test.go @@ -288,6 +288,30 @@ func (s *BaseConnectionPoolSuite) TestMaxActiveConnections(c *C) { c.Assert(pool.NumActive(), Equals, int32(0)) } +func (s *BaseConnectionPoolSuite) TestDefaultMaxIdleConnections(c *C) { + // Unset MaxIdleConnections used to mean keep zero idle handles, which + // forced a dial on every Get. Zero now applies a small default idle size. + dialer := fakeDialer{} + mockClock := time2.MockClock{} + + options := ConnectionOptions{ + Dial: dialer.FakeDial, + NowFunc: mockClock.Now, + } + pool := NewSimpleConnectionPool(options) + pool.Register("foo", "bar") + + c1, err := pool.Get("foo", "bar") + c.Assert(err, IsNil) + err = c1.ReleaseConnection() + c.Assert(err, IsNil) + c.Assert(pool.NumIdle(), Equals, 1) + + c2, err := pool.Get("foo", "bar") + c.Assert(err, IsNil) + c.Assert(SameConnection(c1, c2), IsTrue) +} + func (s *BaseConnectionPoolSuite) TestMaxIdleConnections(c *C) { dialer := fakeDialer{} mockClock := time2.MockClock{} diff --git a/net2/connection_pool.go b/net2/connection_pool.go index 5b8d4d2..931111b 100644 --- a/net2/connection_pool.go +++ b/net2/connection_pool.go @@ -12,7 +12,8 @@ type ConnectionOptions struct { MaxActiveConnections int32 // The maximum number of idle connections per host that are kept alive by - // the connection pool. + // the connection pool. When unset (zero), a default of 2 is used so the + // Go zero value does not disable idle reuse and force a dial on every Get. MaxIdleConnections uint32 // The maximum amount of time an idle connection can alive (if specified).