Fix unproblematic race in parallel notify-mailer (#6273)

The newly-parallel notify-mailer has a potential race where multiple
goroutines all try to set an option on the email template at the same
time. This is not an issue, as they're all setting the same option, but
fix it anyway by moving that option-setting to before the parallelism
begins.
This commit is contained in:
Aaron Gable 2022-08-03 15:37:54 -07:00 committed by GitHub
parent f5525ccd15
commit 07e96e326f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 4 deletions

View File

@ -109,9 +109,6 @@ func sortAddresses(input addressToRecipientMap) []string {
func (m *mailer) makeMessageBody(recipients []recipient) (string, error) {
var messageBody strings.Builder
// Ensure that in the event of a missing key, an informative error is
// returned.
m.emailTemplate.Option("missingkey=error")
err := m.emailTemplate.Execute(&messageBody, recipients)
if err != nil {
return "", err
@ -555,6 +552,10 @@ func main() {
template, err := template.ParseFiles(*bodyFile)
cmd.FailOnError(err, "Couldn't parse message template")
// Ensure that in the event of a missing key, an informative error is
// returned.
template.Option("missingkey=error")
address, err := mail.ParseAddress(*from)
cmd.FailOnError(err, fmt.Sprintf("Couldn't parse %q to address", *from))

View File

@ -234,7 +234,7 @@ func TestMakeMessageBody(t *testing.T) {
m := &mailer{
log: blog.UseMock(),
mailer: &mocks.Mailer{},
emailTemplate: template.Must(template.New("email").Parse(emailTemplate)),
emailTemplate: template.Must(template.New("email").Parse(emailTemplate)).Option("missingkey=error"),
sleepInterval: 0,
targetRange: interval{end: "\xFF"},
clk: newFakeClock(t),