From 3aae06ff6b802ea4450e5d5c500576ed56e43fe1 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Wed, 18 Jun 2025 17:04:07 +1200 Subject: [PATCH] Fix: Improve version polling, add thread safety and exponential backoff (#523) Squashed commit of the following: commit 1ed713dd8de2adb7d761e20bb8018804c2e27ea6 Author: Ralph Slooten Date: Wed Jun 18 17:03:36 2025 +1200 Refactor latest version caching, add console logging if update checks fails commit bf880e583372d81a0597bc263ab22f6989e48fa9 Author: Ben Edmunds Date: Wed Jun 18 05:52:35 2025 +0100 Fix: Improve version polling, add thread safety and exponential backoff (#523) * make version polling thread safe and add expo backoff * tidy up --- internal/stats/stats.go | 59 +++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/internal/stats/stats.go b/internal/stats/stats.go index 1d514a2..25ccd9f 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -7,17 +7,30 @@ import ( "time" "github.com/axllent/mailpit/config" + "github.com/axllent/mailpit/internal/logger" "github.com/axllent/mailpit/internal/storage" "github.com/axllent/mailpit/internal/updater" ) +// Stores cached version along with its expiry time and error count. +// Used to minimize repeated version lookups and track consecutive errors. +type versionCache struct { + // github version string + value string + // time to expire the cache + expiry time.Time + // count of consecutive errors + errCount int +} + var ( - // to prevent hammering Github for latest version - latestVersionCache string + // Version cache storing the latest GitHub version + vCache versionCache // StartedAt is set to the current ime when Mailpit starts startedAt time.Time + // sync mutex to prevent race condition with simultaneous requests mu sync.RWMutex smtpAccepted uint64 @@ -62,6 +75,12 @@ type AppInformation struct { } } +// Calculates exponential backoff duration based on the error count. +func getBackoff(errCount int) time.Duration { + backoff := min(time.Duration(1<