From 6e857563506ec7a2a247feff92f865ade40f36ff Mon Sep 17 00:00:00 2001 From: "GL.iNet-Yongping.Xie" Date: Wed, 8 Apr 2026 02:41:23 -0700 Subject: [PATCH] fix: suppress notification emails during server restart Skip device online/offline email notifications for 60s after startup to prevent inbox flooding from reconnect storms. Mirrors the existing device-log grace window. Remote access notifications are unaffected. Signed-off-by: GL.iNet-Yongping.Xie --- internal/domain/notification/service.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/domain/notification/service.go b/internal/domain/notification/service.go index 86a8ac4..47e0d41 100644 --- a/internal/domain/notification/service.go +++ b/internal/domain/notification/service.go @@ -10,12 +10,22 @@ import ( "github.com/rs/zerolog/log" ) +// startupGraceWindow mirrors the device-log grace period: device online/ +// offline emails are suppressed during this window to avoid a reconnect +// storm flooding inboxes after a server restart. +const startupGraceWindow = 60 * time.Second + type Service struct { - repo Repository + repo Repository + startupTime time.Time } func NewService(repo Repository) *Service { - return &Service{repo: repo} + return &Service{repo: repo, startupTime: time.Now()} +} + +func (s *Service) inGracePeriod() bool { + return time.Since(s.startupTime) < startupGraceWindow } // ─── SMTP config ──────────────────────────────────────────────── @@ -69,7 +79,11 @@ func (s *Service) RemoveRecipient(ctx context.Context, id int64) error { // ─── Event triggers (called from device runtime) ──────────────── // NotifyDeviceOnline sends a device-online notification if enabled. +// Suppressed during the startup grace window. func (s *Service) NotifyDeviceOnline(deviceID, mac string) { + if s.inGracePeriod() { + return + } s.sendEventNotification("deviceOnline", "[GLKVM Cloud] Device Online", "Device Online", []EmailField{ {Label: "Event", Value: "Device Online"}, {Label: "Device ID", Value: deviceID}, @@ -79,7 +93,11 @@ func (s *Service) NotifyDeviceOnline(deviceID, mac string) { } // NotifyDeviceOffline sends a device-offline notification if enabled. +// Suppressed during the startup grace window. func (s *Service) NotifyDeviceOffline(deviceID, mac string) { + if s.inGracePeriod() { + return + } s.sendEventNotification("deviceOffline", "[GLKVM Cloud] Device Offline", "Device Offline", []EmailField{ {Label: "Event", Value: "Device Offline"}, {Label: "Device ID", Value: deviceID},