-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.go
More file actions
180 lines (153 loc) · 3.42 KB
/
pool.go
File metadata and controls
180 lines (153 loc) · 3.42 KB
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
package gomail
import (
"context"
"errors"
"sync"
"time"
)
var (
ErrPoolClosed = errors.New("connection pool is closed")
ErrPoolEmpty = errors.New("no connections available")
)
// Pool manages a pool of persistent SMTP connections
type Pool struct {
gomail *GoMail
connections chan *SendCloser
mu sync.Mutex
closed bool
maxSize int
currentSize int
idleTimeout time.Duration
maxLifetime time.Duration
}
// PoolConfig configures the connection pool
type PoolConfig struct {
MaxSize int // Maximum number of connections in the pool
IdleTimeout time.Duration // How long a connection can be idle before being closed
MaxLifetime time.Duration // Maximum lifetime of a connection
}
// NewPool creates a new connection pool
func (g *GoMail) NewPool(config PoolConfig) *Pool {
if config.MaxSize <= 0 {
config.MaxSize = 10
}
if config.IdleTimeout <= 0 {
config.IdleTimeout = 5 * time.Minute
}
if config.MaxLifetime <= 0 {
config.MaxLifetime = 30 * time.Minute
}
return &Pool{
gomail: g,
connections: make(chan *SendCloser, config.MaxSize),
maxSize: config.MaxSize,
idleTimeout: config.IdleTimeout,
maxLifetime: config.MaxLifetime,
}
}
// Get retrieves a connection from the pool or creates a new one
func (p *Pool) Get(ctx context.Context) (*SendCloser, error) {
p.mu.Lock()
if p.closed {
p.mu.Unlock()
return nil, ErrPoolClosed
}
p.mu.Unlock()
select {
case conn := <-p.connections:
// Got a connection from the pool
return conn, nil
default:
// No available connections, try to create a new one
p.mu.Lock()
if p.currentSize < p.maxSize {
p.currentSize++
p.mu.Unlock()
return p.gomail.Dial()
}
p.mu.Unlock()
// Wait for a connection to become available
select {
case conn := <-p.connections:
return conn, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
// Put returns a connection to the pool
func (p *Pool) Put(conn *SendCloser) error {
p.mu.Lock()
defer p.mu.Unlock()
if p.closed {
conn.Close()
return ErrPoolClosed
}
select {
case p.connections <- conn:
return nil
default:
// Pool is full, close the connection
conn.Close()
p.currentSize--
return nil
}
}
// Send sends an email using a connection from the pool
func (p *Pool) Send(ctx context.Context, mail *Mail) error {
conn, err := p.Get(ctx)
if err != nil {
return err
}
sendErr := conn.Send(ctx, mail)
// Return connection to pool regardless of send result
putErr := p.Put(conn)
if sendErr != nil {
return sendErr
}
return putErr
}
// SendBulk sends multiple emails using a connection from the pool
func (p *Pool) SendBulk(ctx context.Context, mails []*Mail) error {
if len(mails) == 0 {
return nil
}
conn, err := p.Get(ctx)
if err != nil {
return err
}
var sendErr error
for _, mail := range mails {
if err := conn.Send(ctx, mail); err != nil {
sendErr = err
break
}
}
// Return connection to pool
putErr := p.Put(conn)
if sendErr != nil {
return sendErr
}
return putErr
}
// Close closes all connections in the pool
func (p *Pool) Close() error {
p.mu.Lock()
defer p.mu.Unlock()
if p.closed {
return nil
}
p.closed = true
close(p.connections)
// Close all connections in the pool
for conn := range p.connections {
conn.Close()
}
return nil
}
// Size returns the current number of connections in the pool
func (p *Pool) Size() int {
p.mu.Lock()
defer p.mu.Unlock()
return p.currentSize
}