From 1ec99f2476257aef733bada29bffe3e8b83f7b6b Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 13 Nov 2023 22:46:33 +0200 Subject: fix duration - minor refactor --- internal/notifier/notifier.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/internal/notifier/notifier.go b/internal/notifier/notifier.go index 5fa1936..1f228f9 100644 --- a/internal/notifier/notifier.go +++ b/internal/notifier/notifier.go @@ -11,21 +11,21 @@ import ( ) func Start(ctx context.Context, conf config.Config, scoreCh <-chan string) { + log.Println("notifier: Starting") + changedCh := make(chan email, 1) errorCh := make(chan email, 1) update := func(ch chan email, email email) { // Replace (update) current element in the channel. - if cap(ch) == len(ch) { - <-ch + select { + case <-ch: + default: } ch <- email } go func() { - go sendEmail(ctx, "change report", conf, changedCh) - go sendEmail(ctx, "error report", conf, errorCh) - for scoresStr := range scoreCh { if err := persistToDisk(conf, scoresStr); err != nil { update(errorCh, email{"GORUM error", err.Error()}) @@ -33,10 +33,13 @@ func Start(ctx context.Context, conf config.Config, scoreCh <-chan string) { update(changedCh, email{"GORUM changed", scoresStr}) } }() + + go sendEmail(ctx, "change report", conf, changedCh) + go sendEmail(ctx, "error report", conf, errorCh) } func sendEmail(ctx context.Context, what string, conf config.Config, ch <-chan email) { - throttleDuration := time.Duration(conf.MailThrottle) + throttleDuration := time.Duration(conf.MailThrottle) * time.Second for { select { @@ -50,7 +53,7 @@ func sendEmail(ctx context.Context, what string, conf config.Config, ch <-chan e select { case <-time.After(throttleDuration): - log.Println("notifier:", what, "slept some seconds", throttleDuration) + log.Println("notifier:", what, "slept some time", throttleDuration) case <-ctx.Done(): return } -- cgit v1.2.3