phdb

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package phdb centralizes database/sql connection-pool configuration for PayCloud services so pool sizing is consistent, env-driven, and safe (never unbounded). See paycloud-docs postgres-migration 01-analysis/05-...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(sqlDB *sql.DB, cfg PoolConfig) error

Apply validates cfg and applies all four pool knobs to sqlDB, then logs the effective configuration (observability parity with dual-engine-pattern.md). ConnMaxLifetime is stretched by up to +10% jitter before SetConnMaxLifetime.

func OpenAndApply

func OpenAndApply(sqlDB *sql.DB, cfg PoolConfig) (*sql.DB, error)

OpenAndApply applies pool configuration to an already-open *sql.DB and returns it. DSN building stays per-service / dual-engine — this helper only configures the pool knobs.

Types

type PoolConfig

type PoolConfig struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
	Logger          func(format string, args ...any)
}

PoolConfig holds the four database/sql pool knobs. All four are always set by Apply — including ConnMaxIdleTime, which most services historically omitted.

Logger, when non-nil, receives the Apply log line instead of the default phlogger/log fallback (avoids an import cycle with the root paycloudhelper package).

func DefaultPoolConfig

func DefaultPoolConfig() PoolConfig

DefaultPoolConfig returns conservative defaults sized for a PgBouncer-fronted PostgreSQL instance. Kept small on purpose: PgBouncer multiplexes, so large per-pod pools only inflate the client-connection count.

func LoadPoolConfig

func LoadPoolConfig(prefix string) PoolConfig

LoadPoolConfig reads <prefix>_MAX_OPEN_CONN, <prefix>_MAX_IDLE_CONN, <prefix>_CONN_MAX_LIFETIME (minutes), <prefix>_CONN_MAX_IDLE_TIME (minutes). Missing or non-positive values fall back to DefaultPoolConfig — a bad env value can never produce an unbounded pool.

func (PoolConfig) Validate

func (c PoolConfig) Validate() error

Validate rejects configurations that are unsafe for PostgreSQL. Most importantly it forbids MaxOpenConns <= 0, which database/sql treats as UNLIMITED — a fast path to exhausting a small PostgreSQL instance.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL