-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dialer.go
189 lines (155 loc) · 3.61 KB
/
test_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
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
package balancer
import (
"bytes"
"context"
"fmt"
"io"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/getlantern/mockconn"
)
var (
rttMultiplier = time.Duration(1)
)
type testDialer struct {
name string
baseRTT time.Duration
rtt time.Duration
bandwidth float64
untrusted int32
failingUpstream bool
muSuccessRate sync.RWMutex
successRate float64
attempts int64
successes int64
failures int64
stopped int32
connectivityChecks int32
remainingFailures int
}
// Name returns the name for this Dialer
func (d *testDialer) Name() string {
return d.name
}
func (d *testDialer) Label() string {
return d.name
}
func (d *testDialer) JustifiedLabel() string {
return d.name
}
func (d *testDialer) Location() (string, string, string) {
return "", "", ""
}
func (d *testDialer) Protocol() string {
return "https"
}
func (d *testDialer) Addr() string {
return ""
}
func (d *testDialer) Trusted() bool {
return atomic.LoadInt32(&d.untrusted) == 0
}
func (d *testDialer) NumPreconnecting() int {
return 0
}
func (d *testDialer) NumPreconnected() int {
return 0
}
func (d *testDialer) SupportsAddr(network, addr string) bool {
return true
}
func (d *testDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, bool, error) {
var conn net.Conn
var err error
if d.remainingFailures > 0 {
err = fmt.Errorf("Failing intentionally")
d.remainingFailures--
} else if !d.Succeeding() {
err = fmt.Errorf("Not succeeding")
} else if d.failingUpstream {
err = fmt.Errorf("Failing upstream")
} else {
time.Sleep(d.rtt)
if network != "" {
var d net.Dialer
conn, err = d.DialContext(ctx, network, addr)
} else {
var buf bytes.Buffer
conn = mockconn.New(&buf, strings.NewReader(""))
}
}
atomic.AddInt64(&d.attempts, 1)
if err == nil {
atomic.AddInt64(&d.successes, 1)
} else if !d.failingUpstream {
atomic.AddInt64(&d.failures, 1)
}
return conn, d.failingUpstream, err
}
func (d *testDialer) MarkFailure() {
atomic.AddInt64(&d.failures, 1)
}
func (d *testDialer) EstRTT() time.Duration {
return d.rtt
}
func (d *testDialer) setSuccessRate(rate float64) {
d.muSuccessRate.Lock()
defer d.muSuccessRate.Unlock()
d.successRate = rate
}
func (d *testDialer) EstSuccessRate() float64 {
d.muSuccessRate.RLock()
defer d.muSuccessRate.RUnlock()
return d.successRate
}
func (d *testDialer) EstBandwidth() float64 {
return d.bandwidth
}
func (d *testDialer) Attempts() int64 {
return atomic.LoadInt64(&d.attempts)
}
func (d *testDialer) Successes() int64 {
return atomic.LoadInt64(&d.successes)
}
func (d *testDialer) ConsecSuccesses() int64 {
return 0
}
func (d *testDialer) Failures() int64 {
return atomic.LoadInt64(&d.failures)
}
func (d *testDialer) ConsecFailures() int64 {
return 0
}
func (d *testDialer) Succeeding() bool {
return d.EstSuccessRate() > 0.9
}
func (d *testDialer) recalcRTT() {
if d.baseRTT != 0 {
d.rtt = d.baseRTT * rttMultiplier
}
}
func (d *testDialer) connectivityChecksSinceLast() int {
return int(atomic.SwapInt32(&d.connectivityChecks, 0))
}
func (d *testDialer) DataSent() uint64 {
return 0
}
func (d *testDialer) DataRecv() uint64 {
return 0
}
func (d *testDialer) Probe(forPerformance bool) bool {
d.recalcRTT()
time.Sleep(d.rtt)
atomic.AddInt32(&d.connectivityChecks, 1)
return true
}
func (d *testDialer) ProbeStats() (successes uint64, successKBs uint64, failures uint64, failedKBs uint64) {
return 0, 0, 0, 0
}
func (d *testDialer) Stop() {
atomic.StoreInt32(&d.stopped, 1)
}
func (d *testDialer) WriteStats(w io.Writer) {}