Skip to content

Commit a9d6e7b

Browse files
committed
handlers/email: More precise name, fix data race possibility
1 parent 2fe2d7b commit a9d6e7b

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

handlers/email/email.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535

3636
// Email is an instance of the email logger
3737
type Email struct {
38+
enabled bool
3839
formatter Formatter
3940
formatFunc FormatFunc
4041
timestampFormat string
@@ -45,22 +46,21 @@ type Email struct {
4546
password string
4647
from string
4748
to []string
48-
send bool
4949
rw sync.RWMutex
5050
once sync.Once
5151
}
5252

5353
// New returns a new instance of the email logger
5454
func New(host string, port int, username string, password string, from string, to []string) *Email {
5555
e := &Email{
56+
enabled: true,
5657
timestampFormat: log.DefaultTimeFormat,
5758
host: host,
5859
port: port,
5960
username: username,
6061
password: password,
6162
from: from,
6263
to: to,
63-
send: true,
6464
formatFunc: defaultFormatFunc,
6565
}
6666
e.SetTemplate(defaultTemplate)
@@ -130,12 +130,12 @@ func (email *Email) SetEmailConfig(host string, port int, username string, passw
130130
email.formatter = email.formatFunc(email)
131131
}
132132

133-
// SetSend enables or disables the email handler sending emails
134-
func (email *Email) SetSend(send bool) {
133+
// SetEnabled enables or disables the email handler sending emails
134+
func (email *Email) SetEnabled(enabled bool) {
135135
email.rw.Lock()
136136
defer email.rw.Unlock()
137137

138-
email.send = send
138+
email.enabled = enabled
139139
}
140140

141141
func defaultFormatFunc(email *Email) Formatter {
@@ -165,12 +165,18 @@ func defaultFormatFunc(email *Email) Formatter {
165165

166166
// Log handles the log entry
167167
func (email *Email) Log(e log.Entry) {
168-
if !email.send {
168+
email.rw.RLock()
169+
170+
if !email.enabled {
169171
return
170172
}
173+
174+
email.rw.RUnlock()
175+
171176
email.once.Do(func() {
172177
email.formatter = email.formatFunc(email)
173178
})
179+
174180
var s gomail.SendCloser
175181
var err error
176182
var open bool

handlers/email/email_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestEmailHandler(t *testing.T) {
104104
email.SetTimestampFormat("MST")
105105
email.SetTemplate(defaultTemplate)
106106
email.SetEmailConfig("localhost", 3041, "", "", "[email protected]", []string{"[email protected]"})
107-
email.SetSend(true)
107+
email.SetEnabled(true)
108108
// email.SetFormatFunc(testFormatFunc)
109109
log.AddHandler(email, log.InfoLevel, log.DebugLevel)
110110

@@ -175,7 +175,6 @@ func TestBadEmailTemplate(t *testing.T) {
175175
}
176176

177177
func TestBadSend(t *testing.T) {
178-
179178
email := New("localhost", 3041, "", "", "[email protected]", []string{"[email protected]"})
180179
log.AddHandler(email, log.InfoLevel)
181180

0 commit comments

Comments
 (0)