-
Notifications
You must be signed in to change notification settings - Fork 31
/
pool.go
457 lines (394 loc) · 11.1 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Package smtppool creates a pool of reusable SMTP connections for high
// throughput e-mailing.
package smtppool
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/mail"
"net/smtp"
"net/textproto"
"sync/atomic"
"time"
)
// Opt represents SMTP pool options.
type Opt struct {
// Host is the SMTP server's hostname.
Host string `json:"host"`
// Port is the SMTP server port.
Port int `json:"port"`
// HelloHostname is the optional hostname to pass with the HELO command.
// Default is "localhost".
HelloHostname string `json:"hello_hostname"`
// MaxConns is the maximum allowed concurrent SMTP connections.
MaxConns int `json:"max_conns"`
// MaxMessageRetries is the number of times a message should be retried
// if sending fails. Default is 2. Min is 1.
MaxMessageRetries int `json:"max_msg_retries"`
// IdleTimeout is the maximum time to wait for new activity on a connection
// before closing it and removing it from the pool.
IdleTimeout time.Duration `json:"idle_timeout"`
// PoolWaitTimeout is the maximum time to wait to obtain a connection from
// a pool before timing out. This may happen when all open connections are
// busy sending e-mails and they're not returning to the pool fast enough.
// This is also the timeout used when creating new SMTP connections.
PoolWaitTimeout time.Duration `json:"wait_timeout"`
// Given TLSConfig:
// SSL = true; uses an SSL (TLS) connection without the STARTTLS extension.
// SSL = false; opens a non-TLS connection and then requests the STARTTLS extension
// from the server for encryption.
SSL bool `json:"ssl"`
// Auth is the smtp.Auth authentication scheme.
Auth smtp.Auth
// TLSConfig is the optional TLS configuration.
TLSConfig *tls.Config
}
// Pool represents an SMTP connection pool.
type Pool struct {
opt Opt
conns chan *conn
createdConns atomic.Int32
// stopBorrow signals all waiting borrowCon() calls on the pool to
// immediately return an ErrPoolClosed.
stopBorrow chan bool
// closed marks the pool as closed.
closed atomic.Bool
}
// conn represents an AMTP client connection in the pool.
type conn struct {
conn *smtp.Client
numErr int
// lastActivity records the time when the last message on this client
// was sent. Used for sweeping and disconnecting idle connections.
lastActivity time.Time
}
// LoginAuth is the SMTP "LOGIN" type implementation for smtp.Auth.
type LoginAuth struct {
Username string
Password string
}
// ErrPoolClosed is thrown when a closed Pool is used.
var ErrPoolClosed = errors.New("pool closed")
// New initializes and returns a new SMTP Pool.
func New(o Opt) (*Pool, error) {
if o.MaxConns < 1 {
return nil, errors.New("MaxConns should be >= 1")
}
if o.MaxMessageRetries == 0 {
o.MaxMessageRetries = 2
}
if o.PoolWaitTimeout.Seconds() < 1 {
o.PoolWaitTimeout = time.Second * 2
}
p := &Pool{
opt: o,
conns: make(chan *conn, o.MaxConns),
stopBorrow: make(chan bool),
}
// Start the idle connection sweeper.
if o.IdleTimeout.Seconds() >= 1 && o.MaxConns > 1 {
go p.sweepConns(time.Second * 2)
}
return p, nil
}
// Send sends an e-mail using an available connection in the pool.
// On error, the message is retried on a new connection.
func (p *Pool) Send(e Email) error {
// Get a connection from the pool.
var lastErr error
for i := 0; i < p.opt.MaxMessageRetries; i++ {
c, err := p.borrowConn()
if err != nil {
return err
}
// Send the message.
canRetry, err := c.send(e)
if err == nil {
_ = p.returnConn(c, nil)
return nil
}
lastErr = err
// Not a retriable error.
_ = p.returnConn(c, err)
if !canRetry {
return err
}
}
return lastErr
}
// Close closes the pool.
func (p *Pool) Close() {
p.closed.Store(true)
close(p.stopBorrow)
// If the sweeper isn't already running, run it.
if p.opt.IdleTimeout.Seconds() <= 1 {
p.sweepConns(time.Second * 1)
}
}
// newConn creates a new SMTP client connection that can be added to the pool.
func (p *Pool) newConn() (cn *conn, err error) {
var (
netCon net.Conn
addr = fmt.Sprintf("%s:%d", p.opt.Host, p.opt.Port)
)
if p.opt.SSL {
// TLS connection.
c, err := tls.DialWithDialer(&net.Dialer{Timeout: p.opt.PoolWaitTimeout}, "tcp", addr, p.opt.TLSConfig)
if err != nil {
return nil, err
}
netCon = c
} else {
// Non-TLS connection that may be upgraded later using STARTTLS.
c, err := net.DialTimeout("tcp", addr, p.opt.PoolWaitTimeout)
if err != nil {
return nil, err
}
netCon = c
}
// Connect to the SMTP server
sm, err := smtp.NewClient(netCon, p.opt.Host)
if err != nil {
return nil, err
}
// The return values are named so that the errors from multiple points
// here on are captured and the connection closed.
defer func() {
if err != nil {
sm.Close()
}
}()
// Is there a custom hostname for doing a HELLO with the SMTP server?
if p.opt.HelloHostname != "" {
sm.Hello(p.opt.HelloHostname)
}
// STARTTLS.
if !p.opt.SSL {
if ok, _ := sm.Extension("STARTTLS"); !ok {
return nil, errors.New("SMTP STARTTLS extension not found")
}
if err := sm.StartTLS(p.opt.TLSConfig); err != nil {
return nil, err
}
}
// Optional auth.
if p.opt.Auth != nil {
if ok, _ := sm.Extension("AUTH"); !ok {
return nil, errors.New("SMTP AUTH extension not found")
}
if err := sm.Auth(p.opt.Auth); err != nil {
return nil, err
}
}
return &conn{
conn: sm,
}, nil
}
// borrowConn borrows a connection from the pool.
func (p *Pool) borrowConn() (*conn, error) {
// Check pool status and connection counts first.
switch {
case p.closed.Load():
// If the pool is closed, return an error immediately.
return nil, ErrPoolClosed
case int(p.createdConns.Load()) < p.opt.MaxConns && len(p.conns) == 0:
// If there are no connections in the pool and if there is room for new
// connections, create a new connection. Locks are used ad-hoc to avoid
// locking when IO bound newConn() is happening.
p.createdConns.Add(1)
cn, err := p.newConn()
if err != nil {
// Decrement counter on failed connection creation.
p.createdConns.Add(-1)
return nil, err
}
return cn, nil
}
// Try to get a connection from the pool or handle timeouts and pool closure.
select {
case c := <-p.conns:
// Return the connection if one is available.
return c, nil
case <-p.stopBorrow:
// Return error if the pool is closing down.
return nil, ErrPoolClosed
case <-time.After(p.opt.PoolWaitTimeout):
// Return timeout error if no connection becomes available.
return nil, errors.New("timed out waiting for free conn in pool")
}
}
// returnConn returns connection to the pool based on the error from the last
// transaction on it.
func (p *Pool) returnConn(c *conn, lastErr error) (err error) {
// If the function returns an error, that it means it's a bad connection
// and should be closed and not added back to the pool.
defer func() {
if err != nil {
p.createdConns.Add(-1)
c.conn.Close()
}
}()
if lastErr != nil {
// Any error, except for textproto.Error (according to jordan-wright/email),
// is a bad connection that should be killed.
if _, ok := lastErr.(*textproto.Error); !ok {
return lastErr
}
}
// Always RSET (SMTP) the connection bfeore reusing it as some servers
// throw "sender already specified", or "commands out of sequence" errors.
if err := c.conn.Reset(); err != nil {
return err
}
select {
case p.conns <- c:
return nil
case <-time.After(p.opt.PoolWaitTimeout):
return errors.New("timed out returning connection to pool")
case <-p.stopBorrow:
return ErrPoolClosed
}
}
// sweepConns periodically sweeps through connections and closes that have not
// any activity in Opt.IdleTimeout time. This is a blocking function and should
// be run as a goroutine.
func (p *Pool) sweepConns(interval time.Duration) {
activeConns := make([]*conn, cap(p.conns))
for {
<-time.After(interval)
activeConns = activeConns[:0]
// The number of conns in the channel are the ones that are potentially
// idling. Iterate through them and examine their activity timestamp.
var (
num = len(p.conns)
createdConns = p.createdConns.Load()
closed = p.closed.Load()
)
if closed && createdConns == 0 {
// If the pool is closed and there are no more connections, exit
// the sweeper.
return
}
for i := 0; i < num; i++ {
var c *conn
// Pick a connection to check from the pool.
select {
case c = <-p.conns:
default:
continue
}
if closed || time.Since(c.lastActivity) > p.opt.IdleTimeout {
// If the pool is closed or the the connection is idling,
// close the conn.
p.createdConns.Add(-1)
// Unlock mutex before blockong on IO.
if closed {
_ = c.conn.Quit()
} else {
_ = c.conn.Close()
}
continue
}
activeConns = append(activeConns, c)
}
// Put the active conns back in the pool.
for _, c := range activeConns {
select {
case p.conns <- c:
default:
_ = c.conn.Close()
p.createdConns.Add(-1)
}
}
}
}
// send sends a message using the connection. The bool in the return indicates
// if the message can be retried in case of an SMTP related error.
func (c *conn) send(e Email) (bool, error) {
c.lastActivity = time.Now()
// Combine e-mail addresses from multiple lists.
emails, err := combineEmails(e.To, e.Cc, e.Bcc)
if err != nil {
return true, err
}
// Extract SMTP envelope sender from the email struct.
from, err := e.parseSender()
if err != nil {
return true, err
}
// Send the Mail command.
if err = c.conn.Mail(from); err != nil {
return false, err
}
// Send RCPT for all receipients.
for _, recip := range emails {
if err = c.conn.Rcpt(recip); err != nil {
return false, err
}
}
// Write the message.
w, err := c.conn.Data()
if err != nil {
return false, err
}
isClosed := false
defer func() {
if !isClosed {
w.Close()
}
}()
// Get raw message payload.
msg, err := e.Bytes()
if err != nil {
return false, err
}
if _, err = w.Write(msg); err != nil {
return false, err
}
if err := w.Close(); err != nil {
return false, err
}
isClosed = true
return false, nil
}
// Start starts the SMTP LOGIN auth type.
// https://gist.github.com/andelf/5118732
func (a *LoginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte{}, nil
}
// Next passes the credentials for SMTP LOGIN auth type.
func (a *LoginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if !more {
return nil, nil
}
switch string(fromServer) {
case "Username:":
return []byte(a.Username), nil
case "Password:":
return []byte(a.Password), nil
default:
return nil, errors.New("unkown SMTP fromServer")
}
}
// combineEmails takes multiple lists of e-mails, parses them, and combines
// them into a single list.
func combineEmails(lists ...[]string) ([]string, error) {
ln := 0
for _, l := range lists {
ln += len(l)
}
out := make([]string, 0, ln)
for _, l := range lists {
for _, email := range l {
// Parse the e-mail out of the address string.
// Eg: a@a.com out of John Doe <a@a.com>.
addr, err := mail.ParseAddress(email)
if err != nil {
return nil, err
}
out = append(out, addr.Address)
}
}
return out, nil
}