store active stats workers in list (#2690)

* store active stats workers in list

* test

* single node cleanup

* cleanup

* cleanup

* cleanup
This commit is contained in:
Paul Wells
2024-04-26 17:24:10 -07:00
committed by GitHub
parent 15ffb53df9
commit ac1b0e38ca
3 changed files with 81 additions and 72 deletions
+15 -13
View File
@@ -29,6 +29,8 @@ import (
// StatsWorker handles participant stats
type StatsWorker struct {
next *StatsWorker
ctx context.Context
t TelemetryService
roomID livekit.RoomID
@@ -91,8 +93,8 @@ func (s *StatsWorker) IsConnected() bool {
return s.isConnected
}
func (s *StatsWorker) Flush() {
ts := timestamppb.Now()
func (s *StatsWorker) Flush(now time.Time) bool {
ts := timestamppb.New(now)
s.lock.Lock()
stats := make([]*livekit.AnalyticsStat, 0, len(s.incomingPerTrack)+len(s.outgoingPerTrack))
@@ -102,6 +104,8 @@ func (s *StatsWorker) Flush() {
outgoingPerTrack := s.outgoingPerTrack
s.outgoingPerTrack = make(map[livekit.TrackID][]*livekit.AnalyticsStat)
closed := !s.closedAt.IsZero() && now.Sub(s.closedAt) > workerCleanupWait
s.lock.Unlock()
stats = s.collectStats(ts, livekit.StreamType_UPSTREAM, incomingPerTrack, stats)
@@ -109,21 +113,19 @@ func (s *StatsWorker) Flush() {
if len(stats) > 0 {
s.t.SendStats(s.ctx, stats)
}
return closed
}
func (s *StatsWorker) Close() {
s.Flush()
func (s *StatsWorker) Close() bool {
s.lock.Lock()
s.closedAt = time.Now()
s.lock.Unlock()
}
defer s.lock.Unlock()
func (s *StatsWorker) ClosedAt() time.Time {
s.lock.RLock()
defer s.lock.RUnlock()
return s.closedAt
ok := s.closedAt.IsZero()
if ok {
s.closedAt = time.Now()
}
return ok
}
func (s *StatsWorker) collectStats(