Using shadow pattern for stats workers (#742)
This commit is contained in:
@@ -28,9 +28,9 @@ type telemetryServiceInternal struct {
|
|||||||
webhookPool *workerpool.WorkerPool
|
webhookPool *workerpool.WorkerPool
|
||||||
|
|
||||||
// one worker per participant
|
// one worker per participant
|
||||||
workersMu sync.RWMutex
|
workersMu sync.RWMutex
|
||||||
workers []*StatsWorker
|
workers map[livekit.ParticipantID]*StatsWorker
|
||||||
workersIdx map[livekit.ParticipantID]int
|
workersShadow map[livekit.ParticipantID]*StatsWorker
|
||||||
|
|
||||||
analytics AnalyticsService
|
analytics AnalyticsService
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ func NewTelemetryServiceInternal(notifier webhook.Notifier, analytics AnalyticsS
|
|||||||
return &telemetryServiceInternal{
|
return &telemetryServiceInternal{
|
||||||
notifier: notifier,
|
notifier: notifier,
|
||||||
webhookPool: workerpool.New(maxWebhookWorkers),
|
webhookPool: workerpool.New(maxWebhookWorkers),
|
||||||
workersIdx: make(map[livekit.ParticipantID]int),
|
workers: make(map[livekit.ParticipantID]*StatsWorker),
|
||||||
analytics: analytics,
|
analytics: analytics,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,13 +93,11 @@ func (t *telemetryServiceInternal) Report(ctx context.Context, stats []*livekit.
|
|||||||
|
|
||||||
func (t *telemetryServiceInternal) SendAnalytics() {
|
func (t *telemetryServiceInternal) SendAnalytics() {
|
||||||
t.workersMu.RLock()
|
t.workersMu.RLock()
|
||||||
workers := t.workers
|
workers := t.workersShadow
|
||||||
t.workersMu.RUnlock()
|
t.workersMu.RUnlock()
|
||||||
|
|
||||||
for _, worker := range workers {
|
for _, worker := range workers {
|
||||||
if worker != nil {
|
worker.Update()
|
||||||
worker.Update()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,8 +105,8 @@ func (t *telemetryServiceInternal) getStatsWorker(participantID livekit.Particip
|
|||||||
t.workersMu.RLock()
|
t.workersMu.RLock()
|
||||||
defer t.workersMu.RUnlock()
|
defer t.workersMu.RUnlock()
|
||||||
|
|
||||||
if idx, ok := t.workersIdx[participantID]; ok {
|
if worker, ok := t.workersShadow[participantID]; ok {
|
||||||
return t.workers[idx]
|
return worker
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -54,30 +54,19 @@ func (t *telemetryServiceInternal) ParticipantJoined(
|
|||||||
) {
|
) {
|
||||||
prometheus.IncrementParticipantJoin(1)
|
prometheus.IncrementParticipantJoin(1)
|
||||||
|
|
||||||
newWorker := newStatsWorker(
|
|
||||||
ctx,
|
|
||||||
t,
|
|
||||||
livekit.RoomID(room.Sid),
|
|
||||||
livekit.RoomName(room.Name),
|
|
||||||
livekit.ParticipantID(participant.Sid),
|
|
||||||
livekit.ParticipantIdentity(participant.Identity),
|
|
||||||
)
|
|
||||||
t.workersMu.Lock()
|
t.workersMu.Lock()
|
||||||
var free = false
|
if _, ok := t.workers[livekit.ParticipantID(participant.Sid)]; !ok {
|
||||||
for idx, worker := range t.workers {
|
newWorker := newStatsWorker(
|
||||||
if worker != nil {
|
ctx,
|
||||||
continue
|
t,
|
||||||
}
|
livekit.RoomID(room.Sid),
|
||||||
|
livekit.RoomName(room.Name),
|
||||||
|
livekit.ParticipantID(participant.Sid),
|
||||||
|
livekit.ParticipantIdentity(participant.Identity),
|
||||||
|
)
|
||||||
|
t.workers[livekit.ParticipantID(participant.Sid)] = newWorker
|
||||||
|
|
||||||
free = true
|
t.shadowWorkersLocked()
|
||||||
t.workersIdx[livekit.ParticipantID(participant.Sid)] = idx
|
|
||||||
t.workers[idx] = newWorker
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if !free {
|
|
||||||
t.workersIdx[livekit.ParticipantID(participant.Sid)] = len(t.workers)
|
|
||||||
t.workers = append(t.workers, newWorker)
|
|
||||||
}
|
}
|
||||||
t.workersMu.Unlock()
|
t.workersMu.Unlock()
|
||||||
|
|
||||||
@@ -120,9 +109,10 @@ func (t *telemetryServiceInternal) ParticipantLeft(ctx context.Context, room *li
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.workersMu.Lock()
|
t.workersMu.Lock()
|
||||||
if idx, ok := t.workersIdx[livekit.ParticipantID(participant.Sid)]; ok {
|
if _, ok := t.workers[livekit.ParticipantID(participant.Sid)]; ok {
|
||||||
delete(t.workersIdx, livekit.ParticipantID(participant.Sid))
|
delete(t.workers, livekit.ParticipantID(participant.Sid))
|
||||||
t.workers[idx] = nil
|
|
||||||
|
t.shadowWorkersLocked()
|
||||||
}
|
}
|
||||||
t.workersMu.Unlock()
|
t.workersMu.Unlock()
|
||||||
|
|
||||||
@@ -340,3 +330,10 @@ func (t *telemetryServiceInternal) EgressEnded(ctx context.Context, info *liveki
|
|||||||
RoomId: info.RoomId,
|
RoomId: info.RoomId,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *telemetryServiceInternal) shadowWorkersLocked() {
|
||||||
|
t.workersShadow = make(map[livekit.ParticipantID]*StatsWorker, len(t.workers))
|
||||||
|
for pID, worker := range t.workers {
|
||||||
|
t.workersShadow[pID] = worker
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user