forked from google/gomemcache
/
pool.go
311 lines (262 loc) · 7.38 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
package memcache
import (
"bufio"
"context"
"errors"
"net"
"sync"
"sync/atomic"
"time"
)
const (
DefaultMaxConns = 10
DefaultIdleLifetime = 5 * time.Minute
DefaultIdleClosePeriod = 1 * time.Minute
)
type PoolConfig struct {
MaxConns int // maximum number of connections to keep open per server (includes active and idle connections)
MaxIdleLifetime time.Duration // maximum amount of time an idle connection is kept open
IdleClosePeriod time.Duration // how often to close (reap) idle connections
OnAcquire func(string, error, time.Duration) // when we acquire a connection slot
OnCreate func(string, error, time.Duration) // when we create a new connection
}
// withDefaults sets default values for any zero fields in the config.
func (c *PoolConfig) withDefaults() {
if c.MaxConns == 0 {
c.MaxConns = DefaultMaxConns
}
if c.MaxIdleLifetime == 0 {
c.MaxIdleLifetime = DefaultIdleLifetime
}
if c.IdleClosePeriod == 0 {
c.IdleClosePeriod = DefaultIdleClosePeriod
}
if c.OnAcquire == nil {
c.OnAcquire = func(string, error, time.Duration) {}
}
if c.OnCreate == nil {
c.OnCreate = func(string, error, time.Duration) {}
}
}
type dialer func(ctx context.Context, network, addr string) (net.Conn, error)
// ClusterPool is a thin wrapper around multiple Pools, one for each server in the cluster.
type ClusterPool struct {
dial dialer
config PoolConfig
mu sync.RWMutex
pools map[string]*Pool
}
func NewClusterPool(dial dialer, config PoolConfig) *ClusterPool {
return &ClusterPool{
dial: dial,
config: config,
pools: make(map[string]*Pool),
}
}
func (cp *ClusterPool) Get(ctx context.Context, addr net.Addr) (*Conn, error) {
pool := cp.getPool(addr)
return pool.Get(ctx)
}
func (cp *ClusterPool) Put(c *Conn) {
pool := cp.getPool(c.addr)
pool.Put(c)
}
func (cp *ClusterPool) Release(addr net.Addr) {
pool := cp.getPool(addr)
pool.Release()
}
func (cp *ClusterPool) Each(fn func(string, *Pool)) {
cp.mu.RLock()
defer cp.mu.RUnlock()
for addr, pool := range cp.pools {
fn(addr, pool)
}
}
// getPool returns the pool for the given address, creating it if necessary.
// This is required because the cluster is dynamic and new servers can be added at any time.
func (cp *ClusterPool) getPool(addr net.Addr) *Pool {
cp.mu.RLock()
pool, ok := cp.pools[addr.String()]
cp.mu.RUnlock()
if ok {
return pool
}
cp.mu.Lock()
defer cp.mu.Unlock()
// double check now that we hold the exclusive lock
pool, ok = cp.pools[addr.String()]
if ok {
return pool
}
pool = NewPool(addr, cp.dial, cp.config)
cp.pools[addr.String()] = pool
return pool
}
// Pool is a connection pool for a single server.
// It manages a pool of idle connections and limits the number of active connections.
// It also periodically closes idle connections to minimize unused resources.
// Note: Any connection obtained from the pool must be returned to the pool when done (either using the Put or Release method).
type Pool struct {
addr net.Addr
dialer func(ctx context.Context, network, addr string) (net.Conn, error)
config PoolConfig
mu sync.Mutex
idle []idleConn
active chan struct{}
hits atomic.Int64
misses atomic.Int64
}
type idleConn struct {
c *Conn
t time.Time // last used time
}
func NewPool(addr net.Addr, dial dialer, config PoolConfig) *Pool {
// ensure config has defaults
config.withDefaults()
p := &Pool{
addr: addr,
dialer: dial,
config: config,
}
// start a goroutine to close idle connections periodically
go func() {
ticker := time.NewTicker(p.config.IdleClosePeriod)
defer ticker.Stop()
for range ticker.C {
p.closeIdle()
}
}()
p.active = make(chan struct{}, p.config.MaxConns)
return p
}
// Get returns a connection from the pool or creates a new one if necessary.
// The connection must be returned to the pool when done.
// If max connections are already in use, Get will block until a connection is available or the context is canceled.
func (p *Pool) Get(ctx context.Context) (*Conn, error) {
// first acquire a slot
acquireStart := time.Now()
select {
case <-ctx.Done():
p.config.OnAcquire(p.addr.String(), ctx.Err(), time.Since(acquireStart))
return nil, ctx.Err()
case p.active <- struct{}{}:
}
p.config.OnAcquire(p.addr.String(), nil, time.Since(acquireStart))
// then try to get an idle connection
if c := p.getIdle(); c != nil {
p.hits.Add(1)
return c, nil
}
p.misses.Add(1)
// if no idle connection, create a new one
createStart := time.Now()
nc, err := p.dialer(ctx, p.addr.Network(), p.addr.String())
if err != nil {
p.config.OnCreate(p.addr.String(), err, time.Since(createStart))
// release the slot
<-p.active
var ne net.Error
if errors.As(err, &ne) && ne.Timeout() {
return nil, &ConnectTimeoutError{p.addr}
}
return nil, err
}
p.config.OnCreate(p.addr.String(), nil, time.Since(createStart))
return &Conn{
nc: nc,
addr: p.addr,
rw: bufio.NewReadWriter(bufio.NewReader(nc), bufio.NewWriter(nc)),
pool: p,
}, nil
}
func (p *Pool) getIdle() *Conn {
p.mu.Lock()
defer p.mu.Unlock()
if len(p.idle) == 0 {
return nil
}
// idle is a stack (LIFO)
ic := p.idle[len(p.idle)-1]
p.idle = p.idle[:len(p.idle)-1]
return ic.c
}
// Put returns a connection to the pool for reuse.
func (p *Pool) Put(c *Conn) {
p.mu.Lock()
defer p.mu.Unlock()
// idle is a stack (LIFO)
p.idle = append(p.idle, idleConn{c: c, t: time.Now()})
<-p.active
}
// Release releases a slot in the pool. This should be called if a connection was closed outside the pool
// (most likely due to an error).
func (p *Pool) Release() {
<-p.active
}
// closeIdle closes idle connections that have been idle for longer than the max idle lifetime.
func (p *Pool) closeIdle() {
p.mu.Lock()
defer p.mu.Unlock()
idle := make([]idleConn, 0, len(p.idle))
for _, ic := range p.idle {
if time.Since(ic.t) > p.config.MaxIdleLifetime {
ic.c.nc.Close()
} else {
idle = append(idle, ic)
}
}
p.idle = idle
}
func (p *Pool) Stats() PoolStats {
p.mu.Lock()
defer p.mu.Unlock()
return PoolStats{
Active: int64(len(p.active)),
Idle: int64(len(p.idle)),
Hit: p.hits.Swap(0),
Miss: p.misses.Swap(0),
}
}
type PoolStats struct {
Active int64 // number of active connections
Idle int64 // number of idle connections
Hit int64 // number of cache hits (since last call to Stats)
Miss int64 // number of cache misses (since last call to Stats)
}
type Conn struct {
pool *Pool
addr net.Addr
nc net.Conn
rw *bufio.ReadWriter
}
func (c *Conn) ExtendDeadline(d time.Duration) error {
return c.nc.SetDeadline(time.Now().Add(d))
}
func (c *Conn) Release(err error) {
if err == nil || resumableError(err) {
c.pool.Put(c)
} else {
c.pool.Release()
c.nc.Close()
}
}
// resumableError returns true if err is only a protocol-level cache error.
// This is used to determine whether or not a server connection should
// be re-used or not. If an error occurs, by default we don't reuse the
// connection, unless it was just a cache error.
func resumableError(err error) bool {
switch err {
case ErrCacheMiss, ErrCASConflict, ErrNotStored, ErrMalformedKey:
return true
}
return false
}
// ConnectTimeoutError is the error type used when it takes
// too long to connect to the desired host. This level of
// detail can generally be ignored.
type ConnectTimeoutError struct {
Addr net.Addr
}
func (cte *ConnectTimeoutError) Error() string {
return "memcache: connect timeout to " + cte.Addr.String()
}