-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathforwardstats.go
More file actions
93 lines (76 loc) · 2.22 KB
/
forwardstats.go
File metadata and controls
93 lines (76 loc) · 2.22 KB
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
package sfu
import (
"fmt"
"sync"
"time"
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
)
const (
highForwardingLatency = 500 * time.Millisecond
skewFactor = 10
)
type ForwardStats struct {
lock sync.Mutex
latency *utils.LatencyAggregate
closeCh chan struct{}
}
func NewForwardStats(latencyUpdateInterval, reportInterval, latencyWindowLength time.Duration) *ForwardStats {
s := &ForwardStats{
latency: utils.NewLatencyAggregate(latencyUpdateInterval, latencyWindowLength),
closeCh: make(chan struct{}),
}
go s.report(reportInterval)
return s
}
func (s *ForwardStats) Update(arrival, left int64) (int64, bool) {
transit := left - arrival
isHighForwardingLatency := false
if time.Duration(transit) > highForwardingLatency {
isHighForwardingLatency = true
}
s.lock.Lock()
s.latency.Update(time.Duration(arrival), float64(transit))
s.lock.Unlock()
return transit, isHighForwardingLatency
}
func (s *ForwardStats) GetStats() (time.Duration, time.Duration) {
s.lock.Lock()
w := s.latency.Summarize()
s.lock.Unlock()
latency, jitter := time.Duration(w.Mean()), time.Duration(w.StdDev())
if jitter > latency*skewFactor {
logger.Infow(
"high jitter in forwarding path",
"latency", latency,
"jitter", jitter,
"stats", fmt.Sprintf("count %.2f, mean %.2f, stdDev %.2f", w.Count(), w.Mean(), w.StdDev()),
)
}
return latency, jitter
}
func (s *ForwardStats) GetLastStats(duration time.Duration) (time.Duration, time.Duration) {
s.lock.Lock()
w := s.latency.SummarizeLast(duration)
s.lock.Unlock()
return time.Duration(w.Mean()), time.Duration(w.StdDev())
}
func (s *ForwardStats) Stop() {
close(s.closeCh)
}
func (s *ForwardStats) report(reportInterval time.Duration) {
ticker := time.NewTicker(reportInterval)
defer ticker.Stop()
for {
select {
case <-s.closeCh:
return
case <-ticker.C:
latency, jitter := s.GetLastStats(reportInterval)
latencySlow, jitterSlow := s.GetStats()
prometheus.RecordForwardJitter(uint32(jitter.Microseconds()), uint32(jitterSlow.Microseconds()))
prometheus.RecordForwardLatency(uint32(latency.Microseconds()), uint32(latencySlow.Microseconds()))
}
}
}