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
|
/*
* This file is part of Go Responsiveness.
*
* Go Responsiveness is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software Foundation,
* either version 2 of the License, or (at your option) any later version.
* Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>.
*/
package probe
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptrace"
"os"
"time"
"github.com/network-quality/goresponsiveness/constants"
"github.com/network-quality/goresponsiveness/debug"
"github.com/network-quality/goresponsiveness/extendedstats"
"github.com/network-quality/goresponsiveness/utilities"
)
type ProbeType int64
type ProbeConfiguration struct {
ConnectToAddr string
URL string
Host string
CongestionControl *string
InsecureSkipVerify bool
}
type ProbeDataPoint struct {
Time time.Time `Description:"Time of the generation of the data point." Formatter:"Format" FormatterArgument:"01-02-2006-15-04-05.000"`
RoundTripCount uint64 `Description:"The number of round trips measured by this data point."`
Duration time.Duration `Description:"The duration for this measurement." Formatter:"Seconds"`
TCPRtt time.Duration `Description:"The underlying connection's RTT at probe time." Formatter:"Seconds"`
TCPCwnd uint32 `Description:"The underlying connection's congestion window at probe time."`
Type ProbeType `Description:"The type of the probe." Formatter:"Value"`
CongestionControl string `Description:"The congestion control algorithm used."`
}
const (
SelfUp ProbeType = iota
SelfDown
Foreign
)
type ProbeRoundTripCountType uint16
const (
DefaultDownRoundTripCount ProbeRoundTripCountType = 1
SelfUpRoundTripCount ProbeRoundTripCountType = 1
SelfDownRoundTripCount ProbeRoundTripCountType = 1
ForeignRoundTripCount ProbeRoundTripCountType = 3
)
func (pt ProbeType) Value() string {
if pt == SelfUp {
return "SelfUp"
} else if pt == SelfDown {
return "SelfDown"
}
return "Foreign"
}
func (pt ProbeType) IsSelf() bool {
return pt == SelfUp || pt == SelfDown
}
func Probe(
managingCtx context.Context,
client *http.Client,
probeUrl string,
probeHost string, // optional: for use with a test_endpoint
probeType ProbeType,
probeId uint,
congestionControl *string,
captureExtendedStats bool,
debugging *debug.DebugWithPrefix,
) (*ProbeDataPoint, error) {
if client == nil {
return nil, fmt.Errorf("cannot start a probe with a nil client")
}
probeTracer := NewProbeTracer(client, probeType, probeId, congestionControl, debugging)
time_before_probe := time.Now()
probe_req, err := http.NewRequestWithContext(
httptrace.WithClientTrace(managingCtx, probeTracer.trace),
"GET",
probeUrl,
nil,
)
if err != nil {
return nil, err
}
// Used to disable compression
probe_req.Header.Set("Accept-Encoding", "identity")
probe_req.Header.Set("User-Agent", utilities.UserAgent())
probe_resp, err := client.Do(probe_req)
if err != nil {
if debug.IsDebug(debugging.Level) {
fmt.Printf(
"(%s) (%s Probe %v) An error occurred during http.Do: %v\n",
debugging.Prefix,
probeType.Value(),
probeId,
err,
)
}
return nil, err
}
// Header.Get returns "" when not set
if probe_resp.Header.Get("Content-Encoding") != "" {
return nil, fmt.Errorf("Content-Encoding header was set (compression not allowed)")
}
// TODO: Make this interruptable somehow by using _ctx_.
_, err = io.ReadAll(probe_resp.Body)
if err != nil {
if debug.IsDebug(debugging.Level) {
fmt.Printf(
"(%s) (%s Probe %v) An error occurred during io.ReadAll: %v\n",
debugging.Prefix,
probeType.Value(),
probeId,
err,
)
}
return nil, err
}
time_after_probe := time.Now()
// Depending on whether we think that Close() requires another RTT (via TCP), we
// may need to move this before/after capturing the after time.
probe_resp.Body.Close()
sanity := time_after_probe.Sub(time_before_probe)
// When the probe is run on a load-generating connection (a self probe) there should
// only be a single round trip that is measured. We will take the accumulation of all these
// values just to be sure, though. Because of how this traced connection was launched, most
// of the values will be 0 (or very small where the time that go takes for delivering callbacks
// and doing context switches pokes through). When it is !isSelfProbe then the values will
// be significant and we want to add them regardless!
totalDelay := probeTracer.GetTLSAndHttpHeaderDelta() + probeTracer.GetHttpDownloadDelta(
time_after_probe,
) + probeTracer.GetTCPDelta()
// We must have reused the connection if we are a self probe!
if probeType.IsSelf() && !probeTracer.stats.ConnectionReused {
fmt.Fprintf(os.Stderr,
"(%s) (%s Probe %v) Probe should have reused a connection, but it didn't!\n",
debugging.Prefix,
probeType.Value(),
probeId,
)
panic(!probeTracer.stats.ConnectionReused)
}
if debug.IsDebug(debugging.Level) {
fmt.Printf(
"(%s) (%s Probe %v) sanity vs total: %v vs %v\n",
debugging.Prefix,
probeType.Value(),
probeId,
sanity,
totalDelay,
)
}
roundTripCount := DefaultDownRoundTripCount
if probeType == Foreign {
roundTripCount = ForeignRoundTripCount
}
// Careful!!! It's possible that this channel has been closed because the Prober that
// started it has been stopped. Writing to a closed channel will cause a panic. It might not
// matter because a panic just stops the go thread containing the paniced code and we are in
// a go thread that executes only this function.
defer func() {
isThreadPanicing := recover()
if isThreadPanicing != nil && debug.IsDebug(debugging.Level) {
fmt.Printf(
"(%s) (%s Probe %v) Probe attempted to write to the result channel after its invoker ended (official reason: %v).\n",
debugging.Prefix,
probeType.Value(),
probeId,
isThreadPanicing,
)
}
}()
tcpRtt := time.Duration(0 * time.Second)
tcpCwnd := uint32(0)
// TODO: Only get the extended stats for a connection if the user has requested them overall.
if captureExtendedStats && extendedstats.ExtendedStatsAvailable() {
tcpInfo, err := extendedstats.GetTCPInfo(probeTracer.stats.ConnInfo.Conn)
if err == nil {
tcpRtt = time.Duration(tcpInfo.Rtt) * time.Microsecond
tcpCwnd = tcpInfo.Snd_cwnd
} else {
fmt.Printf("Warning: Could not fetch the extended stats for a probe: %v\n", err)
}
}
return &ProbeDataPoint{
Time: time_before_probe,
RoundTripCount: uint64(roundTripCount),
Duration: totalDelay,
TCPRtt: tcpRtt,
TCPCwnd: tcpCwnd,
Type: probeType,
CongestionControl: *utilities.Conditional(congestionControl == nil,
&constants.DefaultL4SCongestionControlAlgorithm, congestionControl),
}, nil
}
|