Skip to content

Commit

Permalink
feat(iroh): Allow customising the TransportConfig for connections
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
flub committed Jan 9, 2025
1 parent 1ae820d commit 0b31484
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 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 @@ -379,7 +381,7 @@ impl Builder {
/// streams should set `max_concurrent_bidi_streams` and `max_concurrent_uni_streams` to
/// zero.
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 @@ -596,8 +598,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 these 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 @@ -635,18 +654,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 @@ -658,9 +684,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 0b31484

Please sign in to comment.