ratelimit

package
v1.36.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package ratelimit limits requests per key with a counter kept in memory.

The counters live in the process, so each instance of an app counts alone: behind a load balancer a client can get the limit once per instance, up to N times the limit with N instances. A limit that has to hold across instances belongs in the database.

Index

Constants

View Source
const (
	DEFAULT_WINDOW = 60
	DEFAULT_LIMIT  = 60
)

Variables

This section is empty.

Functions

This section is empty.

Types

type UserCounter

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

type WindowCounter

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

WindowCounter implements a simple window counter

func NewWindowCounter

func NewWindowCounter(windowSeconds, limit int) *WindowCounter

NewWindowCounter returns a limiter that allows limit requests per key every windowSeconds. Non-positive values fall back to DEFAULT_WINDOW and DEFAULT_LIMIT. A window longer than a time.Duration can hold, about 292 years, is clamped to the longest one it can.

func (*WindowCounter) Allow

func (rl *WindowCounter) Allow(userID string) bool

Allow charges one request to userID and reports whether it fits in the current window.

func (*WindowCounter) Exhausted added in v1.17.2

func (rl *WindowCounter) Exhausted(userID string) bool

Exhausted reports whether userID has spent its limit in the current window without charging it. It never creates or extends a counter, so peeking with a caller-supplied key cannot grow the map or prolong a lockout.

Jump to

Keyboard shortcuts

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