pool

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package pool provides a pgxpool.Pool wrapper with schema isolation, database/sql compatibility, health check, and graceful shutdown.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Metrics

type Metrics struct {
	Active   int32         `json:"active"`
	Idle     int32         `json:"idle"`
	Total    int32         `json:"total"`
	Max      int32         `json:"max"`
	WaitTime time.Duration `json:"-"`
}

Metrics holds a snapshot of pool statistics.

func (Metrics) HealthJSON

func (m Metrics) HealthJSON() ([]byte, error)

HealthJSON returns a JSON-encoded representation of the metrics suitable for use in health endpoints. WaitTime is expressed as milliseconds.

type Pool

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

Pool wraps pgxpool.Pool with schema isolation and database/sql compatibility.

func NewPool

func NewPool(ctx context.Context, dsn, schema string) (*Pool, error)

NewPool opens a connection pool for the given DSN with schema isolation. It creates the schema if it doesn't exist and sets the search_path.

Example
package main

import (
	"context"
	"log"

	"github.com/benaskins/axon-base/pool"
)

func main() {
	ctx := context.Background()
	p, err := pool.NewPool(ctx, "postgres://postgres@localhost:5432/mydb", "public")
	if err != nil {
		log.Fatal(err)
	}
	defer p.Close()

	if p.Healthy(ctx) {
		log.Println("database is reachable")
	}
}

func (*Pool) Close

func (p *Pool) Close()

Close closes all connections in the pool and the database/sql handle if opened.

func (*Pool) Healthy

func (p *Pool) Healthy(ctx context.Context) bool

Healthy pings the database and returns true if it responds.

func (*Pool) Metrics

func (p *Pool) Metrics() Metrics

Metrics returns a snapshot of the current pool statistics.

Example
package main

import (
	"context"
	"log"

	"github.com/benaskins/axon-base/pool"
)

func main() {
	ctx := context.Background()
	p, err := pool.NewPool(ctx, "postgres://postgres@localhost:5432/mydb", "public")
	if err != nil {
		log.Fatal(err)
	}
	defer p.Close()

	m := p.Metrics()
	data, err := m.HealthJSON()
	if err != nil {
		log.Fatal(err)
	}
	// data is JSON suitable for a health endpoint:
	// {"active":0,"idle":0,"total":0,"max":4,"wait_time_ms":0}
	_ = data
}

func (*Pool) StdDB

func (p *Pool) StdDB() (*sql.DB, error)

StdDB returns a *sql.DB handle backed by the same DSN and search_path. The handle is created lazily and cached for the lifetime of the Pool. This provides compatibility with libraries that require database/sql (e.g., goose migrations, axon-fact event stores).

func (*Pool) WithTransaction

func (p *Pool) WithTransaction(ctx context.Context, fn TxFunc) error

WithTransaction begins a transaction, executes fn, and commits on success. If fn returns an error, the transaction is rolled back and the error is returned.

Example
ctx := context.Background()
p, err := pool.NewPool(ctx, "postgres://postgres@localhost:5432/mydb", "public")
if err != nil {
	log.Fatal(err)
}
defer p.Close()

err = p.WithTransaction(ctx, func(ctx context.Context, tx pgx.Tx) error {
	_, err := tx.Exec(ctx, "INSERT INTO users (id, name) VALUES ($1, $2)", "abc", "alice")
	return err
})
if err != nil {
	log.Fatal(err)
}

type TxFunc

type TxFunc func(ctx context.Context, tx pgx.Tx) error

TxFunc is a callback executed within a transaction.

Jump to

Keyboard shortcuts

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