cache

package
v1.3.0 Latest Latest
Warning

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

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

Documentation

Overview

Package cache provides rate limiting, cooldown, and failure-policy abstractions for Email OTP.

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheCheckFailed = errors.New("cache: security check failed")

ErrCacheCheckFailed is returned when an operation is rejected due to a cache failure under FailClosed.

Functions

func HashIdentifier

func HashIdentifier(identifier string) string

HashIdentifier produces an opaque hex hash of the identifier to avoid storing plaintext emails in cache keys.

Types

type FailurePolicy

type FailurePolicy string

FailurePolicy defines how cache failures (timeouts, partition, downtime) are handled for security controls.

const (
	// FailOpen allows requests to proceed when cache operations fail, prioritizing availability.
	FailOpen FailurePolicy = "fail_open"

	// FailClosed denies requests when cache operations fail, prioritizing abuse protection.
	FailClosed FailurePolicy = "fail_closed"
)

type Limiter

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

Limiter manages rate limiting and cooldown state, utilizing Core Cache when available.

func NewLimiter

func NewLimiter(c corecache.Cache, policy FailurePolicy, now func() time.Time) *Limiter

NewLimiter constructs a Limiter instance.

func (*Limiter) CheckAndSetCooldown

func (l *Limiter) CheckAndSetCooldown(ctx context.Context, email string, cooldown time.Duration) (bool, error)

CheckAndSetCooldown checks whether a cooldown is currently active, and activates it if not.

func (*Limiter) CheckRateLimit

func (l *Limiter) CheckRateLimit(ctx context.Context, email string, maxRequests int, window time.Duration) (bool, error)

CheckRateLimit verifies if an email request is within the allowable limit.

Jump to

Keyboard shortcuts

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