Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #90, #141 pool is now atomic #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
25 changes: 7 additions & 18 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/mail"
"net/smtp"
"net/textproto"
"sync"
"sync/atomic"
"syscall"
"time"
)
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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{}{}:
Expand Down Expand Up @@ -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()
Expand Down