pool

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pool provides enhanced connection pool management utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(db *sql.DB, cfg *PoolConfig) error

Configure applies pool configuration to a sql.DB.

Types

type Monitor

type Monitor struct {
	// contains filtered or unexported fields
}

Monitor provides continuous pool monitoring.

func NewMonitor

func NewMonitor(db *sql.DB, interval time.Duration) *Monitor

NewMonitor creates a new pool monitor.

func (*Monitor) OnSlowQuery

func (m *Monitor) OnSlowQuery(fn func(d time.Duration, query string))

OnSlowQuery registers a callback for slow queries.

func (*Monitor) OnWait

func (m *Monitor) OnWait(fn func(d time.Duration))

OnWait registers a callback for connection wait events.

func (*Monitor) Start

func (m *Monitor) Start(ctx context.Context)

Start begins monitoring the pool.

func (*Monitor) Stop

func (m *Monitor) Stop()

Stop halts the monitor.

type PoolConfig

type PoolConfig struct {
	// MaxOpenConns is the maximum number of open connections to the database.
	MaxOpenConns int
	// MaxIdleConns is the maximum number of connections in the idle connection pool.
	MaxIdleConns int
	// ConnMaxLifetime is the maximum amount of time a connection may be reused.
	ConnMaxLifetime time.Duration
	// ConnMaxIdleTime is the maximum amount of time a connection may be idle.
	ConnMaxIdleTime time.Duration
	// ConnReqDurThreshold is the duration threshold for slow query logging.
	ConnReqDurThreshold time.Duration
}

PoolConfig holds advanced pool configuration.

func DefaultPoolConfig

func DefaultPoolConfig() *PoolConfig

DefaultPoolConfig returns sensible defaults.

func (*PoolConfig) Validate

func (p *PoolConfig) Validate() error

Validate checks if the pool configuration is valid.

type Stats

type Stats struct {
	MaxOpenConnections int
	OpenConnections    int
	InUse              int
	Idle               int
	WaitCount          int64
	WaitDuration       time.Duration
	MaxIdleClosed      int64
	MaxLifetimeClosed  int64
}

Stats holds connection pool statistics.

func GetStats

func GetStats(db *sql.DB) *Stats

GetStats retrieves current pool statistics.

type TracedDB

type TracedDB struct {
	DB      *sql.DB
	SlowLog func(query string, duration time.Duration)
	// contains filtered or unexported fields
}

TracedDB wraps a sql.DB with query timing capabilities.

func NewTracedDB

func NewTracedDB(db *sql.DB) *TracedDB

NewTracedDB creates a traced database wrapper.

func (*TracedDB) ExecContext

func (t *TracedDB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext wraps ExecContext with timing.

func (*TracedDB) QueryContext

func (t *TracedDB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext wraps QueryContext with timing.

func (*TracedDB) QueryRowContext

func (t *TracedDB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext wraps QueryRowContext with timing.

func (*TracedDB) SetSlowQueryThreshold

func (t *TracedDB) SetSlowQueryThreshold(threshold time.Duration)

SetSlowQueryThreshold sets the threshold for slow query logging.

Jump to

Keyboard shortcuts

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