Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add smtp retries #4115

Merged
merged 6 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions remotemonitor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Config struct {
// ServerAddr is the address of the SMTP server, including port.
ServerAddr string
User, Pass string

// Retries is the number of times to retry sending with an email integration key.
Retries int
}

// Instances determine what remote GoAlert instances will be monitored and send potential errors.
Expand Down Expand Up @@ -94,6 +97,9 @@ func (cfg Config) Validate() error {
if cfg.SMTP.From == "" {
return errors.New("SMTP from address is required")
}
if cfg.SMTP.Retries > 5 || cfg.SMTP.Retries < 0 {
return errors.New("SMTP retries must be between 0 and 5")
}

return nil
}
25 changes: 18 additions & 7 deletions remotemonitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package remotemonitor

import (
"context"
"errors"
"fmt"
"io"
"log"
Expand All @@ -14,8 +13,10 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/target/goalert/config"
"github.com/target/goalert/notification/twilio"
"github.com/target/goalert/retry"
)

// Monitor will check for functionality and communication between itself and one or more instances.
Expand Down Expand Up @@ -187,12 +188,22 @@ func (m *Monitor) createEmailAlert(i Instance, dedup, summary, details string) e
auth = smtp.PlainAuth("", m.cfg.SMTP.User, m.cfg.SMTP.Pass, host)
}

err = smtp.SendMail(m.cfg.SMTP.ServerAddr, auth, m.cfg.SMTP.From, []string{addr.Address}, []byte(msg))
if err != nil {
return fmt.Errorf("send email: %w", err)
}

return nil
err = retry.DoTemporaryError(func(_ int) error {
err = smtp.SendMail(m.cfg.SMTP.ServerAddr, auth, m.cfg.SMTP.From, []string{addr.Address}, []byte(msg))
if err == nil {
return nil
}
if strings.HasPrefix(err.Error(), "4") { // SMTP server return codes beginning with 4 are considered transient
nimjor marked this conversation as resolved.
Show resolved Hide resolved
err = retry.TemporaryError(err)
}
return errors.Wrap(err, "send email")
},
retry.Log(m.context()),
retry.Limit(m.cfg.SMTP.Retries),
retry.FibBackoff(time.Second),
)

return err
}

func (m *Monitor) loop() {
Expand Down
Loading