Use Deque in ops queue. (#2418)

* Use Seque in ops queue.

Standardizing some uses
- Change OpsQueue to use Deque so that it can grow/shrink as necessary and
  need not worry about channel getting full and dropping events.
- Change StreamAllocator and TelemetryService to use OpsQueue so that
  they also need not worry about channel size and overflows.

* Address feedback

* delete obvious comment

* clean up
This commit is contained in:
Raja Subramanian
2024-01-28 13:48:30 +05:30
committed by GitHub
parent c2549081c8
commit b71d373f4a
4 changed files with 69 additions and 85 deletions
+10 -15
View File
@@ -20,6 +20,7 @@ import (
"time"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/webhook"
@@ -82,15 +83,15 @@ type TelemetryService interface {
}
const (
workerCleanupWait = 3 * time.Minute
jobQueueBufferSize = 10000
workerCleanupWait = 3 * time.Minute
jobsQueueMinSize = 2048
)
type telemetryService struct {
AnalyticsService
notifier webhook.QueuedNotifier
jobsChan chan func()
notifier webhook.QueuedNotifier
jobsQueue *utils.OpsQueue
lock sync.RWMutex
workers map[livekit.ParticipantID]*StatsWorker
@@ -100,11 +101,12 @@ func NewTelemetryService(notifier webhook.QueuedNotifier, analytics AnalyticsSer
t := &telemetryService{
AnalyticsService: analytics,
notifier: notifier,
jobsChan: make(chan func(), jobQueueBufferSize),
workers: make(map[livekit.ParticipantID]*StatsWorker),
notifier: notifier,
jobsQueue: utils.NewOpsQueue("telemetry", jobsQueueMinSize, true),
workers: make(map[livekit.ParticipantID]*StatsWorker),
}
t.jobsQueue.Start()
go t.run()
return t
@@ -132,19 +134,12 @@ func (t *telemetryService) run() {
t.FlushStats()
case <-cleanupTicker.C:
t.cleanupWorkers()
case op := <-t.jobsChan:
op()
}
}
}
func (t *telemetryService) enqueue(op func()) {
select {
case t.jobsChan <- op:
// success
default:
logger.Warnw("telemetry queue full", nil)
}
t.jobsQueue.Enqueue(op)
}
func (t *telemetryService) getWorker(participantID livekit.ParticipantID) (worker *StatsWorker, ok bool) {