Skip to content
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
4 changes: 2 additions & 2 deletions go/pools/smartconnpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,10 @@ func (pool *ConnPool[C]) closeOnIdleLimitReached(conn *Pooled[C]) bool {

func (pool *ConnPool[D]) extendedMaxLifetime() time.Duration {
maxLifetime := pool.config.maxLifetime.Load()
if maxLifetime == 0 {
if maxLifetime <= 0 {
return 0
}
return time.Duration(maxLifetime) + time.Duration(rand.Uint32N(uint32(maxLifetime)))
return time.Duration(maxLifetime) + time.Duration(rand.Int64N(maxLifetime))
}

func (pool *ConnPool[C]) connReopen(ctx context.Context, dbconn *Pooled[C], now time.Duration) (err error) {
Expand Down
40 changes: 40 additions & 0 deletions go/pools/smartconnpool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,46 @@ func TestExtendedLifetimeTimeout(t *testing.T) {
}
}

func TestExtendedMaxLifetimeJitter(t *testing.T) {
var state TestState
config := &Config[*TestConn]{
Capacity: 1,
MaxLifetime: 30 * time.Minute,
LogWait: state.LogWait,
}

p := NewPool(config).Open(newConnector(&state), nil)
t.Cleanup(p.Close)

threshold := config.MaxLifetime + config.MaxLifetime/2
const maxAttempts = 64

for range maxAttempts {
extended := p.extendedMaxLifetime()
require.LessOrEqual(t, config.MaxLifetime, extended)
require.Greater(t, 2*config.MaxLifetime, extended)

if extended > threshold {
return
}
}

require.Failf(t, "jitter never reached upper half of range",
"no sample in %d tries exceeded %s", maxAttempts, threshold)
}

func TestExtendedMaxLifetimeNegativeDisables(t *testing.T) {
var state TestState
p := NewPool(&Config[*TestConn]{
Capacity: 1,
MaxLifetime: -1 * time.Second,
LogWait: state.LogWait,
}).Open(newConnector(&state), nil)
t.Cleanup(p.Close)

require.EqualValues(t, 0, p.extendedMaxLifetime())
}

// TestMaxIdleCount tests the MaxIdleCount setting, to check if the pool closes
// the idle connections when the number of idle connections exceeds the limit.
func TestMaxIdleCount(t *testing.T) {
Expand Down
Loading