-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnpool.go
457 lines (381 loc) · 12.4 KB
/
connpool.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 main
// Pools connections to upstream servers, does high level lifecycle management and
// prioritizes which upstreams get connections and which don't
import (
"fmt"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"sort"
"sync"
"time"
)
type DialFunc func(address string) (*dns.Conn, error)
type ConnEntry interface {
// Add an exchange with a duration to this conn entry
AddExchange(rtt time.Duration)
// get the address that this connentry's connection is pointing to
GetAddress() string
// gets the weight of this conn entry, indicating how efficient it is
GetWeight() (weight UpstreamWeight)
// retrieves the underlying DNS connection for this entry
GetConn() *dns.Conn
// retrives the underlying upstream for this entry
GetUpstream() (upstream *Upstream)
// Closes the connection stored by this connentry
Close()
// Tells the conn entry to track a new error
AddError()
// Determines if the conn entry has seen any errors
Error() bool
}
type ConnPool interface {
// Retrieves a new connection from the pool
// returns an upstream and a nil connentry if a new
// connection must be made
Get() (ce ConnEntry, upstream Upstream)
// Adds a new connection to the pool
Add(ce ConnEntry) (err error)
// Add a new upstream to the pool
AddUpstream(r *Upstream)
// Close a given connection
CloseConnection(ce ConnEntry)
// Adds a new connection to the pool targetting a given upstream and using a given dial function to make the connection.
// The abstraction of dialFunc is for dependency injection
NewConnection(upstream Upstream, dialFunc DialFunc) (ce ConnEntry, err error)
// Returns the number of open connections in the pool
Size() int
}
type connPool struct {
// List of actual upstream structs
upstreams []*Upstream
// List of upstream names. This is kept separate so that callers
// can iterate through the upstreams list by name without having
// to lock the actual upstreams array.
upstreamNames []UpstreamName
cache map[string][]ConnEntry
lock Lock
}
type CachedConn interface {
Close() error
}
type connEntry struct {
// The actual connection
Conn CachedConn
// The upstream that this connection is associated with
upstream Upstream
// The total RTT for this connection
totalRTT time.Duration
// The total exchanges for this connection
exchanges int
// whether this connection hit an error
error bool
}
type Lock struct {
sync.RWMutex
locklevel int
}
// flag this connentry as having errors
func (c *connEntry) AddError() {
c.error = true
}
// retrieves error status
func (c connEntry) Error() bool {
return c.error
}
// Increment the internal counters tracking successful exchanges and durations
func (c *connEntry) AddExchange(rtt time.Duration) {
// TODO evaluate having multiple timeouts for dialing vs rtt'ing
RttTimeout := GetConfiguration().Timeout
if RttTimeout == 0 {
RttTimeout = 500
}
// check to see if this exchange took too long
if rtt > time.Duration(RttTimeout)*time.Millisecond {
// next time around, treat this as a bogus connection
c.AddError()
}
c.totalRTT += rtt
c.exchanges += 1
}
func (c connEntry) GetAddress() string {
return c.upstream.GetAddress()
}
func (c connEntry) Close() {
c.Conn.Close()
}
func (c connEntry) GetConn() (conn *dns.Conn) {
return c.Conn.(*dns.Conn)
}
func (c connEntry) GetUpstream() (upstream *Upstream) {
return &c.upstream
}
func (ce connEntry) GetWeight() (weight UpstreamWeight) {
if ce.totalRTT == 0 || ce.exchanges == 0 {
// this connection hasn't seen any actual connection time, no weight
weight = 0
} else {
weight = UpstreamWeight(ce.totalRTT / time.Millisecond) / UpstreamWeight(ce.exchanges)
}
Logger.Log(NewLogMessage(
DEBUG,
LogContext{
"what": "setting weight on connection",
"connection_address": ce.GetAddress(),
"currentRTT": fmt.Sprintf("%s", ce.totalRTT),
"exchanges": fmt.Sprintf("%d", ce.exchanges),
"new_weight": fmt.Sprintf("%f", weight),
},
func() string { return fmt.Sprintf("upstream [%v] connection [%v]", ce.upstream, ce) },
))
return
}
func NewConnPool() *connPool {
return &connPool{
cache: make(map[string][]ConnEntry),
}
}
func (c *connPool) Lock() {
c.lock.Lock()
}
func (c *connPool) Unlock() {
c.lock.Unlock()
}
// Retrieves a given upstream based on its address. This is used to avoid excessive
// locking while passing around upstreams
func (c *connPool) getUpstreamByAddress(address string) (upstream *Upstream, err error) {
// technically not the fastest way to do this, but the list of upstreams shouldn't be big enough for it to matter
for _, upstream := range c.upstreams {
if upstream.GetAddress() == address {
return upstream, nil
}
}
return &Upstream{}, fmt.Errorf("could not find upstream with address [%s]", address)
}
// WARNING: this function is not reentrant, it is meant to be called internally
// when the connection pool is already locked and needs to update its upstream weights
func (c *connPool) weightUpstream(upstream *Upstream, ce ConnEntry) {
// the upstream weight will be the result of the most recent connection:
// we want to prefer the upstream with the fastest connections and
// ditch them when they start to slow down
upstream.SetWeight(ce.GetWeight())
}
// Will select the lowest weighted cached connection
// falling back to the lowest weighted upstream if none exist
// upstreams that are cooling will be ignored
func (c *connPool) getBestUpstream() (upstream Upstream) {
// get the first upstream with connections, default to the 0th connection on the list
// iterate through in order; this list is sorted based on weight
var bestCandidate Upstream
// goal: find the highest lowest weighted upstream with connections
for _, each := range c.upstreams {
if conns, ok := c.cache[each.GetAddress()]; ok {
// should this upstream be taking connections?
if !each.IsCooling() {
// it should. does it HAVE connections?
if len(conns) > 0 {
return *each
}
// it's open, but it doesn't have existing connections, let's
// use it iff there's no existing connections
if (bestCandidate == Upstream{}) {
bestCandidate = *each
}
}
}
}
// if we got here, there are no cached connections, did we find a candidate?
// the candidate will be the lowest weighted connection that wasn't cooling
if (bestCandidate != Upstream{}) {
return bestCandidate
}
// no cached connections, everything is cooling, let's abuse the lowest
// weighted upstream
return *c.upstreams[0]
}
// arranges the upstreams based on weight
func (c *connPool) sortUpstreams() {
sort.Slice(c.upstreams, func(i, j int) bool {
return c.upstreams[i].GetWeight() < c.upstreams[j].GetWeight()
})
}
// updates a upstream's weight based on a conn entry being added or closed
func (c *connPool) updateUpstream(ce ConnEntry) (err error) {
address := ce.GetAddress()
// get the actual pointer for this ce's upstream
upstream, err := c.getUpstreamByAddress(address)
if err != nil {
return fmt.Errorf("could not update upstream with address [%s]: %s", address, err)
}
// we may need to update after a fresh connection errored out
// so ignore weightless connections
if ce.GetWeight() != 0 {
c.weightUpstream(upstream, ce)
}
errorString := "no"
if upstream.IsCooling() {
errorString = "cooling"
}
if ce.Error() {
c.coolAndPurgeUpstream(upstream)
errorString += ", connection"
}
c.sortUpstreams()
coolingString := "0"
if upstream.IsCooling() {
// Wake Up Time: wut :)
wut := upstream.WakeupTime()
coolingString = fmt.Sprintf(
"%d-%d-%d %d:%d:%d:%s",
wut.Year(),
wut.Month(),
wut.Day(),
wut.Hour(),
wut.Minute(),
wut.Second(),
time.Now().Sub(wut),
)
}
UpstreamWeightGauge.WithLabelValues(upstream.GetAddress(), errorString, coolingString).Set(float64(upstream.GetWeight()))
return nil
}
// adds a connection to the cache
func (c *connPool) Add(ce ConnEntry) (err error) {
c.Lock()
defer c.Unlock()
address := ce.GetAddress()
if err = c.updateUpstream(ce); err != nil {
err = fmt.Errorf("couldn't update upstream weight on connection to [%s]: %s", address, err.Error())
}
Logger.Log(NewLogMessage(
INFO,
LogContext{
"what": "adding connection back to conn pool",
"address": address,
},
func() string { return fmt.Sprintf("connection entry [%v]", ce) },
))
if _, ok := c.cache[address]; ok {
c.cache[address] = append(c.cache[address], ce)
} else {
// the max is greater than zero and there's nothing here, so we can just insert
c.cache[address] = []ConnEntry{ce}
}
ConnPoolSizeGauge.WithLabelValues(address).Set(float64(len(c.cache[address])))
return
}
// Makes a new connection to a given upstream, wraps the whole thing in conn entry
func (c *connPool) NewConnection(upstream Upstream, dialFunc DialFunc) (ce ConnEntry, err error) {
address := upstream.GetAddress()
Logger.Log(NewLogMessage(
INFO,
LogContext{
"what": "making new connection",
"weight": fmt.Sprintf("%f", upstream.GetWeight()),
"address": address,
"next": "dialing",
},
func() string { return fmt.Sprintf("upstream [%v] connpool [%v]", upstream, c) },
))
tlsTimer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
TLSTimer.WithLabelValues(address).Observe(v)
}),
)
NewConnectionAttemptsCounter.WithLabelValues(address).Inc()
conn, err := dialFunc(address)
dialDuration := tlsTimer.ObserveDuration()
if err != nil {
// errors! better cool this upstream
c.Lock()
defer c.Unlock()
upstream, upstreamErr := c.getUpstreamByAddress(address)
if upstreamErr != nil {
err = fmt.Errorf("could not retrieve upstream for %s: %s: %s", address, upstreamErr, err)
return &connEntry{}, err
}
c.coolAndPurgeUpstream(upstream)
FailedConnectionsCounter.WithLabelValues(address).Inc()
return &connEntry{}, fmt.Errorf("cooling upstream, could not connect to [%s]: %s", address, err)
}
Logger.Log(NewLogMessage(
INFO,
LogContext{
"what": "connection successful",
"address": address,
},
nil,
))
ce = &connEntry{Conn: conn, upstream: upstream}
ce.AddExchange(dialDuration)
return ce, nil
}
// attempts to retrieve a connection from the most attractive upstream
// if it doesn't have one, returns an upstream for the caller to connect to
func (c *connPool) Get() (ce ConnEntry, upstream Upstream) {
c.Lock()
defer c.Unlock()
upstream = c.getBestUpstream()
// now use the address for whichever one came out, the default one with no connections
// or the best weighted upstream with cached connections
address := upstream.GetAddress()
// Check for an existing connection
if conns, ok := c.cache[address]; ok && len(conns) > 0 {
for i := 0; i < len(conns); i++ {
j := i + 1
// pop off a connection and return it
ce, c.cache[address] = conns[i], conns[j:]
ConnPoolSizeGauge.WithLabelValues(address).Set(float64(len(c.cache[address])))
return ce, Upstream{}
}
}
// we couldn't find a single connection, tell the caller to make a new one to the best weighted upstream
return &connEntry{}, upstream
}
// since this reads all the maps, it needs to make sure there are no concurrent writes
// caveat emptor
func (c *connPool) Size() int {
c.Lock()
defer c.Unlock()
size := 0
for _, v := range c.cache {
size = size + len(v)
}
return size
}
// maintains the internal list of upstreams that this connection pool
// will attempt to connect to
func (c *connPool) AddUpstream(r *Upstream) {
c.upstreams = append(c.upstreams, r)
}
func (c *connPool) CloseConnection(ce ConnEntry) {
c.Lock()
defer c.Unlock()
c.updateUpstream(ce)
ce.Close()
}
// take an upstream pointer (so that we can update the actual record)
// and tell it to cool down, sever all connections
// non re-entrant, needs outside locking
func (c *connPool) coolAndPurgeUpstream(upstream *Upstream) {
config := GetConfiguration()
cooldownPeriod := time.Duration(500) * time.Millisecond
if config.CooldownPeriod != 0 {
cooldownPeriod = config.CooldownPeriod * time.Millisecond
}
upstream.Cooldown(cooldownPeriod)
c.purgeUpstream(*upstream)
}
// closes all connections that belong to a given upstream
// non re-entrant
func (c *connPool) purgeUpstream(upstream Upstream) {
addr := upstream.GetAddress()
if _, ok := c.cache[addr]; ok {
for _, conn := range c.cache[addr] {
// run async so as not to block queries that might be calling
go func(conn ConnEntry) {
c.CloseConnection(conn)
}(conn)
}
c.cache[addr] = []ConnEntry{}
}
ConnPoolSizeGauge.WithLabelValues(addr).Set(float64(len(c.cache[addr])))
}