ratelimit

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: 3 Imported by: 0

Documentation

Overview

Package ratelimit provides rate limiting functionality with pluggable storage backends.

Index

Constants

This section is empty.

Variables

View Source
var ErrRateLimited = errors.New("rate limit exceeded")

ErrRateLimited is returned when a request exceeds the rate limit.

Functions

This section is empty.

Types

type Result

type Result struct {
	// Allowed indicates whether the request is permitted.
	Allowed bool
	// Remaining is the number of requests remaining in the current window.
	Remaining int
	// ResetAt is when the current rate limit window resets.
	ResetAt time.Time
}

Result contains the outcome of a rate limit check.

type Store

type Store interface {
	// Check increments the counter for the given key and returns whether
	// the request is allowed based on the maximum requests per window.
	//
	// Parameters:
	//   - key: unique identifier for the rate limit (e.g., "login:username")
	//   - max: maximum number of requests allowed in the window
	//   - window: duration of the rate limit window
	//
	// Returns a Result indicating if the request is allowed, remaining
	// attempts, and when the window resets.
	Check(ctx context.Context, key string, limit int, window time.Duration) (Result, error)

	// Reset clears the rate limit for the given key, allowing immediate
	// access again. Useful after successful authentication.
	Reset(ctx context.Context, key string) error
}

Store defines the interface for rate limit storage backends.

Directories

Path Synopsis
Package postgres provides a PostgreSQL-backed rate limit store.
Package postgres provides a PostgreSQL-backed rate limit store.

Jump to

Keyboard shortcuts

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