limiter

package
v2.6.7 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package limiter provides a distributed rate limiting abstraction built on a Maker/Limiter pair.

A Maker creates Limiter instances for named scenes, and a Limiter decides whether requests identified by a dimension key are allowed. The package holds a global maker so that limiters can be created through Make without passing the maker around explicitly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close closes the global maker. It does nothing when no maker has been set.

func SetMaker

func SetMaker(maker Maker)

SetMaker sets the global limiter maker. It closes the previous maker, if any, before replacing it.

Types

type FailurePolicy

type FailurePolicy int

FailurePolicy determines how a backend behaves when it cannot make a decision.

const (
	// FailOpen allows the request when the backend is unavailable.
	FailOpen FailurePolicy = iota + 1
	// FailClosed rejects the request when the backend is unavailable.
	FailClosed
)

type Limiter

type Limiter interface {
	// Allow takes n tokens from the bucket identified by key and reports the decision. n defaults
	// to 1. A denied request is not an error: it is reported by Result.Allowed.
	Allow(ctx context.Context, key string, n ...int) (Result, error)
}

Limiter decides whether requests identified by key are allowed for a scene.

func Make

func Make(name string, rule Rule) (Limiter, error)

Make creates a limiter for the named scene and rule through the global maker. It returns errors.ErrMissingLimiterInstance when no maker has been set.

type Maker

type Maker interface {
	// Make creates a limiter for the named scene and rule.
	Make(name string, rule Rule) (Limiter, error)
	// Close closes the maker.
	Close() error
}

Maker creates and closes limiters.

func GetMaker

func GetMaker() Maker

GetMaker returns the global limiter maker.

type Result

type Result struct {
	Allowed    bool          // Allowed reports whether the request was allowed.
	Limit      int           // Limit is the bucket capacity.
	Remaining  int           // Remaining is the number of tokens left after the decision.
	RetryAfter time.Duration // RetryAfter is the suggested wait time when the request is denied.
	ResetAfter time.Duration // ResetAfter is the time needed to refill the bucket.
	Degraded   bool          // Degraded reports whether the request was allowed by fail-open.
}

Result reports the outcome of a rate limiting decision.

type Rule

type Rule struct {
	Quota  float64       // Quota is the number of requests allowed per Period.
	Period time.Duration // Period is the quota period; it defaults to one second.
	Burst  int           // Burst is the bucket capacity; it defaults to ceil(Quota).
}

Rule describes a rate limiting rule.

Quota is the number of requests allowed per Period. Period defaults to one second and Burst defaults to ceil(Quota), so a zero Burst allows a burst equal to the quota.

func (Rule) Normalize

func (r Rule) Normalize() (Rule, error)

Normalize validates the rule and fills in its defaults.

func (Rule) Rate

func (r Rule) Rate() float64

Rate returns the refill rate in tokens per second.

It must be called on a normalized rule.

func (Rule) TTL

func (r Rule) TTL() time.Duration

TTL returns the key expiration for the rule.

The expiration covers the time needed to refill an empty bucket plus one full period, so that an idle key expires only after the bucket would have been full again.

Directories

Path Synopsis
Package memory provides an in-process limiter backend.
Package memory provides an in-process limiter backend.

Jump to

Keyboard shortcuts

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