ratelimit

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 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) AllowSnapshot

func (l *Limiter) AllowSnapshot(key string) (ok bool, retryAfter time.Duration, limit, remaining, resetSeconds int)

AllowSnapshot behaves like AllowWithRetryAfter but also returns the IETF RateLimit header values: the window quota (limit), the remaining quota after this request, and the seconds until the window resets (when the oldest in-window hit ages out). resetSeconds is >= 1 whenever any hits are recorded.

func (*Limiter) AllowWithRetryAfter

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.

func (*Limiter) SetMax

func (l *Limiter) SetMax(max int)

SetMax updates the request budget without replacing the limiter, preserving its cleanup goroutine and any existing per-key window state.

Jump to

Keyboard shortcuts

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