postgresservice

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package postgresservice adapts PostgreSQL pools to the service lifecycle. Constructor results are owned by the adapter. Existing pools remain shared unless ownership is explicitly transferred. The adapter performs no retries, closes owned pools exactly once, and never closes shared pools.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidOptions identifies invalid adapter construction.
	ErrInvalidOptions = errors.New("invalid postgres service options")
	// ErrUnavailable identifies a pool that has not started or is stopping.
	ErrUnavailable = errors.New("postgres service pool unavailable")
)

Functions

This section is empty.

Types

type Adapter

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

Adapter owns the lifecycle state for one PostgreSQL pool.

func New

func New(options Options) (*Adapter, error)

New validates and constructs an inert adapter.

Example
package main

import (
	"context"
	"fmt"

	"github.com/faustbrian/go-postgres"
	postgresservice "github.com/faustbrian/go-postgres/adapters/service"
)

var _ postgresservice.Resource = (*postgres.Pool)(nil)

func main() {
	pool := &resource{}
	adapter, err := postgresservice.New(postgresservice.Options{
		Name:              "database",
		Pool:              pool,
		TransferOwnership: true,
		StartupPing:       true,
	})
	if err != nil {
		fmt.Println("adapter setup failed")

		return
	}

	component := adapter.Component()
	_ = component.Start(context.Background())
	check := adapter.Readiness()
	_ = check.Run(context.Background())
	_ = component.Stop(context.Background())

	fmt.Println(pool.pings, pool.closes)
}
Output:
2 1

func (*Adapter) Component

func (adapter *Adapter) Component() service.Component

Component returns the ordered service lifecycle component.

func (*Adapter) Pool

func (adapter *Adapter) Pool() (Resource, bool)

Pool returns the current pool after successful component startup.

func (*Adapter) Readiness

func (adapter *Adapter) Readiness() service.ReadinessCheck

Readiness returns an opt-in dependency check for the active pool.

type Constructor

type Constructor func(context.Context) (Resource, error)

Constructor acquires a pool whose ownership transfers after a successful return.

type Options

type Options struct {
	// Name is the secret-safe component and readiness-check name.
	Name string
	// Construct acquires an adapter-owned pool during component startup.
	Construct Constructor
	// Pool supplies an existing pool. The caller retains ownership unless
	// TransferOwnership is true.
	Pool Resource
	// TransferOwnership closes Pool during shutdown and failed startup.
	TransferOwnership bool
	// StartupPing validates the acquired pool before startup succeeds.
	StartupPing bool
}

Options configure one PostgreSQL lifecycle adapter.

type OptionsError

type OptionsError struct {
	Field  string
	Reason string
}

OptionsError identifies one rejected option.

func (*OptionsError) Error

func (err *OptionsError) Error() string

Error returns a secret-safe construction diagnostic.

func (*OptionsError) Unwrap

func (err *OptionsError) Unwrap() error

Unwrap exposes the stable option classification.

type Resource

type Resource interface {
	Ping(context.Context) error
	Close(context.Context) error
}

Resource is the lifecycle surface required from a PostgreSQL pool.

type StartupError

type StartupError struct {
	Validation error
	Cleanup    error
}

StartupError preserves validation and partial-cleanup failures.

func (*StartupError) Error

func (err *StartupError) Error() string

Error returns a secret-safe startup diagnostic.

func (*StartupError) Unwrap

func (err *StartupError) Unwrap() []error

Unwrap preserves both failures for errors.Is and errors.As.

Jump to

Keyboard shortcuts

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