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

rate

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 8 Imported by: 5

Documentation

Overview

Package rate provides rate limiting functionality at a global, network, and subnet level.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limit

type Limit struct {
	// RPS is the rate of requests per second in steady state.
	RPS float64
	// Burst is the number of requests allowed over the RPS.
	Burst int
}

Limit is the configuration for a token bucket rate limiter. The bucket has a capacity of Burst, and is refilled at a rate of RPS tokens per second. Initially, buckets are completley full, i.e. tokens in the bucket is equal to `Burst`. In any given time interval T seconds, maximum events allowed will be `T*RPS + Burst`.

type Limiter

type Limiter struct {
	// NetworkPrefixLimits are limits for streams with peer IPs belonging to specific subnets.
	// It can be used to increase the limit for trusted networks and decrease the limit for specific networks.
	NetworkPrefixLimits []PrefixLimit
	// GlobalLimit is the limit for all streams where the peer IP doesn't fall within any
	// of the `NetworkPrefixLimits`
	GlobalLimit Limit
	// SubnetRateLimiter is a rate limiter for subnets.
	SubnetRateLimiter SubnetLimiter
	// contains filtered or unexported fields
}

Limiter rate limits new streams for a service. It allows setting NetworkPrefix specific, global, and subnet specific limits. Use 0 for no rate limiting. The limiter maintains state that must be periodically cleaned up using Cleanup

func (*Limiter) Allow

func (r *Limiter) Allow(ipAddr netip.Addr) bool

Allow returns true if requests for `ipAddr` are within specified rate limits

func (*Limiter) Limit

func (r *Limiter) Limit(f func(s network.Stream)) func(s network.Stream)

Limit rate limits a StreamHandler function.

type PrefixLimit

type PrefixLimit struct {
	Prefix netip.Prefix
	Limit
}

PrefixLimit is a rate limit configuration that applies to a specific network prefix.

type SubnetLimit

type SubnetLimit struct {
	PrefixLength int
	Limit
}

SubnetLimit is a rate limit configuration that applies to a specific subnet.

type SubnetLimiter

type SubnetLimiter struct {
	// IPv4SubnetLimits are the per subnet limits for streams with IPv4 Peers.
	IPv4SubnetLimits []SubnetLimit
	// IPv6SubnetLimits are the per subnet limits for streams with IPv6 Peers.
	IPv6SubnetLimits []SubnetLimit
	// GracePeriod is the time to wait to remove a full capacity bucket.
	// Keeping a bucket around helps prevent allocations
	GracePeriod time.Duration
	// contains filtered or unexported fields
}

SubnetLimiter rate limits requests per ip subnet.

func (*SubnetLimiter) Allow

func (s *SubnetLimiter) Allow(ipAddr netip.Addr, now time.Time) bool

Allow returns true if requests for `ipAddr` are within specified rate limits

Jump to

Keyboard shortcuts

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