postgres

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Configuration errors
	ErrInvalidConfig     = errors.New("postgres backend requires postgres.Config")
	ErrInvalidConnString = errors.New("invalid postgres connection string")
	ErrInvalidPoolConfig = errors.New("invalid postgres pool configuration")

	// Connection errors
	ErrConnectionFailed   = errors.New("failed to connect to postgres")
	ErrPingFailed         = errors.New("failed to ping postgres server")
	ErrPoolCreationFailed = errors.New("failed to create connection pool")

	// Table/Schema errors
	ErrTableCreationFailed = errors.New("failed to create ratelimit table")
	ErrTableQueryFailed    = errors.New("failed to query ratelimit table")

	// Operation errors
	ErrSetFailed    = errors.New("failed to set key")
	ErrGetFailed    = errors.New("failed to get key")
	ErrDeleteFailed = errors.New("failed to delete key")
	ErrCloseFailed  = errors.New("failed to close postgres connection pool")

	// CheckAndSet specific errors
	ErrCheckAndSetFailed = errors.New("failed to perform check-and-set operation")
	ErrInvalidValueType  = errors.New("invalid value type for check-and-set")
)

Functions

func NewCheckAndSetFailedError added in v0.0.6

func NewCheckAndSetFailedError(key string, err error) error

CheckAndSet error functions

func NewCloseFailedError added in v0.0.6

func NewCloseFailedError(err error) error

func NewConnectionFailedError added in v0.0.6

func NewConnectionFailedError(err error) error

Connection error functions

func NewDeleteFailedError added in v0.0.6

func NewDeleteFailedError(key string, err error) error

func NewGetFailedError added in v0.0.6

func NewGetFailedError(key string, err error) error

Operation error functions

func NewInvalidConfigError added in v0.0.6

func NewInvalidConfigError(field string) error

Configuration error functions

func NewInvalidConnStringError added in v0.0.6

func NewInvalidConnStringError(err error) error

func NewInvalidPoolConfigError added in v0.0.6

func NewInvalidPoolConfigError(field string) error

func NewInvalidValueTypeError added in v0.0.6

func NewInvalidValueTypeError(key string, value any) error

func NewPingFailedError added in v0.0.6

func NewPingFailedError(err error) error

func NewPoolCreationFailedError added in v0.0.6

func NewPoolCreationFailedError(err error) error

func NewSetFailedError added in v0.0.6

func NewSetFailedError(key string, err error) error

func NewTableCreationFailedError added in v0.0.6

func NewTableCreationFailedError(err error) error

Table/Schema error functions

func NewTableQueryFailedError added in v0.0.6

func NewTableQueryFailedError(query string, err error) error

Types

type Backend added in v0.0.5

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

func New

func New(config Config) (*Backend, error)

New initializes a new PostgresStorage with the given configuration.

func NewWithClient added in v0.0.8

func NewWithClient(pool *pgxpool.Pool) *Backend

NewWithClient initializes a new PostgresBackend with a pre-configured connection pool.

The pool is assumed to be already connected and ready for use.

func (*Backend) CheckAndSet added in v0.0.5

func (p *Backend) CheckAndSet(ctx context.Context, key, oldValue, newValue string, expiration time.Duration) (bool, error)

CheckAndSet atomically sets key to newValue only if current value matches oldValue. This operation provides compare-and-swap (CAS) semantics for implementing optimistic locking.

Parameters:

  • ctx: Context for cancellation and timeouts
  • key: The storage key to operate on
  • oldValue: Expected current value. Use empty string "" for "set if not exists" semantics
  • newValue: New value to set if the current value matches oldValue
  • expiration: Time-to-live for the key. Use 0 for no expiration

Returns:

  • bool: true if the CAS succeeded (value was set), false if the compare failed and no write occurred
  • error: Any storage-related error (not including a compare mismatch)

Behavior:

  • If oldValue is "", the operation succeeds only if the key does not exist (or is expired)
  • If oldValue matches the current value, the key is updated to newValue
  • Expired keys are treated as non-existent for comparison purposes
  • All values are stored and compared as strings

Caller contract:

  • A (false, nil) return indicates the compare condition did not match (e.g., another writer won the race or the key already exists when using "set if not exists"). This is not an error. Callers may safely reload state and retry with backoff according to their contention policy.
  • A non-nil error indicates a storage/backend failure and should not be retried blindly.

func (*Backend) Close added in v0.0.5

func (p *Backend) Close() error

func (*Backend) Delete added in v0.0.5

func (p *Backend) Delete(ctx context.Context, key string) error

func (*Backend) Get added in v0.0.5

func (p *Backend) Get(ctx context.Context, key string) (string, error)

func (*Backend) GetPool added in v0.0.5

func (p *Backend) GetPool() *pgxpool.Pool

func (*Backend) PurgeExpired added in v0.0.9

func (p *Backend) PurgeExpired(ctx context.Context, batchSize int) (int64, error)

PurgeExpired deletes up to batchSize expired rows and returns the number deleted.

func (*Backend) Set added in v0.0.5

func (p *Backend) Set(ctx context.Context, key string, value string, expiration time.Duration) error

type Config added in v0.0.5

type Config struct {
	// ConnString is the PostgreSQL connection string.
	//
	// Format: "postgres://username:password@hostname:port/database?sslmode=disable"
	ConnString string
	// MaxConns is the maximum number of connections in the pool.
	//
	// If 0, a sensible default is used.
	MaxConns int32
	// MinConns is the minimum number of connections in the pool.
	//
	// If 0, defaults to 2.
	MinConns int32
	// ConnErrorStrings contains string patterns to identify connectivity-related errors.
	//
	// If nil, the default patterns from connErrorStrings are used.
	// These patterns help distinguish temporary connectivity issues from operational
	// errors like constraint violations.
	ConnErrorStrings []string
}

Config holds configuration for creating a PostgreSQL backend.

Jump to

Keyboard shortcuts

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