From: Michael Tremer Date: Mon, 6 Oct 2025 17:03:52 +0000 (+0000) Subject: source: Stagger sources to avoid load peaks X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=360fce41179c347ee12f0b68760dc8de4198c2c5;p=telemetry.git source: Stagger sources to avoid load peaks Signed-off-by: Michael Tremer --- diff --git a/src/daemon/source.c b/src/daemon/source.c index cd07503..fd53b31 100644 --- a/src/daemon/source.c +++ b/src/daemon/source.c @@ -38,6 +38,11 @@ #include "time.h" #include "util.h" +// When the daemon starts up, we should not collect everything straight away to +// avoid any usage peaks. Therefore we will stagger all sources randomly across +// N seconds. +#define STAGGER 60 + #define STEPSIZE 60 // seconds // Interval after which the heartbeat function is being called again @@ -326,15 +331,19 @@ static int collecty_source_heartbeat(sd_event_source* source, uint64_t usec, voi } static int collecty_source_register_heartbeat(collecty_source* self) { + uint64_t heartbeat = 0; int r; // No need to do this if we don't have a collect method if (!self->impl->collect) return 0; + // To avoid any load peaks, stagger sources + heartbeat = SEC_TO_USEC(rand() % STAGGER); + // Call the heartbeat function immediately r = sd_event_add_time_relative(self->loop, &self->events.heartbeat, - CLOCK_MONOTONIC, 0, 0, collecty_source_heartbeat, self); + CLOCK_MONOTONIC, heartbeat, 0, collecty_source_heartbeat, self); if (r < 0) { ERROR(self->ctx, "Failed to register the heartbeat timer: %s\n", strerror(-r)); return r;