-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_tracker.go
166 lines (127 loc) · 3.08 KB
/
connection_tracker.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
package http2
import (
"net"
"sync"
"time"
"github.com/dropbox/godropbox/errors"
"github.com/dropbox/godropbox/stats"
)
type DialFunc func(network string, add string) (net.Conn, error)
type ConnectionTracker struct {
maxConnections int
connectionAcquireTimeout time.Duration
dial DialFunc
mutex sync.Mutex
next int64 // guarded by mutex
connections map[int64]*trackedConn // guarded by mutex
disallowNewConn bool // guarded by mutex
// stats
dialMsSummary stats.SummaryStat
}
func NewConnectionTracker(
maxConnections int,
dial DialFunc,
statsFactory stats.StatsFactory) *ConnectionTracker {
tags := map[string]string{}
ct := &ConnectionTracker{
maxConnections: maxConnections,
dial: dial,
next: 0,
connections: make(map[int64]*trackedConn),
disallowNewConn: false,
dialMsSummary: statsFactory.NewSummary("pool_dial_ms", tags),
}
return ct
}
func (c *ConnectionTracker) dialMarker(
network string,
addr string) (
*trackedConn,
error) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.disallowNewConn {
return nil, DialError{errors.New("Dial Error: Pool closed")}
}
if c.maxConnections > 0 && len(c.connections) >= c.maxConnections {
return nil, DialError{
errors.New("Dial Error: Reached pool max connection limit"),
}
}
id := c.next
// Dial is perform outside.
marker := &trackedConn{
id: id,
tracker: c,
}
c.connections[id] = marker
c.next++
return marker, nil
}
func (c *ConnectionTracker) Dial(
network string,
addr string) (
net.Conn,
error) {
marker, err := c.dialMarker(network, addr)
if err != nil {
return nil, err
}
now := time.Now()
conn, err := c.dial(network, addr)
dialMs := time.Now().Sub(now).Seconds() * 1000
c.dialMsSummary.Observe(dialMs)
if err != nil {
c.remove(marker.id)
return nil, DialError{errors.Wrap(err, "Dial Error:\n")}
}
c.mutex.Lock()
defer c.mutex.Unlock()
// we have to perform a second check, or else we could leak connections.
if c.disallowNewConn {
_ = conn.Close()
delete(c.connections, marker.id)
return nil, DialError{errors.New("Dial Error: Pool closed")}
}
marker.Conn = conn
return marker, nil
}
func (c *ConnectionTracker) NumAlive() int {
c.mutex.Lock()
defer c.mutex.Unlock()
return len(c.connections)
}
func (c *ConnectionTracker) ForceCloseAll() {
c.mutex.Lock()
c.disallowNewConn = true
oldConns := c.connections
c.connections = make(map[int64]*trackedConn)
c.mutex.Unlock()
for _, marker := range oldConns {
// We can ignore marker.Conn == nil since the connection will be
// closed by Dial isntead.
if marker.Conn != nil {
// Ignore error on close
_ = marker.Conn.Close()
}
}
}
func (c *ConnectionTracker) DisallowNewConn() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.disallowNewConn = true
}
func (c *ConnectionTracker) remove(id int64) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.connections, id)
}
type trackedConn struct {
net.Conn
id int64
tracker *ConnectionTracker
}
func (t *trackedConn) Close() error {
t.tracker.remove(t.id)
return t.Conn.Close()
}