Documentation
¶
Overview ¶
Package pgctx carries a *pgxpool.Pool through request contexts.
The package has no opinion on how the pool is created, cached, or routed. Deployments wire Middleware with any PoolFunc they like:
- Standalone single-DB: PoolFunc returns the same *pgxpool.Pool always
- Multi-tenant routed: PoolFunc resolves a per-tenant pool from ctx
- Tests: use With(ctx, testPool) to inject directly
The module that imports pgctx sees only From(ctx) — it does not know whether there is one pool or many, what cache or router sits behind it, or even whether the DB is reached through a pooler.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoPool = errors.New("pgctx: no pool for this context")
ErrNoPool is the sentinel returned by adapters that cannot resolve a pool for the given context (e.g., tenant claim missing). Deployments may wrap or replace it.
Functions ¶
func From ¶
From extracts the pool injected by Middleware.
Panics when no pool is present: a handler reaching here without one means the middleware chain is misconfigured, and silent failure would be worse than a loud boot-time smoke-test crash. Callers that must tolerate missing pools should use FromOK.
func Middleware ¶
Middleware resolves the pool via fn and injects it into the request context. Modules read it back with From().
If fn returns an error, onErr is called (or a 503 is written) and the handler chain is aborted.
func With ¶
With returns a context with the given pool attached.
Used by tests and by background workers that resolve the tenant from message payloads instead of HTTP headers: extract the tenant, get the pool from whatever adapter is in use, then With(ctx, pool) before calling service/repository code.
Types ¶
type ErrorHandler ¶
type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
ErrorHandler turns a PoolFunc error into an HTTP response. Pass nil to Middleware to use defaultErrorHandler (plain 503).