summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-11-13 22:30:51 +0200
committerPaul Buetow <paul@buetow.org>2023-11-13 22:30:51 +0200
commit25a372627b98744e53936118d69cb8ebb72c3004 (patch)
tree0e16a91095361be70e9ad94437f4cd0d83564d84
parent17b5c9584c9d84ef24e1a979ccc1433336eb9422 (diff)
in theory, sendEmail throttling should work
-rw-r--r--internal/notifier/notifier.go16
1 files 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
}