proxy

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package proxy holds the forwarding, admission, and serving code shared by proxies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReverseProxy

func NewReverseProxy(target *netaddr.Endpoint, transport http.RoundTripper, log *slog.Logger, discard bool) *httputil.ReverseProxy

NewReverseProxy forwards requests to target with trusted forwarding headers. A discarding proxy logs failures as candidate failures and writes no response.

func Serve

func Serve(ctx context.Context, config Config, log *slog.Logger, bindings []Binding, lifecycle Lifecycle) error

Serve runs every binding until the context is cancelled or any server fails, then shuts all of them down before draining work that outlives requests.

Types

type Binding

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

Binding pairs a listener with the handler that serves its connections.

func NewBinding

func NewBinding(listener net.Listener, handler http.Handler) Binding

NewBinding serves connections accepted by listener with handler.

type Budget

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

Budget caps buffered memory shared across every in-flight request. Reservations never block, so a full budget fails immediately.

func NewBudget

func NewBudget(limit int) *Budget

NewBudget returns a budget that allows at most limit reserved bytes.

func (*Budget) Limit

func (b *Budget) Limit() int

Limit reports the total number of bytes the budget allows.

func (*Budget) Release

func (b *Budget) Release(size int)

Release returns size previously reserved bytes to the budget. Releasing more than is reserved panics.

func (*Budget) Reserve

func (b *Budget) Reserve(size int) (reserved bool)

Reserve claims size bytes and reports whether they fit in the remaining budget.

type CandidateRun

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

CandidateRun is one admitted unit of candidate work. Its pointer gives the non-comparable cancellation function a stable map identity.

type Candidates

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

Candidates admits candidate work and tracks it so quarantine and shutdown can cancel everything in flight. Quarantine lasts for the process lifetime.

func NewCandidates

func NewCandidates(maxInFlight int, log *slog.Logger) *Candidates

NewCandidates tracks candidate work, quarantining beyond maxInFlight concurrent runs.

func (*Candidates) Finish

func (c *Candidates) Finish(run *CandidateRun)

Finish releases an admitted run once all of its work has stopped.

func (*Candidates) Quarantine

func (c *Candidates) Quarantine(ctx context.Context, reason error)

Quarantine refuses all further candidate work and cancels active runs.

func (*Candidates) Shutdown

func (c *Candidates) Shutdown(ctx context.Context) error

Shutdown refuses further candidate work and waits for active runs to finish.

func (*Candidates) Start

func (c *Candidates) Start(ctx context.Context, cancel context.CancelFunc) *CandidateRun

Start admits candidate work that cancel stops, returning nil when it is refused. Exceeding the concurrency limit quarantines the candidate.

type Config

type Config struct {
	// MaxInFlightRequests limits concurrent proxied requests.
	MaxInFlightRequests int `default:"256" help:"Maximum concurrent proxied requests."`
	// MaxConnections limits concurrent client connections.
	MaxConnections int `default:"512" help:"Maximum concurrent client connections."`
	// ReadHeaderTimeout limits the duration spent reading request headers.
	ReadHeaderTimeout time.Duration `default:"10s" help:"Maximum duration for reading request headers."`
	// IdleTimeout limits how long an idle client connection remains open.
	IdleTimeout time.Duration `default:"1m" help:"Maximum idle client connection duration."`
	// MaxHeaderBytes limits the size of request and response headers.
	MaxHeaderBytes int `default:"65536" help:"Maximum request header size in bytes."`
	// ShutdownTimeout limits graceful server shutdown.
	ShutdownTimeout time.Duration `default:"10s" help:"Maximum graceful shutdown duration."`
}

Config contains the server and forwarding limits shared by every proxy.

func NewConfig

func NewConfig() Config

NewConfig returns the default proxy configuration.

func (Config) Validate

func (c Config) Validate() error

Validate checks that every limit is positive.

type Lifecycle

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

Lifecycle holds the proxy-specific steps that Serve runs around its servers.

func NewLifecycle

func NewLifecycle(start func(ctx context.Context), stop func(), drain func(ctx context.Context) error) Lifecycle

NewLifecycle runs start once every listener accepts connections, stop before servers shut down, and drain after they have stopped.

type Transport

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

Transport forwards HTTP/1, encrypted HTTP/2, and unencrypted HTTP/2 requests.

func NewTransport

func NewTransport(config Config) *Transport

NewTransport constructs transports for HTTP/1, encrypted HTTP/2, and h2c.

func (*Transport) CloseIdleConnections

func (t *Transport) CloseIdleConnections()

CloseIdleConnections closes idle connections held by every underlying transport.

func (*Transport) ForBackend

func (t *Transport) ForBackend(target *netaddr.Endpoint) http.RoundTripper

ForBackend selects the transport for a backend, dialing its unix socket when the backend uses one.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(request *http.Request) (*http.Response, error)

RoundTrip sends HTTP requests without deriving backend protocol from the client.

Jump to

Keyboard shortcuts

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