[pdm-devel] [PATCH proxmox-datacenter-manager v2 27/28] metric collection: skip missed timer ticks
Lukas Wagner
l.wagner at proxmox.com
Fri Feb 14 14:06:52 CET 2025
The default behavior of `tokio::time::Interval` when a tick is missed
is 'burst', which is to shorten the timer intervals until the original
alignment is restored, while keeping the *number of ticks* the same
the same as expected.
Example from the official tokio docs [1] for burst mode:
Expected: | 1 | 2 | 3 | 4 | 5 | 6 |
Actual: | work ---| delay |work|work|work-|work-----|
For metric collection, we do not really gain anything from bursting
missed ticks. For us, 'skip' is fine. There, the alignment is
immediately restored by skipping any ticks that have been missed:
Expected: | 1 | 2 | 3 | 4 | 5 | 6 |
Actual: | work ---| delay |work---| work----| work----|
https://docs.rs/tokio/latest/tokio/time/enum.MissedTickBehavior.html
Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
Notes:
New in v2.
server/src/metric_collection/collection_task.rs | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/server/src/metric_collection/collection_task.rs b/server/src/metric_collection/collection_task.rs
index 954df0d6..5c6e2762 100644
--- a/server/src/metric_collection/collection_task.rs
+++ b/server/src/metric_collection/collection_task.rs
@@ -10,7 +10,7 @@ use tokio::{
mpsc::{Receiver, Sender},
oneshot, OwnedSemaphorePermit, Semaphore,
},
- time::Interval,
+ time::{Interval, MissedTickBehavior},
};
use proxmox_section_config::typed::SectionConfigData;
@@ -230,6 +230,13 @@ impl MetricCollectionTask {
fn setup_timer(interval: u64) -> (Interval, Instant) {
log::debug!("setting metric collection interval timer to {interval} seconds.",);
let mut timer = tokio::time::interval(Duration::from_secs(interval));
+
+ // If we miss a tick because a previous collection run took too long, we want to
+ // tick as soon as possible, but we do not need to repeat missing ticks.
+ // We do want to stay aligned, though.
+ // https://docs.rs/tokio/latest/tokio/time/enum.MissedTickBehavior.html#variant.Skip
+ timer.set_missed_tick_behavior(MissedTickBehavior::Skip);
+
let first_run = task_utils::next_aligned_instant(interval);
timer.reset_at(first_run.into());
--
2.39.5
More information about the pdm-devel
mailing list