From fb5e9947bd46e0eb5cda6dec8c1526529b80e763 Mon Sep 17 00:00:00 2001 From: Alexey <247128645+axkurcom@users.noreply.github.com> Date: Fri, 6 Mar 2026 18:54:12 +0300 Subject: [PATCH] Runtime Watch --- src/api/runtime_watch.rs | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/api/runtime_watch.rs diff --git a/src/api/runtime_watch.rs b/src/api/runtime_watch.rs new file mode 100644 index 0000000..0485e55 --- /dev/null +++ b/src/api/runtime_watch.rs @@ -0,0 +1,66 @@ +use std::sync::Arc; +use std::sync::atomic::Ordering; +use std::time::{SystemTime, UNIX_EPOCH}; + +use tokio::sync::watch; + +use crate::config::ProxyConfig; + +use super::ApiRuntimeState; +use super::events::ApiEventStore; + +pub(super) fn spawn_runtime_watchers( + config_rx: watch::Receiver>, + admission_rx: watch::Receiver, + runtime_state: Arc, + runtime_events: Arc, +) { + let mut config_rx_reload = config_rx; + let runtime_state_reload = runtime_state.clone(); + let runtime_events_reload = runtime_events.clone(); + tokio::spawn(async move { + loop { + if config_rx_reload.changed().await.is_err() { + break; + } + runtime_state_reload + .config_reload_count + .fetch_add(1, Ordering::Relaxed); + runtime_state_reload + .last_config_reload_epoch_secs + .store(now_epoch_secs(), Ordering::Relaxed); + runtime_events_reload.record("config.reload.applied", "config receiver updated"); + } + }); + + let mut admission_rx_watch = admission_rx; + tokio::spawn(async move { + runtime_state + .admission_open + .store(*admission_rx_watch.borrow(), Ordering::Relaxed); + runtime_events.record( + "admission.state", + format!("accepting_new_connections={}", *admission_rx_watch.borrow()), + ); + loop { + if admission_rx_watch.changed().await.is_err() { + break; + } + let admission_open = *admission_rx_watch.borrow(); + runtime_state + .admission_open + .store(admission_open, Ordering::Relaxed); + runtime_events.record( + "admission.state", + format!("accepting_new_connections={}", admission_open), + ); + } + }); +} + +fn now_epoch_secs() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_secs() +}