ratelimit

package
v1.19.2 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 3 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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