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

limiter

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package limiter bounds how fast, or how many at once, requests reach downstream handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Concurrency

func Concurrency(limit int) zinc.Middleware

Concurrency bounds the requests running downstream at once to limit and answers 429 to any beyond it, rather than queueing them.

func Doc added in v0.7.1

func Doc() zinc.MiddlewareDoc

Doc describes the middleware for the OpenAPI spec: a request over the limit is answered with 429, with Retry-After saying how many seconds until it would be allowed. Pass it to Document beside the middleware.

func New

func New(configs ...Config) zinc.Middleware

New returns token-bucket rate limiting. With Key set, it keeps a bounded set of buckets; at MaxKeys it rejects new keys rather than evicting active quotas. No cleanup goroutine is required.

Types

type Config

type Config struct {
	// Rate is the token refill rate per second. Zero uses 10.
	Rate float64
	// Capacity is the largest burst allowed. Zero uses 10.
	Capacity float64
	// Key buckets requests by the string it returns, such as c.IP(). Nil
	// applies one bucket to every request.
	Key func(*zinc.Context) string
	// LimitReached answers a request over the limit. Nil returns a 429 error.
	LimitReached zinc.HandlerFunc
	// MaxKeys bounds keyed buckets; zero uses 10,000. New keys are denied at capacity.
	MaxKeys int
	// MaxKeyBytes bounds retained key sizes; zero uses 256.
	MaxKeyBytes int
	// IdleTTL is the minimum idle time before a fully refilled bucket expires.
	// Zero uses five minutes. Active or depleted buckets are never evicted.
	IdleTTL time.Duration
	// Now supplies the clock; nil uses time.Now.
	Now func() time.Time
}

Config sets the token-bucket policy. The zero value allows 10 requests per second with bursts of 10, shared by all clients.

Jump to

Keyboard shortcuts

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