From 60541ef54f91cb65c9af1c7194ad559b0b80b3c3 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 31 Jul 2026 09:00:26 +0000 Subject: [PATCH] fix: add jitter to initial ping delays This introduces a cryptographically secure random delay between 0 and `ping_interval` when starting a new pinger. This staggers initial pings evenly across the ping interval, rather than all pingers starting after a hardcoded 1-second delay, effectively preventing massive ping bursts and rate limiter congestion on large network segments. Co-authored-by: kljama <176691597+kljama@users.noreply.github.com> --- internal/monitoring/pinger.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/monitoring/pinger.go b/internal/monitoring/pinger.go index eb9a76c..a1744c3 100644 --- a/internal/monitoring/pinger.go +++ b/internal/monitoring/pinger.go @@ -2,7 +2,9 @@ package monitoring import ( "context" + "crypto/rand" "fmt" + "math/big" "net" "strings" "sync" @@ -51,8 +53,22 @@ func StartPinger(ctx context.Context, wg *sync.WaitGroup, device state.Device, i pingOp = performPing } - // Initialize timer for first ping with 1 second delay to avoid immediate ping storm - timer := time.NewTimer(1 * time.Second) + // Initialize timer for first ping with jitter to avoid immediate ping storm + // Jitter is random between 0 and interval to spread out initial bursts + initialDelay := 1 * time.Second + if interval > 0 { + val, err := rand.Int(rand.Reader, big.NewInt(int64(interval))) + if err == nil { + initialDelay = time.Duration(val.Int64()) + } else { + log.Warn(). + Str("ip", device.IP). + Err(err). + Msg("Failed to generate random initial delay, using default 1s fallback") + } + } + + timer := time.NewTimer(initialDelay) defer timer.Stop() for {