pub struct SnapshotCache { /* private fields */ }Expand description
A cache that holds monitoring snapshots and refreshes them periodically.
When PrometheusMetrics are attached, the cache also updates Prometheus
gauges during each refresh, keeping metric values in lockstep with the
snapshot data. This means the /metrics handler never needs to compute
values — it only gathers and encodes.
Implementations§
Source§impl SnapshotCache
impl SnapshotCache
Sourcepub fn new(
refresh_interval: Duration,
server_source: Option<Arc<dyn ServerMonitoring + Send + Sync>>,
sv2_clients_source: Option<Arc<dyn Sv2ClientsMonitoring + Send + Sync>>,
) -> Self
pub fn new( refresh_interval: Duration, server_source: Option<Arc<dyn ServerMonitoring + Send + Sync>>, sv2_clients_source: Option<Arc<dyn Sv2ClientsMonitoring + Send + Sync>>, ) -> Self
Create a new snapshot cache with the given refresh interval.
§Arguments
refresh_interval- How often to refresh the cache (e.g., 15 seconds)server_source- Optional server monitoring trait objectsv2_clients_source- Optional Sv2 clients monitoring trait object
Sourcepub fn with_sv1_clients_source(
self,
sv1_source: Arc<dyn Sv1ClientsMonitoring + Send + Sync>,
) -> Self
pub fn with_sv1_clients_source( self, sv1_source: Arc<dyn Sv1ClientsMonitoring + Send + Sync>, ) -> Self
Add SV1 monitoring source (for Tproxy)
Sourcepub fn with_metrics(self, metrics: PrometheusMetrics) -> Self
pub fn with_metrics(self, metrics: PrometheusMetrics) -> Self
Attach (or replace) Prometheus metrics so they are updated during each refresh().
This is called once in MonitoringServer::new and may be called again in
with_sv1_monitoring which re-creates the metrics with SV1 gauges enabled.
Sourcepub fn get_snapshot(&self) -> MonitoringSnapshot
pub fn get_snapshot(&self) -> MonitoringSnapshot
Get the current snapshot.
This is a fast read that does NOT acquire any business logic locks.
The returned snapshot may be up to refresh_interval old.
Sourcepub fn refresh(&self)
pub fn refresh(&self)
Refresh the cache by reading from the data sources.
This method DOES acquire the business logic locks (via the trait methods), but it’s only called periodically by a background task, not on every request.
When Prometheus metrics are attached, they are updated atomically alongside the snapshot — eliminating any gap where metrics could be missing or stale relative to the snapshot data.
Sourcepub fn refresh_interval(&self) -> Duration
pub fn refresh_interval(&self) -> Duration
Get the refresh interval