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 ¶
- func Partition(ctx context.Context) string
- func WithPartition(ctx context.Context, p string) context.Context
- type Config
- type Dial
- type Exclusive
- type Found
- type Got
- type Pool
- func (p *Pool[S, E]) CloseIdle()
- func (p *Pool[S, E]) ExpireKey(key string) bool
- func (p *Pool[S, E]) Get(ctx context.Context, key string) (Got[S, E], error)
- func (p *Pool[S, E]) Peek(key string) Found
- func (p *Pool[S, E]) Release(key string, c E, reusable bool)
- func (p *Pool[S, E]) RemoveShared(key string, c S)
- func (p *Pool[S, E]) Shared(key string) (S, bool)
- func (p *Pool[S, E]) SharedConns() []S
- type Shared
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
Fail fails the dial's waiters with err, unless the dialing request's own context ended, in which case the oldest waiter dials again.
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 Pool ¶
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 (*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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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() bool
// 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.
// no longer needs: after IdleTimeout, or for ExpireKey.
Expire()
}
Shared is a connection that carries many requests at once (HTTP/2, HTTP/3).