-
Notifications
You must be signed in to change notification settings - Fork 323
/
pool.go
367 lines (310 loc) · 6.59 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package email
import (
"crypto/tls"
"errors"
"io"
"net"
"net/mail"
"net/smtp"
"net/textproto"
"sync"
"syscall"
"time"
)
type Pool struct {
addr string
auth smtp.Auth
max int
created int
clients chan *client
rebuild chan struct{}
mut *sync.Mutex
lastBuildErr *timestampedErr
closing chan struct{}
tlsConfig *tls.Config
helloHostname string
}
type client struct {
*smtp.Client
failCount int
}
type timestampedErr struct {
err error
ts time.Time
}
const maxFails = 4
var (
ErrClosed = errors.New("pool closed")
ErrTimeout = errors.New("timed out")
)
func NewPool(address string, count int, auth smtp.Auth, opt_tlsConfig ...*tls.Config) (pool *Pool, err error) {
pool = &Pool{
addr: address,
auth: auth,
max: count,
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]
} else if host, _, e := net.SplitHostPort(address); e != nil {
return nil, e
} else {
pool.tlsConfig = &tls.Config{ServerName: host}
}
return
}
// go1.1 didn't have this method
func (c *client) Close() error {
return c.Text.Close()
}
// SetHelloHostname optionally sets the hostname that the Go smtp.Client will
// use when doing a HELLO with the upstream SMTP server. By default, Go uses
// "localhost" which may not be accepted by certain SMTP servers that demand
// an FQDN.
func (p *Pool) SetHelloHostname(h string) {
p.helloHostname = h
}
func (p *Pool) get(timeout time.Duration) *client {
select {
case c := <-p.clients:
return c
default:
}
if p.created < p.max {
p.makeOne()
}
var deadline <-chan time.Time
if timeout >= 0 {
deadline = time.After(timeout)
}
for {
select {
case c := <-p.clients:
return c
case <-p.rebuild:
p.makeOne()
case <-deadline:
return nil
case <-p.closing:
return nil
}
}
}
func shouldReuse(err error) bool {
// certainly not perfect, but might be close:
// - EOF: clearly, the connection went down
// - textproto.Errors were valid SMTP over a valid connection,
// but resulted from an SMTP error response
// - textproto.ProtocolErrors result from connections going down,
// invalid SMTP, that sort of thing
// - syscall.Errno is probably down connection/bad pipe, but
// passed straight through by textproto instead of becoming a
// ProtocolError
// - if we don't recognize the error, don't reuse the connection
// A false positive will probably fail on the Reset(), and even if
// not will eventually hit maxFails.
// A false negative will knock over (and trigger replacement of) a
// conn that might have still worked.
if err == io.EOF {
return false
}
switch err.(type) {
case *textproto.Error:
return true
case *textproto.ProtocolError, textproto.ProtocolError:
return false
case syscall.Errno:
return false
default:
return false
}
}
func (p *Pool) replace(c *client) {
p.clients <- c
}
func (p *Pool) inc() bool {
if p.created >= p.max {
return false
}
p.mut.Lock()
defer p.mut.Unlock()
if p.created >= p.max {
return false
}
p.created++
return true
}
func (p *Pool) dec() {
p.mut.Lock()
p.created--
p.mut.Unlock()
select {
case p.rebuild <- struct{}{}:
default:
}
}
func (p *Pool) makeOne() {
go func() {
if p.inc() {
if c, err := p.build(); err == nil {
p.clients <- c
} else {
p.lastBuildErr = ×tampedErr{err, time.Now()}
p.dec()
}
}
}()
}
func startTLS(c *client, t *tls.Config) (bool, error) {
if ok, _ := c.Extension("STARTTLS"); !ok {
return false, nil
}
if err := c.StartTLS(t); err != nil {
return false, err
}
return true, nil
}
func addAuth(c *client, auth smtp.Auth) (bool, error) {
if ok, _ := c.Extension("AUTH"); !ok {
return false, nil
}
if err := c.Auth(auth); err != nil {
return false, err
}
return true, nil
}
func (p *Pool) build() (*client, error) {
cl, err := smtp.Dial(p.addr)
if err != nil {
return nil, err
}
// Is there a custom hostname for doing a HELLO with the SMTP server?
if p.helloHostname != "" {
cl.Hello(p.helloHostname)
}
c := &client{cl, 0}
if _, err := startTLS(c, p.tlsConfig); err != nil {
c.Close()
return nil, err
}
if p.auth != nil {
if _, err := addAuth(c, p.auth); err != nil {
c.Close()
return nil, err
}
}
return c, nil
}
func (p *Pool) maybeReplace(err error, c *client) {
if err == nil {
c.failCount = 0
p.replace(c)
return
}
c.failCount++
if c.failCount >= maxFails {
goto shutdown
}
if !shouldReuse(err) {
goto shutdown
}
if err := c.Reset(); err != nil {
goto shutdown
}
p.replace(c)
return
shutdown:
p.dec()
c.Close()
}
func (p *Pool) failedToGet(startTime time.Time) error {
select {
case <-p.closing:
return ErrClosed
default:
}
if p.lastBuildErr != nil && startTime.Before(p.lastBuildErr.ts) {
return p.lastBuildErr.err
}
return ErrTimeout
}
// Send sends an email via a connection pulled from the Pool. The timeout may
// be <0 to indicate no timeout. Otherwise reaching the timeout will produce
// and error building a connection that occurred while we were waiting, or
// otherwise ErrTimeout.
func (p *Pool) Send(e *Email, timeout time.Duration) (err error) {
start := time.Now()
c := p.get(timeout)
if c == nil {
return p.failedToGet(start)
}
defer func() {
p.maybeReplace(err, c)
}()
recipients, err := addressLists(e.To, e.Cc, e.Bcc)
if err != nil {
return
}
msg, err := e.Bytes()
if err != nil {
return
}
from, err := emailOnly(e.From)
if err != nil {
return
}
if err = c.Mail(from); err != nil {
return
}
for _, recip := range recipients {
if err = c.Rcpt(recip); err != nil {
return
}
}
w, err := c.Data()
if err != nil {
return
}
if _, err = w.Write(msg); err != nil {
return
}
err = w.Close()
return
}
func emailOnly(full string) (string, error) {
addr, err := mail.ParseAddress(full)
if err != nil {
return "", err
}
return addr.Address, nil
}
func addressLists(lists ...[]string) ([]string, error) {
length := 0
for _, lst := range lists {
length += len(lst)
}
combined := make([]string, 0, length)
for _, lst := range lists {
for _, full := range lst {
addr, err := emailOnly(full)
if err != nil {
return nil, err
}
combined = append(combined, addr)
}
}
return combined, nil
}
// Close immediately changes the pool's state so no new connections will be
// created, then gets and closes the existing ones as they become available.
func (p *Pool) Close() {
close(p.closing)
for p.created > 0 {
c := <-p.clients
c.Quit()
p.dec()
}
}