From 0b91404c4f9e85570ca96f6bac3286b89b46f618 Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Fri, 20 Apr 2018 20:33:00 -0500 Subject: [PATCH] Resetting connection properly when the underlying TCP connection breaks. Fixes #997 --- mailer/mailer.go | 14 +++++++++++--- mailer/mailer_test.go | 17 +++++++++++------ mailer/mockmailer.go | 1 + 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/mailer/mailer.go b/mailer/mailer.go index 981d90d3..771a8f1c 100644 --- a/mailer/mailer.go +++ b/mailer/mailer.go @@ -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 } } diff --git a/mailer/mailer_test.go b/mailer/mailer_test.go index 24354a20..a1573c39 100644 --- a/mailer/mailer_test.go +++ b/mailer/mailer_test.go @@ -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 diff --git a/mailer/mockmailer.go b/mailer/mockmailer.go index a5fc230d..7226e94f 100644 --- a/mailer/mockmailer.go +++ b/mailer/mockmailer.go @@ -147,6 +147,7 @@ func (mm *mockMessage) GetDialer() (Dialer, error) { func (mm *mockMessage) Backoff(reason error) error { mm.backoffCount++ + mm.err = reason return nil }