-
Notifications
You must be signed in to change notification settings - Fork 0
/
preconnecting_dialer.go
163 lines (143 loc) · 3.73 KB
/
preconnecting_dialer.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
package chained
import (
"context"
"fmt"
"net"
"sync"
"time"
"github.com/getlantern/flashlight/v7/ops"
"github.com/getlantern/golog"
)
type preconnectedConn struct {
net.Conn
expiresAt time.Time
}
func (pc *preconnectedConn) expired() bool {
return pc.expiresAt.Before(time.Now())
}
type preconnectingDialer struct {
wrapped proxyImpl
log golog.Logger
maxPreconnect int
expiration time.Duration
pool chan *preconnectedConn
preconnected int
preconnecting int
statsMutex sync.RWMutex
closeCh chan bool
}
func newPreconnectingDialer(name string, maxPreconnect int, expiration time.Duration, wrapped proxyImpl) *preconnectingDialer {
pd := &preconnectingDialer{
wrapped: wrapped,
log: golog.LoggerFor(fmt.Sprintf("chained.preconnect.%v", name)),
maxPreconnect: maxPreconnect,
expiration: expiration,
pool: make(chan *preconnectedConn, maxPreconnect),
closeCh: make(chan bool),
}
pd.log.Debugf("will preconnect up to %d times", maxPreconnect)
go pd.closeWhenNecessary()
return pd
}
func (pd *preconnectingDialer) dialServer(op *ops.Op, ctx context.Context) (conn net.Conn, err error) {
// Whenever we dial successfully, warm up the pool by preconnecting
defer func() {
if err == nil {
pd.preconnectIfNecessary(op)
}
}()
// Try to get an unexpired preconnected connection when possible
for {
select {
case pconn := <-pd.pool:
pd.decrementPreconnected()
if !pconn.expired() {
pd.log.Tracef("using preconnection")
conn = pconn.Conn
return
}
pd.log.Tracef("preconnection expired before use")
default:
pd.log.Tracef("dialing on demand")
conn, err = pd.wrapped.dialServer(op, ctx)
return
}
}
}
func (pd *preconnectingDialer) preconnectIfNecessary(op *ops.Op) {
pd.statsMutex.Lock()
defer pd.statsMutex.Unlock()
if pd.preconnected+pd.preconnecting >= pd.maxPreconnect {
// pool potentially full once in-flight preconnectings succeed, don't bother preconnecting
return
}
pd.preconnecting++
go func() {
select {
case <-pd.closeCh:
pd.log.Trace("already closed, refusing to preconnect")
pd.decrementPreconnecting()
return
default:
pd.preconnect(op)
}
}()
}
func (pd *preconnectingDialer) preconnect(op *ops.Op) {
ctx, cancel := context.WithTimeout(context.Background(), chainedDialTimeout)
defer cancel()
expiration := time.Now().Add(pd.expiration)
conn, err := pd.wrapped.dialServer(op, ctx)
if err != nil {
pd.log.Errorf("error preconnecting: %v", err)
pd.decrementPreconnecting()
return
}
pd.preconnectingSucceeded()
pd.pool <- &preconnectedConn{conn, expiration}
pd.log.Trace("preconnected")
}
func (pd *preconnectingDialer) numPreconnecting() int {
pd.statsMutex.RLock()
defer pd.statsMutex.RUnlock()
return pd.preconnecting
}
func (pd *preconnectingDialer) numPreconnected() int {
pd.statsMutex.RLock()
defer pd.statsMutex.RUnlock()
return pd.preconnected
}
func (pd *preconnectingDialer) decrementPreconnecting() {
pd.statsMutex.Lock()
pd.preconnecting--
pd.statsMutex.Unlock()
}
func (pd *preconnectingDialer) preconnectingSucceeded() {
pd.statsMutex.Lock()
pd.preconnecting--
pd.preconnected++
pd.statsMutex.Unlock()
}
func (pd *preconnectingDialer) decrementPreconnected() {
pd.statsMutex.Lock()
pd.preconnected--
pd.statsMutex.Unlock()
}
func (pd *preconnectingDialer) closeWhenNecessary() {
pd.log.Trace("waiting for close")
// wait for close
<-pd.closeCh
for {
select {
case pconn := <-pd.pool:
pd.log.Trace("closing preconnection")
pconn.Conn.Close()
case <-time.After(chainedDialTimeout * 2):
pd.log.Trace("waited twice the chained dial timeout, no more preconnections to close")
return
}
}
}
func (pd *preconnectingDialer) close() {
close(pd.closeCh)
}