summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/notifier/email.go18
-rw-r--r--internal/notifier/notifier.go65
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 {