From 4f4f4eb0d6f64c38b6a237351efed30df598c9bb Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Tue, 24 Sep 2024 10:01:09 +0300 Subject: [PATCH] extend configuration interface with `peer_healthcheck_caching_secs` Signed-off-by: onur-ozkan --- src/ctx.rs | 16 +++++++++++++++- src/proxy/http/mod.rs | 5 +++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ctx.rs b/src/ctx.rs index 5eb24ef..2a48dd2 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -9,6 +9,11 @@ pub(crate) use super::*; const DEFAULT_TOKEN_EXPIRATION_TIME: i64 = 3600; pub(crate) const DEFAULT_PORT: u16 = 5000; + +const fn default_peer_caching_secs() -> u64 { + 10 +} + static CONFIG: OnceCell = OnceCell::new(); pub(crate) fn get_app_config() -> &'static AppConfig { @@ -58,6 +63,13 @@ pub(crate) struct AppConfig { pub(crate) proxy_routes: Vec, /// The default rate limiting rules for maintaining the frequency of incoming traffic for per client. pub(crate) rate_limiter: RateLimiter, + /// The number of seconds to cache a known peer. + /// + /// When a peer is identified as connected with `peer_connection_healthcheck` RPC, + /// this value determines how long to cache that peer as known-peer to avoid + /// sending repeated `peer_connection_healthcheck` requests for every proxy request. + #[serde(default = "default_peer_caching_secs")] + pub(crate) peer_healthcheck_caching_secs: u64, } /// Defines a routing rule for proxying requests from an inbound route to an outbound URL @@ -203,6 +215,7 @@ pub(crate) fn get_app_config_test_instance() -> AppConfig { rp_30_min: 555, rp_60_min: 555, }, + peer_healthcheck_caching_secs: 10, } } @@ -282,7 +295,8 @@ fn test_app_config_serialzation_and_deserialization() { "rp_15_min": 555, "rp_30_min": 555, "rp_60_min": 555 - } + }, + "peer_healthcheck_caching_secs": 10, }); let actual_config: AppConfig = serde_json::from_str(&json_config.to_string()).unwrap(); diff --git a/src/proxy/http/mod.rs b/src/proxy/http/mod.rs index ca4e17d..4dcdff6 100644 --- a/src/proxy/http/mod.rs +++ b/src/proxy/http/mod.rs @@ -104,7 +104,8 @@ async fn peer_connection_healthcheck( ) -> Result<(), StatusCode> { // Once we know a peer is connected to the KDF network, we can assume they are connected // for 10 seconds without asking again. - const KNOW_PEER_EXPIRATION: Duration = Duration::from_secs(10); + let know_peer_expiration = Duration::from_secs(cfg.peer_healthcheck_caching_secs); + static KNOWN_PEERS: LazyLock>> = LazyLock::new(|| Mutex::new(ExpirableMap::new())); @@ -117,7 +118,7 @@ async fn peer_connection_healthcheck( match peer_connection_healthcheck_rpc(cfg, &signed_message.address).await { Ok(response) => { if response["result"] == serde_json::json!(true) { - know_peers.insert(signed_message.address.clone(), (), KNOW_PEER_EXPIRATION); + know_peers.insert(signed_message.address.clone(), (), know_peer_expiration); } else { tracked_log( log::Level::Warn,