package rate
import "github.com/libp2p/go-libp2p/x/rate"
Package rate provides rate limiting functionality at a global, network, and subnet level.
Index ¶
- type Limit
- type Limiter
- func (r *Limiter) Allow(ipAddr netip.Addr) bool
- func (r *Limiter) Limit(f func(s network.Stream)) func(s network.Stream)
- type PrefixLimit
- type SubnetLimit
- type SubnetLimiter
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 ¶
Allow returns true if requests for `ipAddr` are within specified rate limits
func (*Limiter) Limit ¶
Limit rate limits a StreamHandler function.
type PrefixLimit ¶
PrefixLimit is a rate limit configuration that applies to a specific network prefix.
type SubnetLimit ¶
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 ¶
Allow returns true if requests for `ipAddr` are within specified rate limits
Source Files ¶
- Version
- v0.42.0 (latest)
- Published
- Jun 18, 2025
- Platform
- linux/amd64
- Imports
- 8 packages
- Last checked
- 4 weeks ago –
Tools for package owners.