Skip to content

Commit

Permalink
Make key_cache_capcity optional in the config files
Browse files Browse the repository at this point in the history
  • Loading branch information
rklaehn committed Dec 16, 2024
1 parent 46598a2 commit 7f99ed1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
7 changes: 7 additions & 0 deletions iroh-relay/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ pub const DEFAULT_HTTPS_PORT: u16 = 443;
/// The default metrics port used by the Relay server.
pub const DEFAULT_METRICS_PORT: u16 = 9090;

/// The default capacity of the key cache for the relay server.
///
/// Sized for 1 million concurrent clients.
/// memory usage will be (32 + 8 + 8 + 8) * 1_000_000 = 56MB on 64 bit,
/// which seems reasonable for a server.
pub const DEFAULT_KEY_CACHE_CAPACITY: usize = 1024 * 1024;

/// Contains all timeouts that we use in `iroh`.
pub(crate) mod timeouts {
use std::time::Duration;
Expand Down
4 changes: 2 additions & 2 deletions iroh-relay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ struct Config {
/// (`[::]:9090` when `http_bind_addr` is set to the default).
metrics_bind_addr: Option<SocketAddr>,
/// The capacity of the key cache.
key_cache_capacity: usize,
key_cache_capacity: Option<usize>,
}

impl Config {
Expand Down Expand Up @@ -201,7 +201,7 @@ impl Default for Config {
limits: None,
enable_metrics: cfg_defaults::enable_metrics(),
metrics_bind_addr: None,
key_cache_capacity: 1024 * 1024,
key_cache_capacity: Default::default(),
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions iroh-relay/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use tokio_util::task::AbortOnDropHandle;
use tracing::{debug, error, info, info_span, instrument, trace, warn, Instrument};

use crate::{
defaults::DEFAULT_KEY_CACHE_CAPACITY,
http::RELAY_PROBE_PATH,
protos,
quic::server::{QuicServer, ServerHandle as QuicServerHandle},
Expand Down Expand Up @@ -120,7 +121,7 @@ pub struct RelayConfig<EC: fmt::Debug, EA: fmt::Debug = EC> {
/// Rate limits.
pub limits: Limits,
/// Key cache capacity.
pub key_cache_capacity: usize,
pub key_cache_capacity: Option<usize>,
}

/// Configuration for the STUN server.
Expand Down Expand Up @@ -310,9 +311,12 @@ impl Server {
Some(ref tls) => tls.https_bind_addr,
None => relay_config.http_bind_addr,
};
let key_cache_capacity = relay_config
.key_cache_capacity
.unwrap_or(DEFAULT_KEY_CACHE_CAPACITY);
let mut builder = http_server::ServerBuilder::new(relay_bind_addr)
.headers(headers)
.key_cache_capacity(relay_config.key_cache_capacity)
.key_cache_capacity(key_cache_capacity)
.request_handler(Method::GET, "/", Box::new(root_handler))
.request_handler(Method::GET, "/index.html", Box::new(root_handler))
.request_handler(Method::GET, RELAY_PROBE_PATH, Box::new(probe_handler))
Expand Down Expand Up @@ -782,7 +786,7 @@ mod tests {
http_bind_addr: (Ipv4Addr::LOCALHOST, 0).into(),
tls: None,
limits: Default::default(),
key_cache_capacity: 1024,
key_cache_capacity: Some(1024),
}),
quic: None,
stun: None,
Expand Down Expand Up @@ -812,7 +816,7 @@ mod tests {
http_bind_addr: (Ipv4Addr::LOCALHOST, 1234).into(),
tls: None,
limits: Default::default(),
key_cache_capacity: 1024,
key_cache_capacity: Some(1024),
}),
stun: None,
quic: None,
Expand Down
6 changes: 2 additions & 4 deletions iroh-relay/src/server/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use tokio_util::{codec::Framed, sync::CancellationToken, task::AbortOnDropHandle
use tracing::{debug, debug_span, error, info, info_span, trace, warn, Instrument};

use crate::{
defaults::DEFAULT_KEY_CACHE_CAPACITY,
http::{Protocol, LEGACY_RELAY_PATH, RELAY_PATH, SUPPORTED_WEBSOCKET_VERSION},
protos::relay::{recv_client_key, DerpCodec, PER_CLIENT_SEND_QUEUE_DEPTH, PROTOCOL_VERSION},
server::{
Expand Down Expand Up @@ -175,10 +176,7 @@ impl ServerBuilder {
handlers: Default::default(),
headers: HeaderMap::new(),
client_rx_ratelimit: None,
// Sized for 1 million concurrent clients.
// memory usage will be (32 + 8 + 8 + 8) * 1_000_000 = 56MB,
// which seems reasonable for a server.
key_cache_capacity: 1024 * 1024,
key_cache_capacity: DEFAULT_KEY_CACHE_CAPACITY,
}
}

Expand Down
2 changes: 1 addition & 1 deletion iroh-relay/src/server/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn relay_config() -> RelayConfig<()> {
http_bind_addr: (Ipv4Addr::LOCALHOST, 0).into(),
tls: Some(tls_config()),
limits: Default::default(),
key_cache_capacity: 1024,
key_cache_capacity: Some(1024),
}
}

Expand Down
2 changes: 1 addition & 1 deletion iroh/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub async fn run_relay_server_with(
http_bind_addr: (Ipv4Addr::LOCALHOST, 0).into(),
tls: Some(tls),
limits: Default::default(),
key_cache_capacity: 1024,
key_cache_capacity: Some(1024),
}),
quic,
stun,
Expand Down

0 comments on commit 7f99ed1

Please sign in to comment.