Net.HTTP.H1.Pool

lib/Net/HTTP/H1/Pool.gob

A client's connections: plain or TLS, read and written under its limits, and kept between requests in a pool per origin.

Pacing. A Paced connection bounds each read and write by two limits: the request's total deadline, if it has one, and the stall limit, measured from when the operation began or the last time a byte moved in either direction, whichever is later. So the limit fires only on a peer that stopped reading or sending, never on a caller that waited between reads. A request body is written in one task while the response is read in another, so a long upload must not stall out the read that waits for the response's head: a read that times out while the other direction moved is tried again, with the stall measured from that movement. A read that times out has taken no byte, so trying it again loses nothing. A write is never tried again, since one that timed out may have sent part of its buffer.

Carrying. Once a connection switches protocols, carry bounds each read and write by the deadline the new protocol sets, or by nothing, and by neither of the two limits: the protocol keeps its own time, and a connection waiting for its peer's next message has not stalled.

A link is a connection with what it keeps from one exchange to the next: its Paced, reset for each exchange, and the Input its responses are read from. The input's array is reused until heads fill it, rather than one made, and zero-filled, for every exchange; header values that view it stay valid, since Input never writes over a head it handed out.

The pool keeps idle connections by origin -- scheme, host and port -- at most perOrigin of each and total in all, for at most idle each. A connection is given back only by a caller that read its last message to the exact end; this module trusts that. One taken out is the caller's alone until it is given back or closed.

Every take and give sweeps the whole pool, closing each connection idle too long and dropping origins left with none. Sweeping only the origin asked for would leak: an origin never asked for again would keep its connections, and a busy one would keep those below its freshest, since take stops at the first fresh one. Each origin's connections are kept oldest first, so the sweep reads only up to the first fresh one of each. Past total, the oldest connection of any origin is closed.

type Conn = Plain(Socket) | Secure(TLS)

A connection to a server, over TCP or TLS. Its errors are packed into SomeError, so that an IOError and a TLSError travel the same way and cast recovers either.

fun close : fun(Conn) -> Unit

Closes c, ending every read and write parked on it. A failure to close is dropped: the connection is not used again either way.

fun halfClose : fun(Conn) -> Either SomeError Unit

Ends c's output; the server can go on sending.

fun isTimeout : fun(SomeError) -> Bool

Whether e is a read or write that timed out.

fun isStale : fun(SomeError) -> Bool

Whether e is what a stale connection, one the server had already closed, fails with: a reset, a broken pipe, or TLS ending without its close_notify. A timeout is not: the server may be slow rather than gone.

type Paced
fun paced : fun(Conn, Option Deadline, Duration) -> Paced
fun pace : fun(Link, Option Deadline) -> Paced

Returns l's pacing, reset for a new exchange bounded by total: no byte read yet, and the stall measured from now.

fun input : fun(Link) -> Input Paced
fun carry : fun(Paced, Option Deadline) -> Unit

Bounds every later read and write on p by d alone, or by nothing.

fun conn : fun(Paced) -> Conn
fun firstRead : fun(Paced) -> Option Reading

Returns when the first byte was read from p, if one has been.

fun progressed : fun(Paced) -> Unit

Records that a byte moved, as the other direction's limit reads it.

type Pool
type Pooled = Pooled { link : Link, since : Reading }

An idle connection, and when it was given back.

fun pool : fun(Int, Int, Duration) -> Pool
fun take : fun(Pool, String) -> Option Link

Returns an idle connection to origin, the most recently given back, after closing every connection idle too long.

fun give : fun(Pool, String, Link) -> Unit

Keeps l for the next request to origin, after closing every connection idle too long. Closes l instead if the origin has as many idle connections as it may, and the pool's oldest if the pool then has more than total. l's input must hold no byte.

fun closeIdle : fun(Pool) -> Unit

Closes every idle connection.

instance Read Conn
instance Write Conn
instance Read Paced
instance Write Paced