Lock stats worker maps (#704)
This commit is contained in:
@@ -2,6 +2,7 @@ package telemetry
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
@@ -19,6 +20,7 @@ type StatsWorker struct {
|
|||||||
participantID livekit.ParticipantID
|
participantID livekit.ParticipantID
|
||||||
participantIdentity livekit.ParticipantIdentity
|
participantIdentity livekit.ParticipantIdentity
|
||||||
|
|
||||||
|
lock sync.Mutex
|
||||||
outgoingPerTrack map[livekit.TrackID][]*livekit.AnalyticsStat
|
outgoingPerTrack map[livekit.TrackID][]*livekit.AnalyticsStat
|
||||||
incomingPerTrack map[livekit.TrackID][]*livekit.AnalyticsStat
|
incomingPerTrack map[livekit.TrackID][]*livekit.AnalyticsStat
|
||||||
}
|
}
|
||||||
@@ -38,52 +40,53 @@ func newStatsWorker(
|
|||||||
roomName: roomName,
|
roomName: roomName,
|
||||||
participantID: participantID,
|
participantID: participantID,
|
||||||
participantIdentity: identity,
|
participantIdentity: identity,
|
||||||
|
outgoingPerTrack: make(map[livekit.TrackID][]*livekit.AnalyticsStat),
|
||||||
outgoingPerTrack: make(map[livekit.TrackID][]*livekit.AnalyticsStat),
|
incomingPerTrack: make(map[livekit.TrackID][]*livekit.AnalyticsStat),
|
||||||
incomingPerTrack: make(map[livekit.TrackID][]*livekit.AnalyticsStat),
|
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatsWorker) OnTrackStat(trackID livekit.TrackID, direction livekit.StreamType, stat *livekit.AnalyticsStat) {
|
func (s *StatsWorker) OnTrackStat(trackID livekit.TrackID, direction livekit.StreamType, stat *livekit.AnalyticsStat) {
|
||||||
|
s.lock.Lock()
|
||||||
if direction == livekit.StreamType_DOWNSTREAM {
|
if direction == livekit.StreamType_DOWNSTREAM {
|
||||||
s.outgoingPerTrack[trackID] = append(s.outgoingPerTrack[trackID], stat)
|
s.outgoingPerTrack[trackID] = append(s.outgoingPerTrack[trackID], stat)
|
||||||
} else {
|
} else {
|
||||||
s.incomingPerTrack[trackID] = append(s.incomingPerTrack[trackID], stat)
|
s.incomingPerTrack[trackID] = append(s.incomingPerTrack[trackID], stat)
|
||||||
}
|
}
|
||||||
|
s.lock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatsWorker) Update() {
|
func (s *StatsWorker) Update() {
|
||||||
ts := timestamppb.Now()
|
ts := timestamppb.Now()
|
||||||
|
|
||||||
|
s.lock.Lock()
|
||||||
stats := make([]*livekit.AnalyticsStat, 0, len(s.incomingPerTrack)+len(s.outgoingPerTrack))
|
stats := make([]*livekit.AnalyticsStat, 0, len(s.incomingPerTrack)+len(s.outgoingPerTrack))
|
||||||
stats = s.collectUpstreamStats(ts, stats)
|
|
||||||
stats = s.collectDownstreamStats(ts, stats)
|
incomingPerTrack := s.incomingPerTrack
|
||||||
|
s.incomingPerTrack = make(map[livekit.TrackID][]*livekit.AnalyticsStat)
|
||||||
|
|
||||||
|
outgoingPerTrack := s.outgoingPerTrack
|
||||||
|
s.outgoingPerTrack = make(map[livekit.TrackID][]*livekit.AnalyticsStat)
|
||||||
|
s.lock.Unlock()
|
||||||
|
|
||||||
|
stats = s.collectStats(ts, livekit.StreamType_UPSTREAM, incomingPerTrack, stats)
|
||||||
|
stats = s.collectStats(ts, livekit.StreamType_DOWNSTREAM, outgoingPerTrack, stats)
|
||||||
if len(stats) > 0 {
|
if len(stats) > 0 {
|
||||||
s.t.Report(s.ctx, stats)
|
s.t.Report(s.ctx, stats)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatsWorker) collectDownstreamStats(ts *timestamppb.Timestamp, stats []*livekit.AnalyticsStat) []*livekit.AnalyticsStat {
|
func (s *StatsWorker) collectStats(
|
||||||
for trackID, analyticsStats := range s.outgoingPerTrack {
|
ts *timestamppb.Timestamp,
|
||||||
analyticsStat := s.getDeltaStats(analyticsStats, ts, trackID, livekit.StreamType_DOWNSTREAM)
|
streamType livekit.StreamType,
|
||||||
|
perTrack map[livekit.TrackID][]*livekit.AnalyticsStat,
|
||||||
|
stats []*livekit.AnalyticsStat,
|
||||||
|
) []*livekit.AnalyticsStat {
|
||||||
|
for trackID, analyticsStats := range perTrack {
|
||||||
|
analyticsStat := s.getDeltaStats(analyticsStats, ts, trackID, streamType)
|
||||||
if analyticsStat != nil {
|
if analyticsStat != nil {
|
||||||
stats = append(stats, analyticsStat)
|
stats = append(stats, analyticsStat)
|
||||||
}
|
}
|
||||||
// clear the queue
|
|
||||||
delete(s.outgoingPerTrack, trackID)
|
|
||||||
}
|
|
||||||
return stats
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StatsWorker) collectUpstreamStats(ts *timestamppb.Timestamp, stats []*livekit.AnalyticsStat) []*livekit.AnalyticsStat {
|
|
||||||
for trackID, analyticsStats := range s.incomingPerTrack {
|
|
||||||
analyticsStat := s.getDeltaStats(analyticsStats, ts, trackID, livekit.StreamType_UPSTREAM)
|
|
||||||
if analyticsStat != nil {
|
|
||||||
stats = append(stats, analyticsStat)
|
|
||||||
}
|
|
||||||
// clear the queue
|
|
||||||
delete(s.incomingPerTrack, trackID)
|
|
||||||
}
|
}
|
||||||
return stats
|
return stats
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user