Help us shape the future of Go! Take the 2026 Go Developer Survey

connlimit

package
v1.150.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package connlimit holds a connection to the number of calls its upstream can take at once (#2209): a connection's max_concurrent_calls, across every replica of the deployment.

Some upstreams cannot serve parallel requests. Twelve runs of one script started together against such an endpoint all failed, and nothing on the platform could say "this upstream takes one request at a time". A limited connection's every request now takes a slot first and gives it back when its response has been read, whatever sent it: an interactive tool call, an export's page walk, a script's platform.call.

A slot is a PostgreSQL session-level advisory lock, (connection, slot n), held on the process's lock session (internal/advisorylock) for the length of the call, so the limit holds across replicas: an upstream that takes one request does not take three because the deployment runs three replicas. A call over the limit waits for a slot instead of failing, and the wait counts against the call's own deadline; a call whose deadline passes while it waits fails naming the limit and how long it waited. A deployment with no database holds the limit in this process alone.

Index

Constants

View Source
const Ceiling = 32

Ceiling bounds a connection's max_concurrent_calls. Each call holding a slot holds a database connection for its length, so the limit is for an upstream that takes a few calls at once, not a throughput setting.

Variables

This section is empty.

Functions

func Install

func Install(l *Limiter)

Install makes l the limiter every limited transport takes its slots from. A transport built before Install takes them from it all the same: it reads the installed limiter on every request.

func Parse

func Parse(raw any) (int, error)

Parse reads a configured max_concurrent_calls, absent or nil being unlimited. A value that is not a whole number is refused rather than read as unlimited: a limit an operator set and the platform silently ignored is an upstream flooded by the calls the limit was set to hold back.

func Transport

func Transport(base http.RoundTripper, kind, connection string, limit int) http.RoundTripper

Transport is base with every request taking one of the connection's limit slots before it is sent and giving it back once its response body is read to the end or closed, which is when the upstream is done with the call. A limit of zero or less is base unchanged: an unlimited connection, the default.

Types

type Limiter

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

Limiter hands out a connection's slots.

func Default

func Default() *Limiter

Default is the installed limiter, or one that holds limits in this process until one is installed.

func New

func New(db *sql.DB, metrics func() *observability.Metrics) *Limiter

New builds a Limiter over db. A nil db holds each limit in this process.

func (*Limiter) Acquire

func (l *Limiter) Acquire(ctx context.Context, kind, connection string, limit int) (*Slot, time.Duration, error)

Acquire takes one of a connection's limit slots, waiting for one to free, until ctx ends. Waited is how long it waited.

type Slot

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

Slot is one held slot. Release gives it back; it is safe to call more than once.

func (*Slot) Release

func (s *Slot) Release()

Release gives the slot back.

type WaitError

type WaitError struct {
	Connection string
	Limit      int
	Waited     time.Duration
	Err        error
}

WaitError is a call that ended before a slot was free, or whose slot could not be taken. Its text names the limit and how long the call waited, which is what an author whose calls queue behind each other needs to read.

func (*WaitError) Error

func (e *WaitError) Error() string

func (*WaitError) Unwrap

func (e *WaitError) Unwrap() error

Jump to

Keyboard shortcuts

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