postgres

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package postgres provides an inspectable durable PostgreSQL rate-limit store.

Index

Constants

View Source
const (
	DefaultTable = "goforge_rate_limits"
	MaxAttempts  = 1_000_001

	// Schema is the default plain SQL schema for application migrations.
	Schema = `` /* 320-byte string literal not displayed */

	// DropSchema is intended for isolated integration-test cleanup.
	DropSchema = `DROP TABLE IF EXISTS "goforge_rate_limits";`
)

Variables

This section is empty.

Functions

func DropSchemaFor

func DropSchemaFor(table string) (string, error)

DropSchemaFor returns a quoted cleanup statement for an isolated table.

func SchemaFor

func SchemaFor(table string) (string, error)

SchemaFor returns the same plain schema for an application-selected table.

Types

type Option

type Option func(*config) error

func WithPruneLimit

func WithPruneLimit(limit int) Option

WithPruneLimit bounds how many expired counters each Take may remove before consuming an attempt. Pruning runs first, so a pruning failure never leaves a caller unsure whether its attempt was consumed.

func WithTable

func WithTable(table string) Option

WithTable selects an application-owned PostgreSQL table. Identifiers are validated and quoted before being interpolated into statements.

type SQLStatements

type SQLStatements struct {
	Prune string
	Take  string
	Reset string
}

SQLStatements exposes the exact SQL used by a Store. Values remain bind parameters; only the validated table identifier is interpolated.

func StatementsFor

func StatementsFor(table string) (SQLStatements, error)

StatementsFor returns the parameterized operational SQL for a validated application-owned table without requiring a live database.

func (SQLStatements) String

func (statements SQLStatements) String() string

Ensure SQL stays easy to inspect in source and diagnostics.

type Store

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

Store is a durable fixed-window limiter shared by every process using its PostgreSQL table. It owns neither the database nor schema lifecycle.

func New

func New(db *sql.DB, options ...Option) (*Store, error)

func (*Store) Reset

func (store *Store) Reset(ctx context.Context, key string) error

func (*Store) Statements

func (store *Store) Statements() SQLStatements

func (*Store) Take

func (store *Store) Take(ctx context.Context, key string, policy ratelimit.Policy, _ time.Time) (ratelimit.Decision, error)

Jump to

Keyboard shortcuts

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