hs-libp2p-0.2.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

LibP2P.Switch.Connection

Description

Connection lifecycle for the Switch.

Provides the single teardown path for upgraded connections and a resource-accounted outbound stream opener. Teardown fires from the stream accept loop exit (remote disconnect or session death), from explicit closeConnection calls, and from switchClose via closeAllConnections.

Synopsis

Documentation

closeConnection :: Switch -> Connection -> IO () Source #

Tear down a connection: remove it from the pool, release its resource reservation, publish a Disconnected event, run the disconnect notifiers, and close the muxer session together with the underlying transport.

Idempotent: the state transition to ConnClosed is atomic, so concurrent calls (accept loop exit, explicit close, switchClose) perform the teardown exactly once, and the notifiers run once.

Notifiers run synchronously, after the pool removal has committed and before the muxer is closed, so a notifier that asks whether the peer still has a live connection (Circuit Relay v2 reservation cleanup) never sees the connection being torn down. Each is isolated so a failing notifier cannot abort the teardown.

closeAllConnections :: Switch -> IO () Source #

Tear down every pooled connection (used by switchClose).

newStream :: Switch -> Connection -> IO (Either StreamOpenError StreamIO) Source #

Open an outbound stream on a connection, reserving a stream slot against the peer's resource scope. The slot is released when the returned stream is closed (exactly once, even on double close).

Refuses a relay-limited connection. Protocols that must run over a relay circuit (Identify, Ping, DCUtR) use newStreamAllowingLimited.

newStreamAllowingLimited :: Switch -> Connection -> IO (Either StreamOpenError StreamIO) Source #

Like newStream, but allows a relay-limited connection.

The circuit still dies when its budget is exhausted (RelayCircuitExhausted). Opt in only for a short exchange that can tolerate that.

data StreamOpenError Source #

Failure to open an outbound stream.

Constructors

StreamResourceLimit !ResourceError

Peer or connection stream budget is exhausted

StreamLimitedConnection

The connection is a relay circuit with an active budget, and the caller did not opt in. No stream slot is reserved.