Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iroh): Allow customising the TransportConfig for connections #3111

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -596,8 +601,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 @@ -635,18 +657,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 +687,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
Loading