add typed ops queue (#2655)

* add typed ops queue

* tidy
This commit is contained in:
Paul Wells
2024-04-16 02:51:49 -07:00
committed by GitHub
parent 844cecaffc
commit 5c38d58987
2 changed files with 69 additions and 28 deletions
+16 -18
View File
@@ -168,7 +168,7 @@ type StreamAllocator struct {
state streamAllocatorState state streamAllocatorState
eventsQueue *utils.OpsQueue eventsQueue *utils.TypedOpsQueue[Event]
isStopped atomic.Bool isStopped atomic.Bool
} }
@@ -182,7 +182,7 @@ func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
}), }),
// STREAM-ALLOCATOR-DATA rateMonitor: NewRateMonitor(), // STREAM-ALLOCATOR-DATA rateMonitor: NewRateMonitor(),
videoTracks: make(map[livekit.TrackID]*Track), videoTracks: make(map[livekit.TrackID]*Track),
eventsQueue: utils.NewOpsQueue(utils.OpsQueueParams{ eventsQueue: utils.NewTypedOpsQueue[Event](utils.OpsQueueParams{
Name: "stream-allocator", Name: "stream-allocator",
MinSize: 64, MinSize: 64,
Logger: params.Logger, Logger: params.Logger,
@@ -559,9 +559,7 @@ func (s *StreamAllocator) maybePostEventAllocateTrack(downTrack *sfu.DownTrack)
} }
func (s *StreamAllocator) postEvent(event Event) { func (s *StreamAllocator) postEvent(event Event) {
s.eventsQueue.Enqueue(func() { s.eventsQueue.Enqueue(s.handleEvent, event)
s.handleEvent(&event)
})
} }
func (s *StreamAllocator) ping() { func (s *StreamAllocator) ping() {
@@ -580,7 +578,7 @@ func (s *StreamAllocator) ping() {
} }
} }
func (s *StreamAllocator) handleEvent(event *Event) { func (s *StreamAllocator) handleEvent(event Event) {
switch event.Signal { switch event.Signal {
case streamAllocatorSignalAllocateTrack: case streamAllocatorSignalAllocateTrack:
s.handleSignalAllocateTrack(event) s.handleSignalAllocateTrack(event)
@@ -611,7 +609,7 @@ func (s *StreamAllocator) handleEvent(event *Event) {
} }
} }
func (s *StreamAllocator) handleSignalAllocateTrack(event *Event) { func (s *StreamAllocator) handleSignalAllocateTrack(event Event) {
s.videoTracksMu.Lock() s.videoTracksMu.Lock()
track := s.videoTracks[event.TrackID] track := s.videoTracks[event.TrackID]
if track != nil { if track != nil {
@@ -624,7 +622,7 @@ func (s *StreamAllocator) handleSignalAllocateTrack(event *Event) {
} }
} }
func (s *StreamAllocator) handleSignalAllocateAllTracks(event *Event) { func (s *StreamAllocator) handleSignalAllocateAllTracks(Event) {
s.videoTracksMu.Lock() s.videoTracksMu.Lock()
s.isAllocateAllPending = false s.isAllocateAllPending = false
s.videoTracksMu.Unlock() s.videoTracksMu.Unlock()
@@ -634,11 +632,11 @@ func (s *StreamAllocator) handleSignalAllocateAllTracks(event *Event) {
} }
} }
func (s *StreamAllocator) handleSignalAdjustState(event *Event) { func (s *StreamAllocator) handleSignalAdjustState(Event) {
s.adjustState() s.adjustState()
} }
func (s *StreamAllocator) handleSignalEstimate(event *Event) { func (s *StreamAllocator) handleSignalEstimate(event Event) {
receivedEstimate, _ := event.Data.(int64) receivedEstimate, _ := event.Data.(int64)
s.lastReceivedEstimate = receivedEstimate s.lastReceivedEstimate = receivedEstimate
// s.monitorRate(receivedEstimate) // s.monitorRate(receivedEstimate)
@@ -651,7 +649,7 @@ func (s *StreamAllocator) handleSignalEstimate(event *Event) {
} }
} }
func (s *StreamAllocator) handleSignalPeriodicPing(event *Event) { func (s *StreamAllocator) handleSignalPeriodicPing(Event) {
// finalize probe if necessary // finalize probe if necessary
trend, _ := s.channelObserver.GetTrend() trend, _ := s.channelObserver.GetTrend()
isHandled, isNotFailing, isGoalReached := s.probeController.MaybeFinalizeProbe( isHandled, isNotFailing, isGoalReached := s.probeController.MaybeFinalizeProbe(
@@ -671,7 +669,7 @@ func (s *StreamAllocator) handleSignalPeriodicPing(event *Event) {
// s.updateTracksHistory() // s.updateTracksHistory()
} }
func (s *StreamAllocator) handleSignalSendProbe(event *Event) { func (s *StreamAllocator) handleSignalSendProbe(event Event) {
bytesToSend := event.Data.(int) bytesToSend := event.Data.(int)
if bytesToSend <= 0 { if bytesToSend <= 0 {
return return
@@ -692,12 +690,12 @@ func (s *StreamAllocator) handleSignalSendProbe(event *Event) {
} }
} }
func (s *StreamAllocator) handleSignalProbeClusterDone(event *Event) { func (s *StreamAllocator) handleSignalProbeClusterDone(event Event) {
info, _ := event.Data.(ProbeClusterInfo) info, _ := event.Data.(ProbeClusterInfo)
s.probeController.ProbeClusterDone(info) s.probeController.ProbeClusterDone(info)
} }
func (s *StreamAllocator) handleSignalResume(event *Event) { func (s *StreamAllocator) handleSignalResume(event Event) {
s.videoTracksMu.Lock() s.videoTracksMu.Lock()
track := s.videoTracks[event.TrackID] track := s.videoTracks[event.TrackID]
s.videoTracksMu.Unlock() s.videoTracksMu.Unlock()
@@ -711,11 +709,11 @@ func (s *StreamAllocator) handleSignalResume(event *Event) {
} }
} }
func (s *StreamAllocator) handleSignalSetAllowPause(event *Event) { func (s *StreamAllocator) handleSignalSetAllowPause(event Event) {
s.allowPause = event.Data.(bool) s.allowPause = event.Data.(bool)
} }
func (s *StreamAllocator) handleSignalSetChannelCapacity(event *Event) { func (s *StreamAllocator) handleSignalSetChannelCapacity(event Event) {
s.overriddenChannelCapacity = event.Data.(int64) s.overriddenChannelCapacity = event.Data.(int64)
if s.overriddenChannelCapacity > 0 { if s.overriddenChannelCapacity > 0 {
s.params.Logger.Infow("allocating on override channel capacity", "override", s.overriddenChannelCapacity) s.params.Logger.Infow("allocating on override channel capacity", "override", s.overriddenChannelCapacity)
@@ -726,7 +724,7 @@ func (s *StreamAllocator) handleSignalSetChannelCapacity(event *Event) {
} }
/* STREAM-ALLOCATOR-DATA /* STREAM-ALLOCATOR-DATA
func (s *StreamAllocator) handleSignalNACK(event *Event) { func (s *StreamAllocator) handleSignalNACK(event Event) {
nackInfos := event.Data.([]sfu.NackInfo) nackInfos := event.Data.([]sfu.NackInfo)
s.videoTracksMu.Lock() s.videoTracksMu.Lock()
@@ -738,7 +736,7 @@ func (s *StreamAllocator) handleSignalNACK(event *Event) {
} }
} }
func (s *StreamAllocator) handleSignalRTCPReceiverReport(event *Event) { func (s *StreamAllocator) handleSignalRTCPReceiverReport(event Event) {
rr := event.Data.(rtcp.ReceptionReport) rr := event.Data.(rtcp.ReceptionReport)
s.videoTracksMu.Lock() s.videoTracksMu.Lock()
+53 -10
View File
@@ -19,8 +19,8 @@ import (
"sync" "sync"
"github.com/gammazero/deque" "github.com/gammazero/deque"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
) )
type OpsQueueParams struct { type OpsQueueParams struct {
@@ -30,28 +30,71 @@ type OpsQueueParams struct {
Logger logger.Logger Logger logger.Logger
} }
type untypedOpsQueueOp func()
func (it untypedOpsQueueOp) run() {
it()
}
type OpsQueue struct { type OpsQueue struct {
OpsQueueBase[untypedOpsQueueOp]
}
func NewOpsQueue(params OpsQueueParams) *OpsQueue {
return &OpsQueue{
OpsQueueBase: *newOpsQueueBase[untypedOpsQueueOp](params),
}
}
type typedOpsQueueOp[T any] struct {
fn func(T)
arg T
}
func (it typedOpsQueueOp[T]) run() {
it.fn(it.arg)
}
type TypedOpsQueue[T any] struct {
OpsQueueBase[typedOpsQueueOp[T]]
}
func NewTypedOpsQueue[T any](params OpsQueueParams) *TypedOpsQueue[T] {
return &TypedOpsQueue[T]{
OpsQueueBase: *newOpsQueueBase[typedOpsQueueOp[T]](params),
}
}
func (oq *TypedOpsQueue[T]) Enqueue(fn func(T), arg T) {
oq.OpsQueueBase.Enqueue(typedOpsQueueOp[T]{fn, arg})
}
type opsQueueItem interface {
run()
}
type OpsQueueBase[T opsQueueItem] struct {
params OpsQueueParams params OpsQueueParams
lock sync.Mutex lock sync.Mutex
ops deque.Deque[func()] ops deque.Deque[T]
wake chan struct{} wake chan struct{}
isStarted bool isStarted bool
doneChan chan struct{} doneChan chan struct{}
isStopped bool isStopped bool
} }
func NewOpsQueue(params OpsQueueParams) *OpsQueue { func newOpsQueueBase[T opsQueueItem](params OpsQueueParams) *OpsQueueBase[T] {
oq := &OpsQueue{ oq := &OpsQueueBase[T]{
params: params, params: params,
wake: make(chan struct{}, 1), wake: make(chan struct{}, 1),
doneChan: make(chan struct{}), doneChan: make(chan struct{}),
} }
oq.ops.SetMinCapacity(uint(utils.Min(bits.Len64(uint64(oq.params.MinSize-1)), 7))) oq.ops.SetMinCapacity(uint(min(bits.Len64(uint64(oq.params.MinSize-1)), 7)))
return oq return oq
} }
func (oq *OpsQueue) Start() { func (oq *OpsQueueBase[T]) Start() {
oq.lock.Lock() oq.lock.Lock()
if oq.isStarted { if oq.isStarted {
oq.lock.Unlock() oq.lock.Unlock()
@@ -64,7 +107,7 @@ func (oq *OpsQueue) Start() {
go oq.process() go oq.process()
} }
func (oq *OpsQueue) Stop() <-chan struct{} { func (oq *OpsQueueBase[T]) Stop() <-chan struct{} {
oq.lock.Lock() oq.lock.Lock()
if oq.isStopped { if oq.isStopped {
oq.lock.Unlock() oq.lock.Unlock()
@@ -77,7 +120,7 @@ func (oq *OpsQueue) Stop() <-chan struct{} {
return oq.doneChan return oq.doneChan
} }
func (oq *OpsQueue) Enqueue(op func()) { func (oq *OpsQueueBase[T]) Enqueue(op T) {
oq.lock.Lock() oq.lock.Lock()
defer oq.lock.Unlock() defer oq.lock.Unlock()
@@ -94,7 +137,7 @@ func (oq *OpsQueue) Enqueue(op func()) {
} }
} }
func (oq *OpsQueue) process() { func (oq *OpsQueueBase[T]) process() {
defer close(oq.doneChan) defer close(oq.doneChan)
for { for {
@@ -113,7 +156,7 @@ func (oq *OpsQueue) process() {
op := oq.ops.PopFront() op := oq.ops.PopFront()
oq.lock.Unlock() oq.lock.Unlock()
op() op.run()
} }
} }
} }