diff --git a/iroh-relay/src/client.rs b/iroh-relay/src/client.rs index 09906576b1..b00181fd86 100644 --- a/iroh-relay/src/client.rs +++ b/iroh-relay/src/client.rs @@ -227,7 +227,7 @@ impl ClientBuilder { #[cfg(any(test, feature = "test-utils"))] insecure_skip_cert_verify: false, proxy_url: None, - key_cache_capacity: 0, + key_cache_capacity: 128, } } diff --git a/iroh-relay/src/key_cache.rs b/iroh-relay/src/key_cache.rs index 2c6563a9cc..6766a2f446 100644 --- a/iroh-relay/src/key_cache.rs +++ b/iroh-relay/src/key_cache.rs @@ -9,10 +9,15 @@ type SignatureError = >::Error; type PublicKeyBytes = [u8; PublicKey::LENGTH]; /// A cache for public keys. -#[derive(Debug, Clone, Default)] +/// +/// This is used solely to make parsing public keys from byte slices more +/// efficient for the very common case where a large number of identical keys +/// are being parsed, like in the relay server. +/// +/// The cache stores only successful parse results. +#[derive(Debug, Clone)] pub enum KeyCache { /// The key cache is disabled. - #[default] Disabled, /// The key cache is enabled with a fixed capacity. It is shared between /// multiple threads. @@ -43,6 +48,8 @@ impl KeyCache { return PublicKey::try_from(slice); }; let Ok(bytes) = PublicKeyBytes::try_from(slice) else { + // if the size is wrong, use PublicKey::try_from to fail with a + // SignatureError. PublicKey::try_from(slice)?; unreachable!(); }; diff --git a/iroh-relay/src/server/http_server.rs b/iroh-relay/src/server/http_server.rs index 535b46e93a..c9d6865b5d 100644 --- a/iroh-relay/src/server/http_server.rs +++ b/iroh-relay/src/server/http_server.rs @@ -175,7 +175,7 @@ impl ServerBuilder { handlers: Default::default(), headers: HeaderMap::new(), client_rx_ratelimit: None, - key_cache_capacity: 0, + key_cache_capacity: 1024 * 1024, } }