...
1
2
3
4
5
6
7 package quic
8
9 import (
10 "time"
11 )
12
13 type rttState struct {
14 minRTT time.Duration
15 latestRTT time.Duration
16 smoothedRTT time.Duration
17 rttvar time.Duration
18 firstSampleTime time.Time
19 }
20
21 func (r *rttState) init() {
22 r.minRTT = -1
23
24
25
26 const initialRTT = 333 * time.Millisecond
27
28
29 r.smoothedRTT = initialRTT
30 r.rttvar = initialRTT / 2
31 }
32
33 func (r *rttState) establishPersistentCongestion() {
34
35
36
37 r.minRTT = r.latestRTT
38 }
39
40
41
42 func (r *rttState) updateSample(now time.Time, handshakeConfirmed bool, spaceID numberSpace, latestRTT, ackDelay, maxAckDelay time.Duration) {
43 r.latestRTT = latestRTT
44
45 if r.minRTT < 0 {
46
47
48
49 r.minRTT = latestRTT
50
51 r.smoothedRTT = latestRTT
52 r.rttvar = latestRTT / 2
53 r.firstSampleTime = now
54 return
55 }
56
57
58
59
60 r.minRTT = min(r.minRTT, latestRTT)
61
62
63 if handshakeConfirmed {
64 ackDelay = min(ackDelay, maxAckDelay)
65 }
66 adjustedRTT := latestRTT - ackDelay
67 if adjustedRTT < r.minRTT {
68 adjustedRTT = latestRTT
69 }
70 rttvarSample := abs(r.smoothedRTT - adjustedRTT)
71 r.rttvar = (3*r.rttvar + rttvarSample) / 4
72 r.smoothedRTT = ((7 * r.smoothedRTT) + adjustedRTT) / 8
73 }
74
View as plain text