Skip to content

feat: allow to initialize remote connections before application requests - #155

Open
Frando wants to merge 3 commits into
mainfrom
Frando/reconnect2
Open

Frando wants to merge 3 commits into
mainfrom
Frando/reconnect2

Conversation

@Frando

@Frando Frando commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Description

Clients can now run setup requests, such as authentication, on each new
remote connection, before any application request uses it. This also happens
after a lazy client reconnects.

Before this PR, the lazy iroh and noq connections reconnected without informing
the user at all. If a server kept per-connection state (like authed: bool in
examples/auth.rs), the client could not recover after a reconnect (#60).

let client = Client::<Protocol>::iroh_builder(endpoint, addr, ALPN)
    .on_connect(move |conn_client| {
        let token = token.clone();
        async move {
            let accepted = conn_client.rpc(Auth(token)).await?;
            anyhow::ensure!(accepted, "authentication failed");
            Ok(())
        }
    })
    .build();

// Optional: connect and authenticate now instead of on the first request.
client.connect().await?;
  • New Client::noq_builder / Client::iroh_builder return a
    RemoteClientBuilder. RemoteClientBuilder::from_noq / from_iroh wrap
    one existing connection. Client::noq / Client::iroh behave as before.
  • The hook gets a Client bound to the new connection. All clones of the
    outer client wait until the hook returns Ok. If setup fails or is
    cancelled, the connection is closed and not reused.
  • New Client::connect() and RemoteConnection::connect() (with a default
    impl), and new RequestError::Initialization / ConnectUnsupported
    variants.
  • The noq and iroh lazy connections now share one implementation.
  • examples/auth.rs now re-authenticates after the server drops the
    connection.

Fixes #60
Replaces #61

Breaking Changes

None

Change checklist

  • Self-review.
  • Documentation updates following the style guide, if relevant.
  • Tests if relevant.
  • All breaking changes documented.

@Frando
Frando force-pushed the Frando/reconnect2 branch from 7680502 to ab9d1ea Compare November 9, 2026 09:35
@Frando
Frando marked this pull request as ready for review November 9, 2026 13:44
@Frando
Frando force-pushed the Frando/reconnect2 branch from b14cdc0 to 23ddbbb Compare November 9, 2026 13:47
Frando and others added 3 commits November 9, 2026 17:09
Run a configurable initializer before opening application streams on each
new connection, including reconnects. Share connection setup between noq
and iroh, with eager setup and cleanup on failure or cancellation.

Update the auth example to reuse the same client after the server closes
its connection. Cover concurrent callers, fixed connections, cancellation,
and closed connections with a completed 0-RTT handshake.
OutgoingZeroRttConnection::connect turned every handshake error into
RequestError::IrohConnect. A connection error now becomes
ConnectionClosed or ConnectionFailed, as it does for open_bi on the
same connection, so remote_code() returns the peer's close code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Keep the other examples and the irpc-opentelemetry tests on
  IrohLazyRemoteConnection, which still works.
- Trim the auth example to the reconnect, and keep its variable names.
- Test the anyhow conversion through the shared test helper instead
  of in a separate test.
- Close an unfinished connection with one shared code and reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Frando
Frando force-pushed the Frando/reconnect2 branch from 23ddbbb to e39ae37 Compare November 9, 2026 16:21
@rklaehn

rklaehn commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Two things about the new RequestError variants:

ConnectUnsupported can go entirely. The default of RemoteConnection::connect() can just return Ok(()). Every RemoteConnection in irpc overrides connect(), so the default only applies to implementations outside irpc. The ones I know of wrap a connection that is already established, for example a connection from a pool. For those, Ok(()) is the right answer, while ConnectUnsupported would make Client::connect() fail on a client that is connected. A wrapper that wants the "is it still open" check can forward to the connection it wraps.

Initialization should be named OnConnectFailed. The other variants say what failed (ConnectionFailed, and EncodeFailed/DecodeFailed elsewhere), and the only source of this error is the on_connect hook. With OnConnectFailed, a user who gets the error knows right away that it came from their hook. Something like:

/// The `on_connect` hook of the client returned an error.
#[cfg(feature = "rpc")]
#[error("on_connect hook failed")]
OnConnectFailed { source: n0_error::AnyError },

@rklaehn

rklaehn commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

On second thought, SetupFailed instead of OnConnectFailed. The hook is the only source today, but connection setup is a category: other setup steps we might add later, for example a version handshake or an auth helper, would fit the same variant instead of each adding one. The cause stays in source.

/// Setting up a new connection failed, for example because the `on_connect` hook returned an error.
#[cfg(feature = "rpc")]
#[error("Connection setup failed")]
SetupFailed { source: n0_error::AnyError },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make it possible to have stateful connections

2 participants