Browse Source

Resetting connection properly when the underlying TCP connection breaks. Fixes #997

pull/504/merge
Jordan Wright 3 years ago
parent
commit
0b91404c4f
No known key found for this signature in database GPG Key ID: 138D5AD2331B3C11
3 changed files with 23 additions and 9 deletions
  1. +11
    -3
      mailer/mailer.go
  2. +11
    -6
      mailer/mailer_test.go
  3. +1
    -0
      mailer/mockmailer.go

+ 11
- 3
mailer/mailer.go View File

@ -146,7 +146,7 @@ func sendMail(ctx context.Context, dialer Dialer, ms []Mail) {
}
defer sender.Close()
message := gomail.NewMessage()
for _, m := range ms {
for i, m := range ms {
select {
case <-ctx.Done():
return
@ -187,8 +187,16 @@ func sendMail(ctx context.Context, dialer Dialer, ms []Mail) {
continue
}
} else {
m.Error(err)
sender.Reset()
// This likely indicates that something happened to the underlying
// connection. We'll try to reconnect and, if that fails, we'll
// error out the remaining emails.
origErr := err
sender, err = dialHost(ctx, dialer)
if err != nil {
errorMail(err, ms[i:])
break
}
m.Backoff(origErr)
continue
}
}

+ 11
- 6
mailer/mailer_test.go View File

@ -263,16 +263,21 @@ func (ms *MailerSuite) TestUnknownError() {
message := messages[0].(*mockMessage)
// Check that the first message did not perform a backoff
expectedBackoffCount := 0
// If we get an unexpected error, this means that it's likely the
// underlying connection dropped. When this happens, we expect the
// connection to be re-established (see #997).
// In this case, we're successfully reestablishing the connection
// so we expect the backoff to occur.
expectedBackoffCount := 1
backoffCount := message.backoffCount
if backoffCount != expectedBackoffCount {
ms.T().Fatalf("Did not receive expected backoff. Got backoffCount %d, Expected %d", backoffCount, expectedCount)
ms.T().Fatalf("Did not receive expected backoff. Got backoffCount %d, Expected %d", backoffCount, expectedBackoffCount)
}
// Check that there was a reset performed on the sender
if sender.resetCount != expectedCount {
ms.T().Fatalf("Did not receive expected reset. Got resetCount %d, expected %d", sender.resetCount, expectedCount)
// Check that the underlying connection was reestablished
expectedDialCount := 2
if dialer.dialCount != expectedDialCount {
ms.T().Fatalf("Did not receive expected dial count. Got %d expected %d", dialer.dialCount, expectedDialCount)
}
// Check that the email errored out appropriately

+ 1
- 0
mailer/mockmailer.go View File

@ -147,6 +147,7 @@ func (mm *mockMessage) GetDialer() (Dialer, error) {
func (mm *mockMessage) Backoff(reason error) error {
mm.backoffCount++
mm.err = reason
return nil
}

Loading…
Cancel
Save