Skip to content

Commit

Permalink
feat(iroh): Allow customising the TransportConfig for connections (#3111
Browse files Browse the repository at this point in the history
)

## Description

Previously the TransportConfig was hardcoded for the
client (client/server in QUIC terminology: client initiates the
connection, server accepts the connection).  This rendered some
customisations on the server-side, as supported by the Builder,
impossible since the client would negotiate different limits.

Specifically the QUIC keep-alives were at a 1s or faster interval
while the connection timeout was at 30s or less.

This adds a new API marked as experimental which allows customising
the transport config for clients.

Secondly it also respects any custom TransportConfig set in the
Builder for clients as well as servers.  This is not a behaviour
change since the parameters set by default are now moved to the
Builder and due to how these are negotiated it does not matter that
the server will now also have the same values set by default.

Closes #2872.

## Breaking Changes

### iroh

If Builder::transport_config is used it will now also affect initiated
connections, while before it only affected the accepted connections.

## Notes & open questions

<!-- Any notes, remarks or open questions you have to make about the PR.
-->

## Change checklist

- [x] Self-review.
- [x] Documentation updates following the [style
guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text),
if relevant.
- [x] Tests if relevant.
- [x] All breaking changes documented.
  • Loading branch information
flub authored Jan 23, 2025
1 parent c532de3 commit 2b92db4
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions iroh/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct Builder {
secret_key: Option<SecretKey>,
relay_mode: RelayMode,
alpn_protocols: Vec<Vec<u8>>,
transport_config: Option<quinn::TransportConfig>,
transport_config: quinn::TransportConfig,
keylog: bool,
#[debug(skip)]
discovery: Vec<DiscoveryBuilder>,
Expand All @@ -113,11 +113,13 @@ pub struct Builder {

impl Default for Builder {
fn default() -> Self {
let mut transport_config = quinn::TransportConfig::default();
transport_config.keep_alive_interval(Some(Duration::from_secs(1)));
Self {
secret_key: Default::default(),
relay_mode: default_relay_mode(),
alpn_protocols: Default::default(),
transport_config: Default::default(),
transport_config,
keylog: Default::default(),
discovery: Default::default(),
proxy_url: None,
Expand Down Expand Up @@ -146,7 +148,7 @@ impl Builder {
.secret_key
.unwrap_or_else(|| SecretKey::generate(rand::rngs::OsRng));
let static_config = StaticConfig {
transport_config: Arc::new(self.transport_config.unwrap_or_default()),
transport_config: Arc::new(self.transport_config),
keylog: self.keylog,
secret_key: secret_key.clone(),
};
Expand Down Expand Up @@ -378,8 +380,11 @@ impl Builder {
/// internet applications. Applications protocols which forbid remotely-initiated
/// streams should set `max_concurrent_bidi_streams` and `max_concurrent_uni_streams` to
/// zero.
///
/// Please be aware that changing some settings may have adverse effects on establishing
/// and maintaining direct connections.
pub fn transport_config(mut self, transport_config: quinn::TransportConfig) -> Self {
self.transport_config = Some(transport_config);
self.transport_config = transport_config;
self
}

Expand Down Expand Up @@ -599,8 +604,25 @@ impl Endpoint {
/// The `alpn`, or application-level protocol identifier, is also required. The remote
/// endpoint must support this `alpn`, otherwise the connection attempt will fail with
/// an error.
#[instrument(skip_all, fields(me = %self.node_id().fmt_short(), alpn = ?String::from_utf8_lossy(alpn)))]
pub async fn connect(&self, node_addr: impl Into<NodeAddr>, alpn: &[u8]) -> Result<Connection> {
self.connect_with(node_addr, alpn, self.static_config.transport_config.clone())
.await
}

/// Connects to a remote [`Endpoint`] using a custom [`TransportConfig`].
///
/// Like [`Endpoint::connect`], but allows providing a custom for the connection. See
/// the docs of [`Endpoint::connect`] for details.
///
/// Please be aware that changing some settings may have adverse effects on establishing
/// and maintaining direct connections. Carefully test settings you use and consider
/// this currently as still rather experimental.
pub async fn connect_with(
&self,
node_addr: impl Into<NodeAddr>,
alpn: &[u8],
transport_config: Arc<TransportConfig>,
) -> Result<Connection> {
let node_addr: NodeAddr = node_addr.into();
tracing::Span::current().record("remote", node_addr.node_id.fmt_short());
// Connecting to ourselves is not supported.
Expand Down Expand Up @@ -638,18 +660,25 @@ impl Endpoint {

// Start connecting via quinn. This will time out after 10 seconds if no reachable
// address is available.
self.connect_quinn(node_id, alpn, addr).await
self.connect_quinn(node_id, alpn, addr, transport_config)
.await
}

#[instrument(
name = "connect",
skip_all,
fields(remote_node = node_id.fmt_short(), alpn = %String::from_utf8_lossy(alpn))
fields(
me = %self.node_id().fmt_short(),
remote_node = node_id.fmt_short(),
alpn = %String::from_utf8_lossy(alpn),
)
)]
async fn connect_quinn(
&self,
node_id: NodeId,
alpn: &[u8],
addr: QuicMappedAddr,
transport_config: Arc<TransportConfig>,
) -> Result<Connection> {
debug!("Attempting connection...");
let client_config = {
Expand All @@ -661,9 +690,7 @@ impl Endpoint {
self.static_config.keylog,
)?;
let mut client_config = quinn::ClientConfig::new(Arc::new(quic_client_config));
let mut transport_config = quinn::TransportConfig::default();
transport_config.keep_alive_interval(Some(Duration::from_secs(1)));
client_config.transport_config(Arc::new(transport_config));
client_config.transport_config(transport_config);
client_config
};

Expand Down

0 comments on commit 2b92db4

Please sign in to comment.