connlimit

package
v1.0.310 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 3 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 connlimit provides per-IP connection limiting. It prevents a single client IP from consuming all proxy resources via many concurrent connections (e.g. HTTP flood, slow-read attacks). It is a self-contained leaf (stdlib only, no Culvert coupling) extracted from the flat package main per ADR-0002.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnLimiter

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

ConnLimiter tracks active connections per client IP.

maxPerIP is atomic rather than lock-guarded: with the counters sharded there is no single lock left that could serialise it against Acquire, and none is needed. The cap is only ever read to make a point-in-time admit/reject comparison, and Enable never touches a counter, so an Enable interleaved with an Acquire has always been able to land on either side of it. What the lock DID protect — the counter increment against Release's delete (the TOCTOU note on Acquire) — is preserved exactly, per shard.

func New

func New() *ConnLimiter

New returns a ConnLimiter with the default per-IP cap, initially disabled.

func (*ConnLimiter) Acquire

func (cl *ConnLimiter) Acquire(ip string) bool

Acquire records a connection from ip and reports whether it is admitted (false ⇒ the per-IP limit is exceeded and the caller should reject).

The per-IP counter is ALWAYS maintained, even while the limiter is disabled; the enabled flag gates only the rejection decision, not the accounting. This keeps Acquire and Release symmetric across a runtime disable/re-enable: every admitted connection is counted exactly once and released exactly once. If Acquire skipped counting while disabled (the historical behavior), a connection admitted during the disabled window would later be Released unconditionally and decrement a DIFFERENT, still-counted connection's slot — letting a subsequent re-enable admit past the cap (fail-open). Conversely, gating Release on enabled leaks the slot of a connection counted while enabled and released after disable, wedging that IP over-limit forever (the #503 fail-closed bug). Counting unconditionally closes both.

func (*ConnLimiter) ActiveConns

func (cl *ConnLimiter) ActiveConns(ip string) int64

ActiveConns returns the current connection count for an IP (testing).

func (*ConnLimiter) ActiveIPs

func (cl *ConnLimiter) ActiveIPs() int

ActiveIPs returns the number of IPs currently tracked.

It is a diagnostic gauge — the admin API's activeIPs field (ui_config.go) and the tests, never the request path — and with the counters sharded it is a sum of per-shard snapshots rather than one instantaneous whole-map reading: under concurrent traffic it can land between two consistent states. Quiescent readings, every Acquire paired with its Release, stay exact. That is the one behavioural difference the shard split makes, and it is confined here.

It touches all 64 shard locks, but each is held only for a len() read, so an admin poll costs the request path nothing measurable.

func (*ConnLimiter) Disable

func (cl *ConnLimiter) Disable()

Disable turns off connection limiting.

func (*ConnLimiter) Enable

func (cl *ConnLimiter) Enable(maxPerIP int)

Enable turns on connection limiting.

func (*ConnLimiter) Enabled

func (cl *ConnLimiter) Enabled() bool

Enabled reports whether connection limiting is currently active.

func (*ConnLimiter) MaxPerIP

func (cl *ConnLimiter) MaxPerIP() int

MaxPerIP returns the current per-IP limit.

func (*ConnLimiter) Rejected added in v1.0.187

func (cl *ConnLimiter) Rejected() int64

Rejected returns the cumulative count of connections refused because the client IP was over its per-IP cap (process lifetime; never reset). This is the only signal an admin has that a configured limit is actually rejecting live traffic rather than sitting unused — the reject path (proxy.go, socks5.go) has no other counter or metric.

func (*ConnLimiter) Release

func (cl *ConnLimiter) Release(ip string)

Release decrements the connection count for ip. It does NOT gate on enabled: Acquire counts every admitted connection unconditionally (see its doc), so Release must mirror that exactly. The decrement is guarded by map-entry presence, and the delete-at-one prevents underflow, so releasing an IP with no live count is a safe no-op.

ONE CRITICAL SECTION, and that is a correctness property before it is a cost one. Release used to take the shard lock TWICE — once to look the counter up, then again to delete the entry once an UNLOCKED decrement had taken it to zero — and the second pass re-checked the counter's VALUE without re-checking its IDENTITY. Acquire's reject path had exactly that identity guard (`cur == ctr`), so the pattern was known and applied in one of the two places that needed it.

What the gap permitted (reproduced deterministically by TestRelease_LegacyTwoPhaseLosesALiveSlot, which drives the interleaving rather than racing for it): Release A reads the counter, unlocks, decrements 1 -> 0 and is descheduled. Acquire B finds the still-mapped counter, takes it 0 -> 1; Release B takes it back to 0 and deletes the entry. Acquire C then misses, allocates a FRESH counter, maps it, and takes it to 1 — a live, admitted connection. A now re-locks, loads its STALE pointer, sees 0, and deletes whatever is at ip — which is C's entry, count 1. C's accounting is destroyed, so the IP goes on to hold limit+1 concurrent connections: a fail-OPEN past a configured per-IP cap, the same direction as the #503 bug the unconditional-accounting contract above exists to close.

Doing the lookup, the decrement and the conditional delete in one critical section removes the window outright. There is no longer a pointer that can go stale, no unlocked mutation of a count the lock is supposed to serialise, and no value-vs-identity re-check to get wrong — and it halves this function's lock traffic, which is the dominant cost when traffic arrives from a single NAT egress (see the package header).

Jump to

Keyboard shortcuts

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