Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extend configuration interface with peer_healthcheck_caching_secs
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
onur-ozkan committed Sep 24, 2024
1 parent 9ba90c1 commit 4f4f4eb
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/ctx.rs
Original file line number Diff line number Diff line change
@@ -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<AppConfig> = OnceCell::new();

pub(crate) fn get_app_config() -> &'static AppConfig {
@@ -58,6 +63,13 @@ pub(crate) struct AppConfig {
pub(crate) proxy_routes: Vec<ProxyRoute>,
/// 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();
5 changes: 3 additions & 2 deletions src/proxy/http/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Mutex<ExpirableMap<String, ()>>> =
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,

0 comments on commit 4f4f4eb

Please sign in to comment.