diff --git a/internal/infra/db/postgres.go b/internal/infra/db/postgres.go index 4983d77..517c2ba 100644 --- a/internal/infra/db/postgres.go +++ b/internal/infra/db/postgres.go @@ -10,11 +10,12 @@ import ( // 默认连接池配置:10–50 用户私部署量级够用;超出后再按实际负载调整。 const ( - defaultMaxConns = 20 - defaultMinConns = 2 - defaultMaxConnLifetime = time.Hour - defaultPingTimeout = 5 * time.Second - defaultHealthTimeout = time.Second + defaultMaxConns = 20 + defaultMinConns = 2 + defaultMaxConnLifetime = time.Hour // 单连接最大寿命,到点后回收重建 + defaultMaxConnLifetimeJitter = defaultMaxConnLifetime / 10 // 在寿命基础上叠加 0~6 分钟随机抖动,防同步重建 storm + defaultPingTimeout = 5 * time.Second + defaultHealthTimeout = time.Second ) // Pool 是 pgxpool.Pool 的类型别名,外部代码可直接以 db.Pool 表达连接池类型, @@ -31,6 +32,7 @@ func NewPool(ctx context.Context, dsn string) (*Pool, error) { cfg.MaxConns = defaultMaxConns cfg.MinConns = defaultMinConns cfg.MaxConnLifetime = defaultMaxConnLifetime + cfg.MaxConnLifetimeJitter = defaultMaxConnLifetimeJitter pool, err := pgxpool.NewWithConfig(ctx, cfg) if err != nil {