-
Notifications
You must be signed in to change notification settings - Fork 16
/
rtt.go
66 lines (57 loc) · 1.74 KB
/
rtt.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
package nex
import (
"math"
"sync"
"time"
)
const (
alpha float64 = 1.0 / 8.0
beta float64 = 1.0 / 4.0
k float64 = 4.0
)
// RTT is an implementation of rdv::RTT.
// Used to calculate the average round trip time of reliable packets
type RTT struct {
sync.Mutex
lastRTT float64
average float64
variance float64
initialized bool
}
// Adjust updates the average RTT with the new value
func (rtt *RTT) Adjust(next time.Duration) {
// * This calculation comes from the RFC6298 which defines RTT calculation for TCP packets
rtt.Lock()
if rtt.initialized {
rtt.variance = (1.0-beta)*rtt.variance + beta*math.Abs(rtt.variance-float64(next))
rtt.average = (1.0-alpha)*rtt.average + alpha*float64(next)
} else {
rtt.lastRTT = float64(next)
rtt.variance = float64(next) / 2
rtt.average = float64(next) + k*rtt.variance
rtt.initialized = true
}
rtt.Unlock()
}
// GetRTTSmoothedAvg returns the smoothed average of this RTT, it is used in calls to the custom
// RTO calculation function set on `PRUDPEndpoint::SetCalcRetransmissionTimeoutCallback`
func (rtt *RTT) GetRTTSmoothedAvg() float64 {
return rtt.average / 16
}
// GetRTTSmoothedDev returns the smoothed standard deviation of this RTT, it is used in calls to the custom
// RTO calculation function set on `PRUDPEndpoint::SetCalcRetransmissionTimeoutCallback`
func (rtt *RTT) GetRTTSmoothedDev() float64 {
return rtt.variance / 8
}
// Initialized returns a bool indicating whether this RTT has been initialized
func (rtt *RTT) Initialized() bool {
return rtt.initialized
}
// GetRTO returns the current average
func (rtt *RTT) Average() time.Duration {
return time.Duration(rtt.average)
}
// NewRTT returns a new RTT based on the first value
func NewRTT() *RTT {
return &RTT{}
}