diff options
| author | Paul Buetow <paul@buetow.org> | 2023-11-09 00:01:37 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-11-09 00:01:37 +0200 |
| commit | 17b5c9584c9d84ef24e1a979ccc1433336eb9422 (patch) | |
| tree | 02e1174efaeeb439af8e43e459e256808f86a11c | |
| parent | 869fed9c8f8eb50f8666cf5d450c9f587e198d05 (diff) | |
have an error and a quorum change email throttling ch
| -rw-r--r-- | internal/notifier/email.go | 18 | ||||
| -rw-r--r-- | internal/notifier/notifier.go | 65 |
2 files changed, 39 insertions, 44 deletions
diff --git a/internal/notifier/email.go b/internal/notifier/email.go index 4ce8d69..423b763 100644 --- a/internal/notifier/email.go +++ b/internal/notifier/email.go @@ -8,16 +8,20 @@ import ( "codeberg.org/snonux/gorum/internal/config" ) -func emailNotify(conf config.Config, subject, body string) error { +type email struct { + subject, body string +} + +func (em email) send(conf config.Config) error { if !conf.EmailNotifycationEnabled() { return nil } - log.Println("notify:", subject, body) + log.Println("notify:", em.subject, em.body) headers := map[string]string{ "From": conf.EmailFrom, "To": conf.EmailTo, - "Subject": subject, + "Subject": em.subject, "MIME-Version": "1.0", "Content-Type": "text/plain; charset=\"utf-8\"", } @@ -27,15 +31,9 @@ func emailNotify(conf config.Config, subject, body string) error { header += fmt.Sprintf("%s: %s\r\n", k, v) } - message := header + "\r\n" + body + message := header + "\r\n" + em.body log.Println("Using SMTP server", conf.SMTPServer) return smtp.SendMail(conf.SMTPServer, nil, conf.EmailFrom, []string{conf.EmailTo}, []byte(message)) } - -func emailNotifyError(conf config.Config, err error) { - if err := emailNotify(conf, fmt.Sprintf("GORUM: An error occured: %v", err), err.Error()); err != nil { - log.Println("error:", err) - } -} diff --git a/internal/notifier/notifier.go b/internal/notifier/notifier.go index 70e6096..ec9f521 100644 --- a/internal/notifier/notifier.go +++ b/internal/notifier/notifier.go @@ -17,52 +17,49 @@ func New() Notifier { } func (notifier Notifier) Start(ctx context.Context, conf config.Config, scoreCh <-chan string) { - // Email throttle channel - ch := make(chan string, 1) + changedCh := make(chan email, 1) + errorCh := make(chan email, 1) + + push := func(ch chan email, email email) { + if cap(ch) == len(ch) { + <-ch + } + ch <- email + } go func() { for scoresStr := range scoreCh { if err := notifier.persistToDisk(conf, scoresStr); err != nil { - log.Println(err) - // TODO: This kind of email should be throttled, too. In case - // there are many errors! - emailNotifyError(conf, err) - } - if cap(ch) == len(ch) { - // Remove the curret score, and replace it with a new score - <-ch - ch <- scoresStr + push(errorCh, email{"GORUM error", err.Error()}) } + push(changedCh, email{"GORUM changed", scoresStr}) } }() - // Throttle how quickly quorum changes are sent out via email. - go func() { - throttleDuration := time.Duration(conf.MailThrottle) - - for { - select { - case scoresStr := <-ch: - if err := notifier.sendEmail(conf, scoresStr); err != nil { - log.Println(err) - } - case <-ctx.Done(): - return - } + go notifier.throttler(ctx, conf, changedCh) + go notifier.throttler(ctx, conf, errorCh) +} - log.Println("notifier: sleeping some seconds before next E-Mail notification", throttleDuration) - select { - case <-time.After(throttleDuration): - case <-ctx.Done(): - return - } +func (notifier Notifier) throttler(ctx context.Context, conf config.Config, ch <-chan email) { + throttleDuration := time.Duration(conf.MailThrottle) + for { + select { + case email := <-ch: + if err := email.send(conf); err != nil { + log.Println(err) + } + case <-ctx.Done(): + return } - }() -} -func (notifier Notifier) sendEmail(conf config.Config, scoresStr string) error { - return emailNotify(conf, "GORUM: Quorum changed", scoresStr) + log.Println("notifier: sleeping some seconds before next E-Mail notification", throttleDuration) + select { + case <-time.After(throttleDuration): + case <-ctx.Done(): + return + } + } } func (notifier Notifier) persistToDisk(conf config.Config, scoresStr string) error { |
