Help us shape the future of Go! Take the 2026 Go Developer Survey

guard

package
v0.68.0 Latest Latest
Warning

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

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

Documentation

Overview

Package guard holds the request guards that need no HTTP: a bounded keyed rate limiter and the client-IP rule.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request, trusted []netip.Prefix) netip.Addr

ClientIP takes the address from r.RemoteAddr and, when it is inside one of trusted, walks X-Forwarded-For from the right and returns the first entry that is not a trusted proxy. With no trusted proxies the header is ignored, so a sender cannot pick its own bucket.

It returns the zero Addr when RemoteAddr does not parse; callers treat that as one shared bucket.

func ParseProxies

func ParseProxies(cidrs []string) ([]netip.Prefix, error)

ParseProxies parses CIDRs, or bare addresses, naming the proxies whose X-Forwarded-For ClientIP honours.

Types

type Limiter

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

Limiter is a keyed token bucket that admits count events per window. The key map is bounded at maxKeys and idle entries are evicted, so a flood of distinct keys (IPs, emails) cannot grow memory without limit.

func NewLimiter

func NewLimiter(count int, window time.Duration, maxKeys int, idle time.Duration, now func() time.Time) *Limiter

NewLimiter returns a Limiter for count events per window, count >= 1. A nil now means time.Now.

func (*Limiter) Allow

func (l *Limiter) Allow(key string) (ok bool, retryAfter time.Duration)

Allow reports whether key may proceed now. When it may not, retryAfter is the wait until it could, in whole seconds and at least one, so callers can put it straight into Retry-After. A denied call spends no token.

Jump to

Keyboard shortcuts

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