Documentation
¶
Index ¶
- Variables
- func NewCheckAndSetFailedError(key string, err error) error
- func NewCloseFailedError(err error) error
- func NewConnectionFailedError(err error) error
- func NewDeleteFailedError(key string, err error) error
- func NewGetFailedError(key string, err error) error
- func NewInvalidConfigError(field string) error
- func NewInvalidConnStringError(err error) error
- func NewInvalidPoolConfigError(field string) error
- func NewInvalidValueTypeError(key string, value any) error
- func NewPingFailedError(err error) error
- func NewPoolCreationFailedError(err error) error
- func NewSetFailedError(key string, err error) error
- func NewTableCreationFailedError(err error) error
- func NewTableQueryFailedError(query string, err error) error
- type Backend
- func (p *Backend) CheckAndSet(ctx context.Context, key, oldValue, newValue string, expiration time.Duration) (bool, error)
- func (p *Backend) Close() error
- func (p *Backend) Delete(ctx context.Context, key string) error
- func (p *Backend) Get(ctx context.Context, key string) (string, error)
- func (p *Backend) GetPool() *pgxpool.Pool
- func (p *Backend) PurgeExpired(ctx context.Context, batchSize int) (int64, error)
- func (p *Backend) Set(ctx context.Context, key string, value string, expiration time.Duration) error
- type Config
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
CheckAndSet error functions
func NewCloseFailedError ¶ added in v0.0.6
func NewConnectionFailedError ¶ added in v0.0.6
Connection error functions
func NewDeleteFailedError ¶ added in v0.0.6
func NewGetFailedError ¶ added in v0.0.6
Operation error functions
func NewInvalidConfigError ¶ added in v0.0.6
Configuration error functions
func NewInvalidConnStringError ¶ added in v0.0.6
func NewInvalidPoolConfigError ¶ added in v0.0.6
func NewInvalidValueTypeError ¶ added in v0.0.6
func NewPingFailedError ¶ added in v0.0.6
func NewPoolCreationFailedError ¶ added in v0.0.6
func NewSetFailedError ¶ added in v0.0.6
func NewTableCreationFailedError ¶ added in v0.0.6
Table/Schema error functions
func NewTableQueryFailedError ¶ added in v0.0.6
Types ¶
type Backend ¶ added in v0.0.5
type Backend struct {
// contains filtered or unexported fields
}
func NewWithClient ¶ added in v0.0.8
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) PurgeExpired ¶ added in v0.0.9
PurgeExpired deletes up to batchSize expired rows and returns the number deleted.
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.
Click to show internal directories.
Click to hide internal directories.