mirror of
https://github.com/jackc/pgx.git
synced 2025-04-27 13:14:32 +00:00
Make MaxConnLifetimeJitter setting actually jitter
This commit is contained in:
parent
f42af35884
commit
29ad306e47
@ -22,10 +22,11 @@ var defaultMaxConnIdleTime = time.Minute * 30
|
|||||||
var defaultHealthCheckPeriod = time.Minute
|
var defaultHealthCheckPeriod = time.Minute
|
||||||
|
|
||||||
type connResource struct {
|
type connResource struct {
|
||||||
conn *pgx.Conn
|
conn *pgx.Conn
|
||||||
conns []Conn
|
conns []Conn
|
||||||
poolRows []poolRow
|
poolRows []poolRow
|
||||||
poolRowss []poolRows
|
poolRowss []poolRows
|
||||||
|
maxAgeTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cr *connResource) getConn(p *Pool, res *puddle.Resource[*connResource]) *Conn {
|
func (cr *connResource) getConn(p *Pool, res *puddle.Resource[*connResource]) *Conn {
|
||||||
@ -219,11 +220,15 @@ func NewWithConfig(ctx context.Context, config *Config) (*Pool, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jitterSecs := rand.Float64() * config.MaxConnLifetimeJitter.Seconds()
|
||||||
|
maxAgeTime := time.Now().Add(config.MaxConnLifetime).Add(time.Duration(jitterSecs) * time.Second)
|
||||||
|
|
||||||
cr := &connResource{
|
cr := &connResource{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
conns: make([]Conn, 64),
|
conns: make([]Conn, 64),
|
||||||
poolRows: make([]poolRow, 64),
|
poolRows: make([]poolRow, 64),
|
||||||
poolRowss: make([]poolRows, 64),
|
poolRowss: make([]poolRows, 64),
|
||||||
|
maxAgeTime: maxAgeTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
return cr, nil
|
return cr, nil
|
||||||
@ -364,17 +369,7 @@ func (p *Pool) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) isExpired(res *puddle.Resource[*connResource]) bool {
|
func (p *Pool) isExpired(res *puddle.Resource[*connResource]) bool {
|
||||||
now := time.Now()
|
return time.Now().After(res.Value().maxAgeTime)
|
||||||
// Small optimization to avoid rand. If it's over lifetime AND jitter, immediately
|
|
||||||
// return true.
|
|
||||||
if now.Sub(res.CreationTime()) > p.maxConnLifetime+p.maxConnLifetimeJitter {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if p.maxConnLifetimeJitter == 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
jitterSecs := rand.Float64() * p.maxConnLifetimeJitter.Seconds()
|
|
||||||
return now.Sub(res.CreationTime()) > p.maxConnLifetime+(time.Duration(jitterSecs)*time.Second)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) triggerHealthCheck() {
|
func (p *Pool) triggerHealthCheck() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user