Skip to content

Commit a9482c3

Browse files
author
Dean Karn
authored
Merge pull request #22 from go-playground/sync-email-handler
safely allow updating of email config
2 parents f6c76e8 + 75c8b80 commit a9482c3

File tree

2 files changed

+11
-30
lines changed

2 files changed

+11
-30
lines changed

handlers/email/email.go

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Email struct {
4545
password string
4646
from string
4747
to []string
48+
rw sync.RWMutex
4849
once sync.Once
4950
}
5051

@@ -104,33 +105,16 @@ func (email *Email) SetFormatFunc(fn FormatFunc) {
104105
email.formatFunc = fn
105106
}
106107

107-
// SetHost sets Email's host
108-
func (email *Email) SetHost(host string) {
109-
email.host = host
110-
}
108+
// SetEmailConfig allows updating of the email config in flight and is thread safe.
109+
func (email *Email) SetEmailConfig(host string, port int, username string, password string, from string, to []string) {
110+
email.rw.Lock()
111+
defer email.rw.Unlock()
111112

112-
// SetPort sets Email's port
113-
func (email *Email) SetPort(port int) {
113+
email.host = host
114114
email.port = port
115-
}
116-
117-
// SetUsername sets Email's username
118-
func (email *Email) SetUsername(username string) {
119115
email.username = username
120-
}
121-
122-
// SetPassword sets Email's password
123-
func (email *Email) SetPassword(password string) {
124116
email.password = password
125-
}
126-
127-
// SetFrom sets Email's email address to send from
128-
func (email *Email) SetFrom(from string) {
129117
email.from = from
130-
}
131-
132-
// SetTo sets Email's email address(es) to send to
133-
func (email *Email) SetTo(to []string) {
134118
email.to = to
135119
}
136120

@@ -144,15 +128,15 @@ func defaultFormatFunc(email *Email) Formatter {
144128
to := make([]string, len(email.to))
145129
copy(to, email.to)
146130

147-
template := email.Template()
131+
tmpl := email.Template()
148132
message := gomail.NewMessage()
149133

150134
message.SetHeader("From", email.from)
151135
message.SetHeader("To", to...)
152136

153137
return func(e log.Entry) *gomail.Message {
154138
b.Reset()
155-
_ = template.ExecuteTemplate(b, "email", e)
139+
_ = tmpl.ExecuteTemplate(b, "email", e)
156140
message.SetHeader("Subject", e.Message)
157141
message.SetBody(contentType, b.String())
158142
return message
@@ -171,7 +155,9 @@ func (email *Email) Log(e log.Entry) {
171155
var message *gomail.Message
172156
var count uint8
173157

158+
email.rw.RLock()
174159
d := gomail.NewDialer(email.host, email.port, email.username, email.password)
160+
email.rw.RUnlock()
175161

176162
for {
177163
count = 0

handlers/email/email_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,7 @@ func TestEmailHandler(t *testing.T) {
103103
email := New("localhost", 3041, "", "", "[email protected]", []string{"[email protected]"})
104104
email.SetTimestampFormat("MST")
105105
email.SetTemplate(defaultTemplate)
106-
email.SetHost("localhost")
107-
email.SetPort(3041)
108-
email.SetUsername("")
109-
email.SetPassword("")
110-
email.SetFrom("[email protected]")
111-
email.SetTo([]string{"[email protected]"})
106+
email.SetEmailConfig("localhost", 3041, "", "", "[email protected]", []string{"[email protected]"})
112107
// email.SetFormatFunc(testFormatFunc)
113108
log.AddHandler(email, log.InfoLevel, log.DebugLevel)
114109

0 commit comments

Comments
 (0)