pool

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 5 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 pool keeps connections by key (scheme, host and port, partition, proxy): one shared HTTP/2 or HTTP/3 connection, idle HTTP/1.1 ones, the dial in progress and the requests waiting for a slot under Config.MaxExclusive. Idle connections, shared ones included, are closed after Config.IdleTimeout, unless it is negative.

The pool never dials and never holds its lock across network I/O; see decisions 0020, 0023 and 0040.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Partition

func Partition(ctx context.Context) string

Partition returns the partition ctx asks for, or "".

func WithPartition

func WithPartition(ctx context.Context, p string) context.Context

WithPartition returns ctx asking for connections of partition p only; partitions never share connections (decision 0011). Empty is the default partition.

Types

type Config

type Config struct {
	// Clock times idle connections and evictions.
	Clock clockwork.Clock
	// IdleTimeout is how long an idle connection is kept: an exclusive one
	// since its release, a shared one since its last request ended.
	// Negative: idle connections are never closed for their idleness.
	IdleTimeout time.Duration
	// KeyTimeout is how long a key with no connections keeps what the pool
	// learned about it (that its server speaks HTTP/1.1 only).
	KeyTimeout time.Duration
	// MaxExclusive caps a key's exclusive connections (in use, idle or
	// dialing) once its server is known to be HTTP/1.1-only: a browser's
	// per-host limit. Further requests wait in order. Zero: no limit.
	MaxExclusive int
	// OnWait, when set, is called with a key when a Get starts waiting. For
	// tests.
	OnWait func(key string)
}

Config configures a Pool.

type Dial

type Dial[S Shared, E Exclusive] struct {
	// contains filtered or unexported fields
}

Dial is a dial Get asked for. The caller reports its outcome once: Shared, Adopt, Exclusive or Fail.

func (*Dial[S, E]) Adopt

func (d *Dial[S, E]) Adopt(c S) S

Adopt makes c, another key's shared connection, this key's too (coalescing) and returns the connection to send on: c, or one another dial installed first. c is never closed.

func (*Dial[S, E]) Exclusive

func (d *Dial[S, E]) Exclusive()

Exclusive records that the dial produced an exclusive connection, handed back with Release; the key's requests now dial in parallel.

func (*Dial[S, E]) Fail

func (d *Dial[S, E]) Fail(err error)

Fail fails the dial's waiters with err, unless the dialing request's own context ended, in which case the oldest waiter dials again.

func (*Dial[S, E]) MayShare

func (d *Dial[S, E]) MayShare() bool

MayShare reports whether other requests wait for this dial (the server is not known to be HTTP/1.1-only), so its connection may be shared or another key's adopted.

func (*Dial[S, E]) Shared

func (d *Dial[S, E]) Shared(c S) S

Shared makes c, which the dial produced, the key's shared connection and returns the connection to send on: c, or the shared connection another dial installed first, in which case c is closed.

type Exclusive

type Exclusive interface {
	comparable
	// Usable reports, without blocking, whether an idle connection can
	// carry another request: the peer has not closed it or sent anything.
	Usable() bool
	Close() error
}

Exclusive is a connection that carries one request at a time (HTTP/1.1).

type Found

type Found int

Found is what Get found for a request.

const (
	// FoundShared is the key's shared connection, to send on.
	FoundShared Found = iota
	// FoundExclusive is an idle exclusive connection, now the caller's:
	// it hands it back with Release.
	FoundExclusive
	// FoundDial asks the caller to dial and report with the Dial's methods.
	FoundDial
)

Values of Found.

type Got

type Got[S Shared, E Exclusive] struct {
	Found     Found
	Shared    S
	Exclusive E
	Dial      *Dial[S, E]
}

Got is a Get's result: Shared, Exclusive or Dial, as Found says.

type Pool

type Pool[S Shared, E Exclusive] struct {
	// contains filtered or unexported fields
}

Pool keeps connections by key: at most one shared connection, idle exclusive ones, the dial in progress and the waiting requests. Closing and Usable run with the lock released. Safe for concurrent use.

func New

func New[S Shared, E Exclusive](cfg Config) *Pool[S, E]

New returns an empty pool.

func (*Pool[S, E]) CloseIdle

func (p *Pool[S, E]) CloseIdle()

CloseIdle closes idle exclusive connections and shared ones with no requests in flight, each once.

func (*Pool[S, E]) ExpireKey

func (p *Pool[S, E]) ExpireKey(key string) bool

ExpireKey closes key's idle connections now, as their idle timeout would (Close for exclusive ones, Expire for the shared one), and reports whether key still has connections or dials in use, which a later ExpireKey may close once idle.

func (*Pool[S, E]) Get

func (p *Pool[S, E]) Get(ctx context.Context, key string) (Got[S, E], error)

Get finds a connection for key: the shared one if it can take the request, else the most recently used usable idle exclusive one, else a dial. Until the server is known to be HTTP/1.1-only, concurrent requests share one dial and its result, error included; after, they dial in parallel up to Config.MaxExclusive, then wait in order. Get returns ctx's error if ctx ends while waiting.

func (*Pool[S, E]) Peek

func (p *Pool[S, E]) Peek(key string) Found

Peek reports what Get would find for key now (FoundShared, FoundExclusive or FoundDial) without taking, waiting, dialing or changing anything.

func (*Pool[S, E]) Release

func (p *Pool[S, E]) Release(key string, c E, reusable bool)

Release hands back an exclusive connection Get or a Dial gave: to the oldest waiting request, or idle for the next one, if reusable; else closed, and the oldest waiting request may dial in its place.

func (*Pool[S, E]) RemoveShared

func (p *Pool[S, E]) RemoveShared(key string, c S)

RemoveShared unpools c if it is key's shared connection, without closing it: in-flight requests go on, and c closes itself after them.

func (*Pool[S, E]) Shared

func (p *Pool[S, E]) Shared(key string) (S, bool)

Shared returns key's shared connection if it can take a new request, without waiting or dialing (for WebSockets, which otherwise dial outside the pool).

func (*Pool[S, E]) SharedConns

func (p *Pool[S, E]) SharedConns() []S

SharedConns returns the shared connections that can take a new request, each once, for a caller looking for one to adopt.

type Shared

type Shared interface {
	comparable
	// CanTakeNewRequest reports whether a new request may start on it.
	CanTakeNewRequest() bool
	// IdleSince reports when its last request ended, and false while it
	// has requests in flight.
	IdleSince() (time.Time, bool)
	// OnClose runs f once the connection has closed, for any reason: at
	// once, on the caller's goroutine, if it already has. f runs with none
	// of the connection's locks held; several may be registered.
	OnClose(f func())
	Close() error
	// Expire closes it, idle, as its browser lets go of a connection it
	// no longer needs: after IdleTimeout, or for ExpireKey.
	Expire()
}

Shared is a connection that carries many requests at once (HTTP/2, HTTP/3).

Jump to

Keyboard shortcuts

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