limits

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package limits implements the relay's two rate controls: a per-pair byte budget (protects one pair's bandwidth from starving others sharing the host) and a per-IP connection-attempt budget (bounds the cost of unauthenticated upgrade attempts before a pair even exists). See the build plan's Step 2 — these are deliberately simple token buckets, not a distributed rate limiter; the relay is a single process per deploy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

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

Bucket is a per-pair byte-rate limiter. Burst is set to one second's worth of the configured rate, which is generous enough not to choke a single large NDJSON chunk while still bounding sustained throughput.

func NewBucket

func NewBucket(bytesPerSec int64) *Bucket

NewBucket builds a Bucket allowing bytesPerSec sustained, bursting up to bytesPerSec in one go (i.e. a single very large frame at MaxFrameBytes still gets through as long as it's under the burst, rather than being rejected outright).

func (*Bucket) AllowN

func (b *Bucket) AllowN(n int) bool

AllowN reports whether n bytes may be admitted right now. It does not block — the relay's forwarding pump treats a rate-limit hit as a protocol violation (close the connection), not something to queue behind, per the "no buffering" backpressure design.

type IPLimiter

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

IPLimiter bounds new-connection attempts per source IP, independent of whether a pair ends up existing — this is what keeps an unauthenticated flood of upgrade requests cheap to reject.

func NewIPLimiter

func NewIPLimiter(perMin int) *IPLimiter

NewIPLimiter builds a limiter allowing perMin connection attempts per minute per source IP, with a burst equal to perMin (so a legitimate client reconnecting a few times in quick succession after a network blip isn't punished).

func (*IPLimiter) Allow

func (l *IPLimiter) Allow(ip netip.Addr) bool

Allow reports whether ip may attempt a new connection now, lazily creating that IP's bucket on first sight.

func (*IPLimiter) Len

func (l *IPLimiter) Len() int

Len reports the number of tracked IPs — exposed for tests and metrics.

func (*IPLimiter) Sweep

func (l *IPLimiter) Sweep(maxAge time.Duration)

Sweep removes entries not seen for longer than maxAge, bounding memory growth from a churn of distinct source IPs over a long-running process. Call periodically (e.g. every few minutes) from a janitor goroutine — not on every request, to keep Allow() cheap.

Jump to

Keyboard shortcuts

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