diff --git a/email.go b/email.go index 57d1b53..69b7932 100644 --- a/email.go +++ b/email.go @@ -301,7 +301,7 @@ func (e *Email) AttachFile(filename string) (a *Attachment, err error) { func (e *Email) msgHeaders() (textproto.MIMEHeader, error) { res := make(textproto.MIMEHeader, len(e.Headers)+6) if e.Headers != nil { - for _, h := range []string{"Reply-To", "To", "Cc", "From", "Subject", "Date", "Message-Id", "MIME-Version"} { + for _, h := range []string{"Reply-To", "To", "Cc", "From", "Subject", "Date", "Message-ID", "MIME-Version"} { if v, ok := e.Headers[h]; ok { res[h] = v } @@ -320,12 +320,12 @@ func (e *Email) msgHeaders() (textproto.MIMEHeader, error) { if _, ok := res["Subject"]; !ok && e.Subject != "" { res.Set("Subject", e.Subject) } - if _, ok := res["Message-Id"]; !ok { - id, err := generateMessageID() + if _, ok := res["Message-ID"]; !ok { + ID, err := generateMessageID() if err != nil { return nil, err } - res.Set("Message-Id", id) + res["Message-ID"] = []string{ID} } // Date and From are required headers. if _, ok := res["From"]; !ok { @@ -335,7 +335,7 @@ func (e *Email) msgHeaders() (textproto.MIMEHeader, error) { res.Set("Date", time.Now().Format(time.RFC1123Z)) } if _, ok := res["MIME-Version"]; !ok { - res.Set("MIME-Version", "1.0") + res["MIME-Version"] = []string{"1.0"} } for field, vals := range e.Headers { if _, ok := res[field]; !ok { diff --git a/pool.go b/pool.go index 67f224a..75f75c5 100644 --- a/pool.go +++ b/pool.go @@ -8,7 +8,7 @@ import ( "net/mail" "net/smtp" "net/textproto" - "sync" + "sync/atomic" "syscall" "time" ) @@ -17,10 +17,9 @@ type Pool struct { addr string auth smtp.Auth max int - created int + created int32 clients chan *client rebuild chan struct{} - mut *sync.Mutex lastBuildErr *timestampedErr closing chan struct{} tlsConfig *tls.Config @@ -52,7 +51,6 @@ func NewPool(address string, count int, auth smtp.Auth, opt_tlsConfig ...*tls.Co clients: make(chan *client, count), rebuild: make(chan struct{}), closing: make(chan struct{}), - mut: &sync.Mutex{}, } if len(opt_tlsConfig) == 1 { pool.tlsConfig = opt_tlsConfig[0] @@ -84,7 +82,7 @@ func (p *Pool) get(timeout time.Duration) *client { default: } - if p.created < p.max { + if int(atomic.LoadInt32(&p.created)) < p.max { p.makeOne() } @@ -142,24 +140,15 @@ func (p *Pool) replace(c *client) { } func (p *Pool) inc() bool { - if p.created >= p.max { + if int(atomic.LoadInt32(&p.created)) >= p.max { return false } - - p.mut.Lock() - defer p.mut.Unlock() - - if p.created >= p.max { - return false - } - p.created++ + atomic.AddInt32(&p.created, 1) return true } func (p *Pool) dec() { - p.mut.Lock() - p.created-- - p.mut.Unlock() + atomic.AddInt32(&p.created, -1) select { case p.rebuild <- struct{}{}: @@ -359,7 +348,7 @@ func addressLists(lists ...[]string) ([]string, error) { func (p *Pool) Close() { close(p.closing) - for p.created > 0 { + for atomic.LoadInt32(&p.created) > 0 { c := <-p.clients c.Quit() p.dec()