ratelimit

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

Limiter is an in-memory sliding-window rate limiter keyed by string.

func New

func New(window time.Duration, max int) *Limiter

func (*Limiter) Allow

func (l *Limiter) Allow(key string) bool

Allow returns true if the key has not exceeded the rate limit. If allowed, the attempt is recorded.

func (*Limiter) AllowWithRetryAfter added in v0.3.0

func (l *Limiter) AllowWithRetryAfter(key string) (bool, time.Duration)

AllowWithRetryAfter behaves like Allow but additionally returns the duration the caller must wait before the next attempt could succeed (rounded up to the next whole second so callers can use it for the Retry-After response header). When the request is allowed, the returned duration is zero. The returned duration is conservative (>= one second when blocked) so that a Retry-After header always communicates a meaningful, RFC 7231 §7.1.3-compatible delay.

func (*Limiter) Cleanup

func (l *Limiter) Cleanup()

Cleanup removes expired entries for all keys. Call periodically to prevent memory growth.

Jump to

Keyboard shortcuts

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