Help us shape the future of Go! Take the 2026 Go Developer Survey

fairpool

package
v0.114.6 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const RetryAfter = time.Second

RetryAfter is how long callers are told to wait before retrying a request that a LimitError rejected. It is a hint: the cap clears as soon as an in-flight checkout is released.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

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

Conn is a pooled connection counted against a tenant. Hijack releases the tenant slot immediately, because a hijacked connection no longer occupies the pool. Release returns the connection and the slot.

func (*Conn) Begin

func (c *Conn) Begin(ctx context.Context) (pgx.Tx, error)

Begin starts a transaction on the checked-out connection. The slot stays held until Release.

func (*Conn) BeginTx

func (c *Conn) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)

BeginTx starts a transaction with options on the checked-out connection. The slot stays held until Release.

func (*Conn) Conn

func (c *Conn) Conn() *pgx.Conn

Conn returns the underlying pgx connection. It stays owned by the pool and counted against the slot; use Hijack to take it over.

func (*Conn) CopyFrom

func (c *Conn) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)

CopyFrom bulk-loads rows over the checked-out connection.

func (*Conn) Exec

func (c *Conn) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)

Exec runs a statement on the checked-out connection.

func (*Conn) Hijack

func (c *Conn) Hijack() *pgx.Conn

Hijack removes the connection from the pool and frees the tenant slot. The caller owns the returned connection.

func (*Conn) Ping

func (c *Conn) Ping(ctx context.Context) error

Ping checks that the checked-out connection is alive.

func (*Conn) Query

func (c *Conn) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

Query runs a query on the checked-out connection. The caller closes the rows; the slot stays held until Release.

func (*Conn) QueryRow

func (c *Conn) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

QueryRow runs a single-row query on the checked-out connection.

func (*Conn) Release

func (c *Conn) Release()

Release returns the connection to the pool and frees the tenant slot.

func (*Conn) SendBatch

func (c *Conn) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults

SendBatch sends a batch on the checked-out connection. The caller closes the results.

type Handle

type Handle interface {
	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
	CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
	SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
	Begin(ctx context.Context) (pgx.Tx, error)
	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
	Acquire(ctx context.Context) (*Conn, error)
	Stat() *pgxpool.Stat
}

Handle is one tenant's checkout, or the shared checkout. sqlc accepts it anywhere it accepts a DBTX. sqlchelpers.Pool is the smaller begin-a-transaction view of a Handle.

type LimitError

type LimitError struct {
	// Key is the gate bucket. Tenants use the tenant id string; shared work uses "shared".
	Key      string
	TenantID uuid.UUID
	Limit    int64
	Waited   time.Duration
}

LimitError is returned when a tenant or shared work waits MaxWait without a free slot.

func (*LimitError) Error

func (e *LimitError) Error() string

type Options

type Options struct {
	MaxPercent       int
	SharedMaxPercent int
	MaxWait          time.Duration
	PoolName         string
	L                *zerolog.Logger
}

Options configures the connection caps.

MaxPercent is the share of the pool one tenant may have in flight. SharedMaxPercent is the same share for the bucket of engine work that is not tied to a tenant (ForShared and a nil tenant id). For both, a value from 1 to 99 installs a cap, and 100 (or 0, the zero value) installs none, so that bucket may hold the whole pool. NewWithConfig rejects values outside 0 to 100.

type Pool

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

Pool is a pgx pool plus optional connection caps. ForTenant counts a checkout against a tenant and ForShared counts it against shared engine work. Each tenant has its own limit, and the shared bucket has a separate one. Ungated and Unwrap do not count.

func NewWithConfig

func NewWithConfig(ctx context.Context, cfg *pgxpool.Config, opts Options) (*Pool, error)

NewWithConfig builds a pool from cfg. The tracer already set on cfg is left in place.

func Ungated

func Ungated(inner *pgxpool.Pool) *Pool

Ungated returns a pool that does not cap connections. Tests and call sites that must not take a slot use this instead of ForTenant or ForShared.

func (*Pool) Close

func (p *Pool) Close()

Close closes the underlying pool. It blocks until all checked-out connections are released.

func (*Pool) Config

func (p *Pool) Config() *pgxpool.Config

Config returns a copy of the underlying pool's configuration.

func (*Pool) ConnectionLimit

func (p *Pool) ConnectionLimit() int64

ConnectionLimit is how many checkouts one tenant may have in flight. Zero means tenants are ungated.

func (*Pool) ForShared

func (p *Pool) ForShared() Handle

ForShared returns the handle for engine work that is not tied to one tenant. It counts against the shared limit, not a tenant's. A pool with no shared gate returns a handle that does not count.

func (*Pool) ForTenant

func (p *Pool) ForTenant(tenantID uuid.UUID) Handle

ForTenant returns a handle that counts acquires against tenantID. A nil id counts against the shared bucket. A pool with no gate returns a handle that does not count.

func (*Pool) SharedConnectionLimit

func (p *Pool) SharedConnectionLimit() int64

SharedConnectionLimit is how many checkouts the shared bucket may have in flight. Zero means the shared bucket is ungated.

func (*Pool) Stat

func (p *Pool) Stat() *pgxpool.Stat

Stat returns the underlying pool's statistics.

func (*Pool) Unwrap

func (p *Pool) Unwrap() *pgxpool.Pool

Unwrap returns the underlying pgx pool. Callers that hijack a connection, such as LISTEN, use this so they never take a gate slot.

Jump to

Keyboard shortcuts

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