From 25a372627b98744e53936118d69cb8ebb72c3004 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 13 Nov 2023 22:30:51 +0200 Subject: in theory, sendEmail throttling should work --- internal/notifier/notifier.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/notifier/notifier.go b/internal/notifier/notifier.go index ec9f521..101303b 100644 --- a/internal/notifier/notifier.go +++ b/internal/notifier/notifier.go @@ -20,7 +20,8 @@ func (notifier Notifier) Start(ctx context.Context, conf config.Config, scoreCh changedCh := make(chan email, 1) errorCh := make(chan email, 1) - push := func(ch chan email, email email) { + update := func(ch chan email, email email) { + // Replace (update) current element in the channel. if cap(ch) == len(ch) { <-ch } @@ -28,19 +29,20 @@ func (notifier Notifier) Start(ctx context.Context, conf config.Config, scoreCh } go func() { + go notifier.sendEmail(ctx, "change report", conf, changedCh) + go notifier.sendEmail(ctx, "error report", conf, errorCh) + for scoresStr := range scoreCh { if err := notifier.persistToDisk(conf, scoresStr); err != nil { - push(errorCh, email{"GORUM error", err.Error()}) + update(errorCh, email{"GORUM error", err.Error()}) } - push(changedCh, email{"GORUM changed", scoresStr}) + update(changedCh, email{"GORUM changed", scoresStr}) } }() - go notifier.throttler(ctx, conf, changedCh) - go notifier.throttler(ctx, conf, errorCh) } -func (notifier Notifier) throttler(ctx context.Context, conf config.Config, ch <-chan email) { +func (notifier Notifier) sendEmail(ctx context.Context, what string, conf config.Config, ch <-chan email) { throttleDuration := time.Duration(conf.MailThrottle) for { @@ -53,9 +55,9 @@ func (notifier Notifier) throttler(ctx context.Context, conf config.Config, ch < return } - log.Println("notifier: sleeping some seconds before next E-Mail notification", throttleDuration) select { case <-time.After(throttleDuration): + log.Println("notifier:", what, "slept some seconds", throttleDuration) case <-ctx.Done(): return } -- cgit v1.2.3