feat: add a pure connection pool driver for the HTTP client
diff --git a/src/Std/Http/Client.lean b/src/Std/Http/Client.lean index 71312cd..75732b9 100644 --- a/src/Std/Http/Client.lean +++ b/src/Std/Http/Client.lean
@@ -13,6 +13,7 @@ public import Std.Http.Client.Interceptor public import Std.Http.Client.Config public import Std.Http.Client.Connection.Driver +public import Std.Http.Client.Pool.Driver public section
diff --git a/src/Std/Http/Client/Pool/Driver.lean b/src/Std/Http/Client/Pool/Driver.lean new file mode 100644 index 0000000..984ec74 --- /dev/null +++ b/src/Std/Http/Client/Pool/Driver.lean
@@ -0,0 +1,378 @@ +/- +Copyright (c) 2026 Lean FRO, LLC. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Sofia Rodrigues +-/ +module + +prelude +public import Init.Data.Queue +public import Std.Http.Client.Config +public import Std.Http.Client.Error + +public section + +/-! +# Pool driver + +The decisions of a client's connection pool, without I/O. For each origin the pool keeps the +connections it opened, which of them are idle, the ones being opened, and the requests waiting for +one. It decides whether a request reuses an idle connection, opens a new one while the origin is +under `Config.maxConnectionsPerOrigin`, or waits; who gets a connection that comes free or a place +under the limit that a closed connection leaves; and what `close` ends. + +The pool is the driver's interpreter. It names each request asking for a connection with a +`Ticket`, reports what happened as the next `Input`, and performs the `Command`s that `step` +returns: opening a connection (`dial`, which names it), handing one to a request (`grant`), failing +a request (`reject`), and closing a connection (`closeConnection`). Every ticket is answered once, +by `grant` or `reject`. + +A connection holds its place under the limit from the `dial` that names it until the interpreter +reports its loop has exited (`connectionClosed`), so the limit also bounds connections being opened +and connections being torn down. A connection that ends on its own, as when its keep-alive timeout +passes or the peer closes it, still finishes its writes before its loop exits; the interpreter +reports `retired` as soon as the connection's driver stops accepting requests, so the pool does not +grant it in the meantime. +-/ + +namespace Std.Http.Client.Pool.Driver + +set_option linter.all true + +open Std Http + +/-- +Names a request waiting for a connection. The interpreter chooses it, unique among the requests +that have not been answered. +-/ +abbrev Ticket := Nat + +/-- +Names a connection. The driver chooses it when it asks for the connection to be opened. +-/ +abbrev ConnectionId := Nat + +/-- +What the interpreter reports to the driver. +-/ +inductive Input where + /-- + A request needs a connection to `origin`. A `fresh` one, used to retry a failed request, never + reuses an idle connection, which may have gone stale the same way. + -/ + | acquire (ticket : Ticket) (origin : URI.Origin) (fresh : Bool) + + /-- + Opening connection `id` ended, with the error that failed it. + -/ + | dialed (id : ConnectionId) (result : Except Client.Error Unit) + + /-- + The request that held connection `id` is done with it. A `reusable` connection can carry another + request; any other is closed. + -/ + | released (id : ConnectionId) (reusable : Bool) + + /-- + Connection `id` takes no further request: it ends on its own, and its loop's exit follows as + `connectionClosed id`. Reported when the connection's driver issues `stopAccepting`, idle or held. + -/ + | retired (id : ConnectionId) + + /-- + The loop of connection `id` exited, however it ended. + -/ + | connectionClosed (id : ConnectionId) + + /-- + The client closes: every connection closes and every request not yet answered fails. The pool + stays usable, and a later `acquire` opens fresh connections. + -/ + | close + +/-- +What the driver asks the interpreter to do. +-/ +inductive Command where + /-- + Open connection `id` to `origin` for `ticket`, then report `dialed id`. + -/ + | dial (ticket : Ticket) (origin : URI.Origin) (id : ConnectionId) + + /-- + Hand connection `id` to `ticket`, which holds it until it reports `released id`. + -/ + | grant (ticket : Ticket) (id : ConnectionId) + + /-- + Fail `ticket` with `err`. + -/ + | reject (ticket : Ticket) (err : Client.Error) + + /-- + Close connection `id`, aborting any exchange on it. Its loop's exit is reported as + `connectionClosed id`. + -/ + | closeConnection (id : ConnectionId) + +/-- +A connection being opened. +-/ +structure Dial where + /-- + The connection's name. + -/ + id : ConnectionId + + /-- + The request it is opened for. + -/ + ticket : Ticket + + /-- + `close` ran since the dial started, and its request was already rejected. + -/ + cancelled : Bool := false + +/-- +The connections of one origin, and the requests waiting for one of them. +-/ +structure OriginPool where + /-- + The origin every connection here was opened for. + -/ + origin : URI.Origin + + /-- + Every connection opened here whose loop has not exited: idle, held by a request, or closing. + -/ + connections : Array ConnectionId := #[] + + /-- + The connections no request holds, most recently released last. + -/ + idle : Array ConnectionId := #[] + + /-- + The connections told to close, which no request gets again. + -/ + closing : Array ConnectionId := #[] + + /-- + The connections being opened. + -/ + dials : Array Dial := #[] + + /-- + The requests waiting for a connection, oldest first. + -/ + waiters : Std.Queue Ticket := .empty + +namespace OriginPool + +/-- +The number of places under `Config.maxConnectionsPerOrigin` the pool holds: connections open or +closing, and connections being opened. +-/ +def size (pool : OriginPool) : Nat := + pool.connections.size + pool.dials.size + +/-- +`true` when the pool holds no connection and no request waits, so the driver can drop it. +-/ +def isUnused (pool : OriginPool) : Bool := + pool.size == 0 && pool.waiters.isEmpty + +/-- +Opens connection `id` for `ticket`, in a place under the limit. +-/ +private def dial (pool : OriginPool) (ticket : Ticket) (id : ConnectionId) : + OriginPool × Array Command := + ({ pool with dials := pool.dials.push { id, ticket } }, #[.dial ticket pool.origin id]) + +/-- +Passes a place under the limit that the pool has just freed to its oldest waiting request, which +opens connection `id` in it. +-/ +private def passPlace (pool : OriginPool) (id : ConnectionId) : OriginPool × Array Command := + match pool.waiters.dequeue? with + | some (ticket, waiters) => { pool with waiters }.dial ticket id + | none => (pool, #[]) + +/-- +Takes the most recently released idle connection, opens connection `id` while under `limit`, or +queues the request. A `fresh` request skips the idle connections, and at the limit closes the least +recently used one to make room: the request waits for a place like any other, since the closing +connection keeps its own until its loop exits. +-/ +private def acquire (limit : Nat) (pool : OriginPool) (ticket : Ticket) (fresh : Bool) + (id : ConnectionId) : OriginPool × Array Command := + match fresh, pool.idle.back? with + | false, some conn => ({ pool with idle := pool.idle.pop }, #[.grant ticket conn]) + | _, _ => + if pool.size < limit then + pool.dial ticket id + else + let pool := { pool with waiters := pool.waiters.enqueue ticket } + match fresh, pool.idle[0]? with + | true, some oldest => + ({ pool with idle := pool.idle.extract 1, closing := pool.closing.push oldest }, + #[.closeConnection oldest]) + | _, _ => (pool, #[]) + +/-- +Grants connection `id`, just opened, to its request. A dial `close` cancelled closes the connection +instead, and a failed one fails its request and passes its place on, to open connection `next`. +-/ +private def dialed (pool : OriginPool) (id : ConnectionId) (result : Except Client.Error Unit) + (next : ConnectionId) : OriginPool × Array Command := + match pool.dials.find? (·.id == id) with + | none => (pool, #[]) + | some dial => + let pool := { pool with dials := pool.dials.filter (·.id != id) } + match result, dial.cancelled with + | .ok (), false => + ({ pool with connections := pool.connections.push id }, #[.grant dial.ticket id]) + | .ok (), true => + ({ pool with connections := pool.connections.push id, closing := pool.closing.push id }, + #[.closeConnection id]) + | .error err, cancelled => + let (pool, more) := pool.passPlace next + (pool, (if cancelled then #[] else #[.reject dial.ticket err]) ++ more) + +/-- +Takes back connection `id` from the request that held it: to the oldest waiting request, else to +the idle connections, when `reusable`; closed otherwise. A connection already closing, or not held, +is left as it is. +-/ +private def released (pool : OriginPool) (id : ConnectionId) (reusable : Bool) : + OriginPool × Array Command := + if !pool.connections.contains id || pool.closing.contains id || pool.idle.contains id then + (pool, #[]) + else if reusable then + match pool.waiters.dequeue? with + | some (ticket, waiters) => ({ pool with waiters }, #[.grant ticket id]) + | none => ({ pool with idle := pool.idle.push id }, #[]) + else + ({ pool with closing := pool.closing.push id }, #[.closeConnection id]) + +/-- +Stops granting connection `id`, which is ending on its own. It keeps its place until its loop exits, +and a request that still holds it releases it to nothing. +-/ +private def retired (pool : OriginPool) (id : ConnectionId) : OriginPool := + if !pool.connections.contains id || pool.closing.contains id then pool + else { pool with idle := pool.idle.filter (· != id), closing := pool.closing.push id } + +/-- +Forgets connection `id`, whose loop exited, and passes its place on, to open connection `next`. +-/ +private def connectionClosed (pool : OriginPool) (id : ConnectionId) (next : ConnectionId) : + OriginPool × Array Command := + if !pool.connections.contains id then + (pool, #[]) + else + passPlace { pool with + connections := pool.connections.filter (· != id) + idle := pool.idle.filter (· != id) + closing := pool.closing.filter (· != id) } next + +/-- +Fails the waiting requests and the ones whose connection is being opened, and closes every +connection not already closing. +-/ +private def close (pool : OriginPool) : OriginPool × Array Command := + let waiting := pool.waiters.toArray.map fun ticket => + Command.reject ticket (.closed "client closed while the request waited for a connection") + let dialing := pool.dials.filterMap fun dial => + if dial.cancelled then none + else some (Command.reject dial.ticket + (.closed "client closed while the connection was being opened")) + let running := pool.connections.filter (!pool.closing.contains ·) + ({ pool with + waiters := .empty + idle := #[] + dials := pool.dials.map ({ · with cancelled := true }) + closing := pool.closing ++ running }, + waiting ++ dialing ++ running.map .closeConnection) + +end OriginPool + +/-- +The state of the pool driver. +-/ +structure State where + /-- + A pool for each origin with a connection or a waiting request. + -/ + pools : Array OriginPool := #[] + + /-- + The name of the next connection to open. + -/ + nextId : ConnectionId := 0 + +namespace State + +/-- +The pool of `origin`, if it holds a connection or a waiting request. +-/ +def pool? (s : State) (origin : URI.Origin) : Option OriginPool := + s.pools.find? (·.origin == origin) + +/-- +Applies `f` to the pool at `index` in `s.pools`, or to a new pool for `origin` when there is none, +gives `f` the next connection name, and drops the pool once `f` leaves it unused. +-/ +private def update (s : State) (origin : URI.Origin) (index? : Option Nat) + (f : OriginPool → ConnectionId → OriginPool × Array Command) : State × Array Command := + let pool := (index?.bind (s.pools[·]?)).getD { origin } + let (pool, cmds) := f pool s.nextId + let pools := match index?, pool.isUnused with + | some index, true => s.pools.eraseIdx! index + | some index, false => s.pools.set! index pool + | none, true => s.pools + | none, false => s.pools.push pool + let opened := cmds.any fun | .dial .. => true | _ => false + ({ pools, nextId := if opened then s.nextId + 1 else s.nextId }, cmds) + +/-- +Applies `f` to the pool of `origin`. +-/ +private def withOrigin (s : State) (origin : URI.Origin) + (f : OriginPool → ConnectionId → OriginPool × Array Command) : State × Array Command := + s.update origin (s.pools.findIdx? (·.origin == origin)) f + +/-- +Applies `f` to the pool that connection `id` belongs to, open or being opened. An unknown +connection changes nothing. +-/ +private def withConnection (s : State) (id : ConnectionId) + (f : OriginPool → ConnectionId → OriginPool × Array Command) : State × Array Command := + let owns (pool : OriginPool) := pool.connections.contains id || pool.dials.any (·.id == id) + match s.pools.findIdx? owns with + | some index => + match s.pools[index]? with + | some pool => s.update pool.origin (some index) f + | none => (s, #[]) + | none => (s, #[]) + +end State + +/-- +Applies one input. +-/ +def step (cfg : Client.Config) (s : State) : Input → State × Array Command + | .acquire ticket origin fresh => + s.withOrigin origin (·.acquire cfg.maxConnectionsPerOrigin.val ticket fresh ·) + | .dialed id result => s.withConnection id (·.dialed id result ·) + | .released id reusable => s.withConnection id fun pool _ => pool.released id reusable + | .retired id => s.withConnection id fun pool _ => (pool.retired id, #[]) + | .connectionClosed id => s.withConnection id (·.connectionClosed id ·) + | .close => + let (pools, cmds) := s.pools.foldl (init := (#[], #[])) fun (pools, cmds) pool => + let (pool, more) := pool.close + (if pool.isUnused then pools else pools.push pool, cmds ++ more) + ({ s with pools }, cmds) + +end Std.Http.Client.Pool.Driver
diff --git a/tests/async/elab/async_http_client_pool_driver.lean b/tests/async/elab/async_http_client_pool_driver.lean new file mode 100644 index 0000000..0afdb56 --- /dev/null +++ b/tests/async/elab/async_http_client_pool_driver.lean
@@ -0,0 +1,239 @@ +import Std.Http + +/-! +Tests the HTTP client pool driver (`Std.Http.Client.Pool.Driver`) without I/O: reusing idle +connections, connections that retire on their own, `maxConnectionsPerOrigin` and the requests that +wait at it, places passed on as connections close or fail to open, retries that skip idle connections, +separate origins (scheme, host and port, RFC 6454), `close`, inputs reported twice, and that every +request is answered once. +-/ + +open Std.Http Std.Http.Client Std.Http.Client.Pool.Driver + +def cfg : Client.Config := { maxConnectionsPerOrigin := ⟨2, by decide⟩ } + +def origin (host : String) : URI.Origin := + (URI.parse! s!"http://{host}/").origin?.getD + { scheme := URI.Scheme.ofString! "http", host := .empty, port := 0 } + +def a := origin "a" +def b := origin "b" + +def tags (cmds : Array Command) : List String := + cmds.toList.map fun + | .dial ticket o id => s!"dial {ticket} {o.host} #{id}" + | .grant ticket id => s!"grant {ticket} #{id}" + | .reject ticket err => s!"reject {ticket}: {err}" + | .closeConnection id => s!"close #{id}" + +/-- Applies `inputs` in order, collecting every command. -/ +def run (inputs : List Input) (s : State := {}) (config : Client.Config := cfg) : + State × Array Command := + inputs.foldl (init := (s, #[])) fun (s, cmds) input => + let (s, more) := step config s input + (s, cmds ++ more) + +def runTags (inputs : List Input) (s : State := {}) : List String := tags (run inputs s).2 + +def ok : Except Client.Error Unit := .ok () +def refused : Except Client.Error Unit := .error (.connect "refused") + +/-- The tickets `cmds` answer, in order. -/ +def answered (cmds : Array Command) : List Nat := + cmds.toList.filterMap fun + | .grant ticket _ | .reject ticket _ => some ticket + | _ => none + +/-! ## Opening and reusing -/ + +#guard runTags [.acquire 1 a false, .dialed 0 ok] == ["dial 1 a #0", "grant 1 #0"] + +-- A released connection is reused, most recently released first. +#guard runTags [.acquire 1 a false, .dialed 0 ok, .acquire 2 a false, .dialed 1 ok, + .released 0 true, .released 1 true, .acquire 3 a false] == + ["dial 1 a #0", "grant 1 #0", "dial 2 a #1", "grant 2 #1", "grant 3 #1"] + +-- One that cannot carry another request is closed, and keeps its place until its loop exits. +#guard + let (s, cmds) := run [.acquire 1 a false, .dialed 0 ok, .released 0 false] + tags cmds == ["dial 1 a #0", "grant 1 #0", "close #0"] && (s.pool? a).any (·.size == 1) && + (run [.connectionClosed 0] s).1.pools.isEmpty + +-- An idle connection that closes is forgotten. +#guard + let (s, _) := run [.acquire 1 a false, .dialed 0 ok, .released 0 true, .connectionClosed 0] + s.pools.isEmpty && runTags [.acquire 2 a false] s == ["dial 2 a #1"] + +/-! ## Connections that end on their own -/ + +-- An idle connection that retires is never granted again, and keeps its place until its loop exits. +#guard + let (s, _) := run [.acquire 1 a false, .acquire 2 a false, .dialed 0 ok, .dialed 1 ok, + .released 0 true, .released 1 true, .retired 1] + runTags [.acquire 3 a false] s == ["grant 3 #0"] && + runTags [.acquire 3 a false, .acquire 4 a false] s == ["grant 3 #0"] && + runTags [.acquire 3 a false, .acquire 4 a false, .connectionClosed 1] s == ["grant 3 #0", "dial 4 a #2"] + +-- Retiring asks for no close: the connection is already ending. +#guard runTags [.acquire 1 a false, .dialed 0 ok, .released 0 true, .retired 0] == + ["dial 1 a #0", "grant 1 #0"] + +-- A held connection that retires goes back to no one, even when released as reusable. +#guard + let (s, _) := run [.acquire 1 a false, .acquire 2 a false, .acquire 3 a false, .dialed 0 ok, + .dialed 1 ok, .retired 0] + runTags [.released 0 true] s == [] && runTags [.released 0 true, .connectionClosed 0] s == ["dial 3 a #2"] + +-- The last connection of an origin that retires and exits leaves no pool behind. +#guard (run [.acquire 1 a false, .dialed 0 ok, .released 0 true, .retired 0, + .connectionClosed 0]).1.pools.isEmpty + +-- Retiring a connection already closing, or twice, changes nothing. +#guard + let (s, _) := run [.acquire 1 a false, .dialed 0 ok, .released 0 false] + let closing (s : State) := (s.pool? a).map (·.closing.toList) + closing (run [.retired 0, .retired 0] s).1 == closing s + +/-! ## The limit -/ + +-- At the limit a request waits, and gets the next connection released. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .acquire 3 a false, .dialed 0 ok, + .dialed 1 ok, .released 1 true] == + ["dial 1 a #0", "dial 2 a #1", "grant 1 #0", "grant 2 #1", "grant 3 #1"] + +-- Waiting requests are served oldest first. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .acquire 3 a false, .acquire 4 a false, + .dialed 0 ok, .dialed 1 ok, .released 0 true, .released 1 true] == + ["dial 1 a #0", "dial 2 a #1", "grant 1 #0", "grant 2 #1", "grant 3 #0", "grant 4 #1"] + +-- A connection that closes passes its place to the oldest waiting request, which opens its own. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .acquire 3 a false, .dialed 0 ok, + .connectionClosed 0] == + ["dial 1 a #0", "dial 2 a #1", "grant 1 #0", "dial 3 a #2"] + +-- So does a connection that fails to open, after failing its own request. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .acquire 3 a false, .dialed 0 refused] == + ["dial 1 a #0", "dial 2 a #1", "reject 1: connect: refused", "dial 3 a #2"] + +-- A connection being closed still counts: no request goes past the limit while it tears down. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .dialed 0 ok, .dialed 1 ok, + .released 0 false, .acquire 3 a false] == + ["dial 1 a #0", "dial 2 a #1", "grant 1 #0", "grant 2 #1", "close #0"] + +-- Origins have separate limits. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .acquire 3 b false] == + ["dial 1 a #0", "dial 2 a #1", "dial 3 b #2"] + +/-! ## Retries -/ + +-- A fresh request skips idle connections while under the limit... +#guard runTags [.acquire 1 a false, .dialed 0 ok, .released 0 true, .acquire 2 a true] == + ["dial 1 a #0", "grant 1 #0", "dial 2 a #1"] + +-- ...and at it closes the least recently used one, then waits for the place it frees. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .dialed 0 ok, .dialed 1 ok, + .released 0 true, .released 1 true, .acquire 3 a true, .connectionClosed 0] == + ["dial 1 a #0", "dial 2 a #1", "grant 1 #0", "grant 2 #1", "close #0", "dial 3 a #2"] + +-- A connection told to close is never granted, even when released as reusable. +#guard + let (s, cmds) := run [.acquire 1 a false, .acquire 2 a false, .dialed 0 ok, .dialed 1 ok, + .released 1 true, .acquire 3 a true] + tags cmds == ["dial 1 a #0", "dial 2 a #1", "grant 1 #0", "grant 2 #1", "close #1"] && + runTags [.released 1 true, .released 0 true] s == ["grant 3 #0"] + +/-! ## Closing -/ + +#guard + let (s, _) := run [.acquire 1 a false, .acquire 2 a false, .acquire 3 a false, .dialed 0 ok] + let (s, cmds) := run [.close] s + tags cmds == ["reject 3: client closed while the request waited for a connection", + "reject 2: client closed while the connection was being opened", "close #0"] && + -- The cancelled dial is closed once it opens, and holds its place until then. + runTags [.dialed 1 ok] s == ["close #1"] && + (run [.dialed 1 ok, .connectionClosed 0, .connectionClosed 1] s).1.pools.isEmpty + +-- The pool is usable after `close`: a later request opens a fresh connection. +#guard + let (s, _) := run [.acquire 1 a false, .dialed 0 ok, .released 0 true, .close] + runTags [.acquire 2 a false] s == ["dial 2 a #1"] + +-- A cancelled dial that fails answers nothing more. +#guard + let (s, _) := run [.acquire 1 a false, .close] + runTags [.dialed 0 refused] s == [] && (run [.dialed 0 refused] s).1.pools.isEmpty + +/-! ## Every request is answered once -/ + +#guard + let (s, cmds) := run [.acquire 1 a false, .acquire 2 a false, .acquire 3 a false, + .acquire 4 a true, .acquire 5 b false, .dialed 0 refused, .dialed 2 ok, .dialed 1 ok, + .released 1 true, .dialed 3 ok, .released 3 false, .connectionClosed 3, .close, + .dialed 4 ok] + let tickets := answered cmds + tickets.length == 5 && [1, 2, 3, 4, 5].all tickets.contains && s.pools.all (·.waiters.isEmpty) + +-- Inputs about unknown connections change nothing. +#guard runTags [.dialed 7 ok, .released 7 true, .retired 7, .connectionClosed 7] == [] && + (run [.dialed 7 ok, .released 7 true, .retired 7, .connectionClosed 7]).1.pools.isEmpty + +/-! ## Origins -/ + +def originOf (u : String) : URI.Origin := (URI.parse! u).origin?.getD a + +-- An origin is a scheme, a host and a port (RFC 6454 §4): each has its own connections, and the +-- default port is the same origin as none. +#guard runTags [.acquire 1 (originOf "http://a/") false, .dialed 0 ok, .released 0 true, + .acquire 2 (originOf "http://a:8080/") false, .acquire 3 (originOf "https://a/") false, + .acquire 4 (originOf "http://a:80/") false] == + ["dial 1 a #0", "grant 1 #0", "dial 2 a #1", "dial 3 a #2", "grant 4 #0"] + +/-! ## More on the limit and retries -/ + +-- At the limit with every connection held, a fresh request waits: no held connection is closed. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .dialed 0 ok, .dialed 1 ok, .acquire 3 a true] == + ["dial 1 a #0", "dial 2 a #1", "grant 1 #0", "grant 2 #1"] + +-- A connection handed straight from its release to a waiting fresh request never sat idle, so it is +-- not one that may have gone stale: the fresh request takes it. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .dialed 0 ok, .dialed 1 ok, .acquire 3 a true, + .released 0 true] == + ["dial 1 a #0", "dial 2 a #1", "grant 1 #0", "grant 2 #1", "grant 3 #0"] + +-- Waiting requests are served oldest first, fresh or not. +#guard + let one : Client.Config := { maxConnectionsPerOrigin := ⟨1, by decide⟩ } + tags (run [.acquire 1 a false, .dialed 0 ok, .acquire 2 a false, .acquire 3 a true, .released 0 true] + (config := one)).2 == ["dial 1 a #0", "grant 1 #0", "grant 2 #0"] && + tags (run [.acquire 1 a false, .dialed 0 ok, .acquire 2 a true, .acquire 3 a false, .released 0 true] + (config := one)).2 == ["dial 1 a #0", "grant 1 #0", "grant 2 #0"] + +-- A connection whose release did not keep it waits for its loop to exit before its place is passed. +#guard runTags [.acquire 1 a false, .acquire 2 a false, .acquire 3 a false, .dialed 0 ok, .dialed 1 ok, + .released 0 false, .connectionClosed 0] == + ["dial 1 a #0", "dial 2 a #1", "grant 1 #0", "grant 2 #1", "close #0", "dial 3 a #2"] + +/-! ## Inputs reported twice -/ + +-- A connection released twice is granted once. +#guard runTags [.acquire 1 a false, .dialed 0 ok, .released 0 true, .released 0 true, .acquire 2 a false, + .acquire 3 a false] == + ["dial 1 a #0", "grant 1 #0", "grant 2 #0", "dial 3 a #1"] + +-- A connection reported open twice is granted once. +#guard runTags [.acquire 1 a false, .dialed 0 ok, .dialed 0 ok] == ["dial 1 a #0", "grant 1 #0"] + +/-! ## More on closing -/ + +-- `close` closes idle connections too. +#guard runTags [.acquire 1 a false, .dialed 0 ok, .released 0 true, .close] == + ["dial 1 a #0", "grant 1 #0", "close #0"] + +-- A connection released after `close` goes back to no one. +#guard + let (s, _) := run [.acquire 1 a false, .dialed 0 ok, .close] + runTags [.released 0 true, .acquire 2 a false] s == ["dial 2 a #1"] + +-- Connection names are never reused, across `close` too. +#guard runTags [.acquire 1 a false, .dialed 0 ok, .close, .connectionClosed 0, .acquire 2 a false] == + ["dial 1 a #0", "grant 1 #0", "close #0", "dial 2 a #1"]