connadmit

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 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 connadmit admits connections to an HTTP listener: up to a cap they are served, past it they wait in a bounded FIFO for a slot for at most a fixed time, and the rest are answered with a 503 and closed. It accepts from the kernel continuously, so a connection flood never sits unanswered in the accept backlog, where SYN drops turn into silent client retransmits.

Scope: the listener wrapper, the connection it hands to Serve, and an http.Server.ConnState hook that tracks idle keep-alive connections so one can be closed for a queued connection. Out of scope: request-level admission (in-flight caps and load shedding live above it), TLS, and shaping the refusal per API: the conn layer has not routed the request, so every refusal carries the one response it was given.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// MaxConns is the most connections handed to Serve at once; must be positive.
	MaxConns int
	// QueueSize is the most connections waiting for a slot; 0 refuses every connection past MaxConns.
	QueueSize int
	// QueueTimeout is how long a connection waits for a slot before it is refused.
	QueueTimeout time.Duration
	// Body is the JSON body of the 503 refusal.
	Body []byte
	// RetryAfter is the refusal's Retry-After value.
	RetryAfter string
	// Header holds extra headers of the refusal.
	Header http.Header
	// CloseIdle closes the oldest keep-alive connection idle for minIdleAge when a connection is queued, and needs ConnState set on the server. Off, a client's spare keep-alive connections stay open: closing them makes it dial again on its next burst, which feeds a reconnect herd.
	CloseIdle bool
}

Config sets the listener's limits and its refusal.

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

Conn is a connection holding one of the listener's slots until it is closed.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection and frees its slot; later calls only close.

func (*Conn) CloseWrite

func (c *Conn) CloseWrite() error

CloseWrite half-closes the connection when the inner one supports it, which net/http uses to avoid resetting a closing connection.

type Listener

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

Listener wraps a net.Listener with connection admission. Accept returns *Conn values.

func New

func New(inner net.Listener, cfg Config) *Listener

New starts accepting from inner. The caller passes the Listener to http.Server.Serve and sets the server's ConnState to l.ConnState.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept returns the next connection holding a slot. After Close, or once the inner listener fails, it returns the inner listener's error.

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the inner listener's address.

func (*Listener) Close

func (l *Listener) Close() error

Close stops accepting and closes the queued connections and those not yet handed to Serve; served ones stay open for the server to close.

func (*Listener) ConnState

func (l *Listener) ConnState(nc net.Conn, state http.ConnState)

ConnState tracks idle keep-alive connections; set it as the http.Server's ConnState.

Jump to

Keyboard shortcuts

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