1
1
package congestion
2
2
3
3
import (
4
- "sync"
5
4
"time"
6
-
7
- "github.com/named-data/ndnd/std/log"
8
5
)
9
6
10
7
// RTTEstimator provides an interface for estimating round-trip time.
@@ -15,67 +12,4 @@ type RTTEstimator interface {
15
12
DeviationRTT () time.Duration // get the deviation of RTT
16
13
17
14
AddMeasurement (sample time.Duration , retransmitted bool ) // add a new RTT measurement
18
- }
19
-
20
- // KarnRTTEstimator is an implementation of RTTEstimator using Karn's algorithm.
21
- type KarnRTTEstimator struct {
22
- mutex sync.RWMutex
23
-
24
- estimatedRTT time.Duration // estimated RTT
25
- deviationRTT time.Duration // deviation of RTT
26
-
27
- alpha float64 // alpha value for exponential smoothing
28
- beta float64 // beta value for exponential smoothing
29
- }
30
-
31
- // NewKarnRTTEstimator creates a new KarnRTTEstimator.
32
- func NewKarnRTTEstimator (alpha float64 , beta float64 ) * KarnRTTEstimator {
33
- return & KarnRTTEstimator {
34
- estimatedRTT : 0.0 ,
35
- deviationRTT : 0.0 ,
36
-
37
- alpha : alpha ,
38
- beta : beta ,
39
- }
40
- }
41
-
42
- func (rtt * KarnRTTEstimator ) String () string {
43
- return "karn-rtt-estimator"
44
- }
45
-
46
- // EstimatedRTT returns the estimated RTT.
47
- func (rtt * KarnRTTEstimator ) EstimatedRTT () time.Duration {
48
- rtt .mutex .RLock ()
49
- defer rtt .mutex .RUnlock ()
50
-
51
- return rtt .estimatedRTT
52
- }
53
-
54
- // DeviationRTT returns the deviation of RTT.
55
- func (rtt * KarnRTTEstimator ) DeviationRTT () time.Duration {
56
- rtt .mutex .RLock ()
57
- defer rtt .mutex .RUnlock ()
58
-
59
- return rtt .deviationRTT
60
- }
61
-
62
- // AddMeasurement adds a new RTT measurement.
63
- // For Karn's algorithm, it ignores retransmitted packets.
64
- func (rtt * KarnRTTEstimator ) AddMeasurement (sample time.Duration , retransmitted bool ) {
65
- if retransmitted {
66
- return // ignore retransmitted packets
67
- }
68
-
69
- rtt .mutex .Lock ()
70
- defer rtt .mutex .Unlock ()
71
-
72
- // calculate new RTT using Karn's algorithm
73
- newEstimatedRTT := rtt .estimatedRTT .Seconds () + rtt .alpha * (sample - rtt .estimatedRTT ).Seconds ()
74
- newDeviationRTT := rtt .deviationRTT .Seconds () + rtt .beta * (sample - rtt .estimatedRTT ).Seconds ()
75
-
76
- // update RTT
77
- rtt .estimatedRTT = time .Duration (newEstimatedRTT * float64 (time .Second ))
78
- rtt .deviationRTT = time .Duration (newDeviationRTT * float64 (time .Second ))
79
-
80
- log .Debug (rtt , "new RTT" , rtt .estimatedRTT .Seconds (), "new deviation" , rtt .deviationRTT .Seconds ())
81
15
}
0 commit comments