Use a go routine to clean up stats workers. (#836)
* Use a go routine to clean up stats workers. It is possible that certain events (like TrackUnpublished) can happen after the participant is closed. For webhooks pertaining to those events, need details like room name/id. So,reap stats workers a little while after the participant left event happens. * handle data race report * log analytics worker reap * debug log
This commit is contained in:
@@ -3,20 +3,26 @@ package telemetry
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gammazero/workerpool"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/webhook"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
||||
)
|
||||
|
||||
const maxWebhookWorkers = 50
|
||||
const (
|
||||
maxWebhookWorkers = 50
|
||||
workerCleanupWait = 3 * time.Minute
|
||||
)
|
||||
|
||||
type TelemetryServiceInternal interface {
|
||||
TelemetryService
|
||||
SendAnalytics()
|
||||
CleanupWorkers()
|
||||
}
|
||||
|
||||
type TelemetryReporter interface {
|
||||
@@ -103,6 +109,30 @@ func (t *telemetryServiceInternal) SendAnalytics() {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *telemetryServiceInternal) CleanupWorkers() {
|
||||
t.workersMu.RLock()
|
||||
workers := t.workers
|
||||
t.workersMu.RUnlock()
|
||||
|
||||
for _, worker := range workers {
|
||||
if worker == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
closedAt := worker.ClosedAt()
|
||||
if !closedAt.IsZero() && time.Since(closedAt) > workerCleanupWait {
|
||||
pID := worker.ParticipantID()
|
||||
logger.Debugw("reaping analytics worker for participant", "pID", pID)
|
||||
t.workersMu.Lock()
|
||||
if idx, ok := t.workersIdx[pID]; ok {
|
||||
delete(t.workersIdx, pID)
|
||||
t.workers[idx] = nil
|
||||
}
|
||||
t.workersMu.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *telemetryServiceInternal) getStatsWorker(participantID livekit.ParticipantID) *StatsWorker {
|
||||
t.workersMu.RLock()
|
||||
defer t.workersMu.RUnlock()
|
||||
|
||||
Reference in New Issue
Block a user