postgres

package
v1.10.2 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package postgres provides a PostgreSQL-backed rate limit store.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store implements ratelimit.Store using PostgreSQL with a fixed-window algorithm.

func New

func New(db *pgxpool.Pool) *Store

New creates a new PostgreSQL rate limit store.

func (*Store) Check

func (s *Store) Check(ctx context.Context, key string, limit int, window time.Duration) (ratelimit.Result, error)

Check implements ratelimit.Store.Check using a fixed-window algorithm.

Algorithm: 1. If no record exists OR window has expired, create/reset with counter=1 2. If window is active and counter < max, increment and allow 3. If window is active and counter >= max, deny

func (*Store) Cleanup

func (s *Store) Cleanup(ctx context.Context, olderThan time.Duration) (int64, error)

Cleanup removes expired rate limit records. This can be called periodically to prevent the table from growing indefinitely.

func (*Store) Get

func (s *Store) Get(ctx context.Context, key string) (*ratelimit.Result, error)

Get retrieves the current rate limit state for a key without incrementing. Returns nil if no rate limit exists for the key.

func (*Store) Reset

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

Reset implements ratelimit.Store.Reset by deleting the rate limit record.

Jump to

Keyboard shortcuts

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