Skip to content

Commit

Permalink
quinn: recommended transport config helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Apr 24, 2023
1 parent 104df43 commit 0bff65d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
3 changes: 1 addition & 2 deletions bench/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ pub fn rt() -> Runtime {
pub fn transport_config(opt: &Opt) -> quinn::TransportConfig {
// High stream windows are chosen because the amount of concurrent streams
// is configurable as a parameter.
let mut config = quinn::TransportConfig::default();
config.mtu_discovery_config(quinn::recommended_mtu_discovery());
let mut config = quinn::recommended_transport_config();
config.max_concurrent_uni_streams(opt.max_streams.try_into().unwrap());
config.initial_max_udp_payload_size(opt.initial_mtu);
config
Expand Down
3 changes: 1 addition & 2 deletions perf/src/bin/perf_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ async fn run(opt: Opt) -> Result<()> {
crypto.key_log = Arc::new(rustls::KeyLogFile::new());
}

let mut transport = quinn::TransportConfig::default();
transport.mtu_discovery_config(quinn::recommended_mtu_discovery());
let mut transport = quinn::recommended_transport_config();
transport.initial_max_udp_payload_size(opt.initial_max_udp_payload_size);

let mut cfg = if opt.no_protection {
Expand Down
3 changes: 1 addition & 2 deletions perf/src/bin/perf_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ async fn run(opt: Opt) -> Result<()> {
crypto.key_log = Arc::new(rustls::KeyLogFile::new());
}

let mut transport = quinn::TransportConfig::default();
transport.mtu_discovery_config(quinn::recommended_mtu_discovery());
let mut transport = quinn::recommended_transport_config();
transport.initial_max_udp_payload_size(opt.initial_max_udp_payload_size);

let mut server_config = if opt.no_protection {
Expand Down
4 changes: 1 addition & 3 deletions quinn/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ impl Context {
roots.add(&cert).unwrap();

let mut client_config = quinn::ClientConfig::with_root_certificates(roots);
let mut transport_config = quinn::TransportConfig::default();
transport_config.mtu_discovery_config(quinn::recommended_mtu_discovery());
client_config.transport_config(Arc::new(transport_config));
client_config.transport_config(Arc::new(quinn::recommended_transport_config()));

Self {
server_config,
Expand Down
11 changes: 5 additions & 6 deletions quinn/examples/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ fn configure_client(server_certs: &[&[u8]]) -> Result<ClientConfig, Box<dyn Erro
certs.add(&rustls::Certificate(cert.to_vec()))?;
}

let mut transport_config = quinn::TransportConfig::default();
transport_config.mtu_discovery_config(quinn::recommended_mtu_discovery());
let mut client_config = ClientConfig::with_root_certificates(certs);
client_config.transport_config(Arc::new(transport_config));
client_config.transport_config(Arc::new(quinn::recommended_transport_config()));

Ok(client_config)
}
Expand All @@ -61,10 +59,11 @@ fn configure_server() -> Result<(ServerConfig, Vec<u8>), Box<dyn Error>> {
let priv_key = rustls::PrivateKey(priv_key);
let cert_chain = vec![rustls::Certificate(cert_der.clone())];

let mut server_config = ServerConfig::with_single_cert(cert_chain, priv_key)?;
let transport_config = Arc::get_mut(&mut server_config.transport).unwrap();
let mut transport_config = quinn::recommended_transport_config();
transport_config.max_concurrent_uni_streams(0_u8.into());
transport_config.mtu_discovery_config(quinn::recommended_mtu_discovery());

let mut server_config = ServerConfig::with_single_cert(cert_chain, priv_key)?;
server_config.transport = Arc::new(transport_config);

Ok((server_config, cert_der))
}
Expand Down
7 changes: 4 additions & 3 deletions quinn/examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ async fn run(options: Opt) -> Result<()> {
server_crypto.key_log = Arc::new(rustls::KeyLogFile::new());
}

let mut server_config = quinn::ServerConfig::with_crypto(Arc::new(server_crypto));
let transport_config = Arc::get_mut(&mut server_config.transport).unwrap();
let mut transport_config = quinn::recommended_transport_config();
transport_config.max_concurrent_uni_streams(0_u8.into());
transport_config.mtu_discovery_config(quinn::recommended_mtu_discovery());

let mut server_config = quinn::ServerConfig::with_crypto(Arc::new(server_crypto));
server_config.transport_config(Arc::new(transport_config));
if options.stateless_retry {
server_config.use_retry(true);
}
Expand Down
16 changes: 13 additions & 3 deletions quinn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,23 @@ enum EndpointEvent {

/// The [`MtuDiscoveryConfig`] recommended for use on the target platform
///
/// Not accurate for custom [`Runtime`]s unless they rely on the same version of the `quinn-udp`
/// crate to perform I/O. Pass to [`TransportConfig::mtu_discovery_config`] when constructing custom
/// [`EndpointConfig`]s.
/// Pass to [`TransportConfig::mtu_discovery_config`] when constructing custom
/// [`EndpointConfig`]s. See [`recommended_transport_config`] for caveats.
pub fn recommended_mtu_discovery() -> Option<MtuDiscoveryConfig> {
udp::fragmentation_disabled().then(MtuDiscoveryConfig::default)
}

/// The [`TransportConfig`] recommended for use on the target platform
///
/// Contrast [`TransportConfig::default`], which does not change between targets. Not accurate for
/// custom [`Runtime`]s unless they rely on the same version of the `quinn-udp` crate to perform
/// I/O.
pub fn recommended_transport_config() -> TransportConfig {
let mut config = TransportConfig::default();
config.mtu_discovery_config(recommended_mtu_discovery());
config
}

/// Maximum number of datagrams processed in send/recv calls to make before moving on to other processing
///
/// This helps ensure we don't starve anything when the CPU is slower than the link.
Expand Down

0 comments on commit 0bff65d

Please sign in to comment.