Skip to content
Open
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
10 changes: 9 additions & 1 deletion net2/base_connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions net2/base_connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
3 changes: 2 additions & 1 deletion net2/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down