Use an atomic flag to stop stream allocator (#395)
* Use an atomic flag to stop stream allocator * use a mutex for event channel * RLock while posting event * lock isStopped flag to prevent posting to closed channel
This commit is contained in:
+17
-22
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
|
"github.com/livekit/protocol/utils"
|
||||||
"github.com/pion/interceptor/pkg/cc"
|
"github.com/pion/interceptor/pkg/cc"
|
||||||
"github.com/pion/rtcp"
|
"github.com/pion/rtcp"
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
@@ -143,9 +144,10 @@ type StreamAllocator struct {
|
|||||||
|
|
||||||
state State
|
state State
|
||||||
|
|
||||||
chMu sync.RWMutex
|
eventChMu sync.RWMutex
|
||||||
eventCh chan Event
|
eventCh chan Event
|
||||||
runningCh chan struct{}
|
|
||||||
|
isStopped utils.AtomicFlag
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
|
func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
|
||||||
@@ -156,8 +158,7 @@ func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
|
|||||||
prober: NewProber(ProberParams{
|
prober: NewProber(ProberParams{
|
||||||
Logger: params.Logger,
|
Logger: params.Logger,
|
||||||
}),
|
}),
|
||||||
eventCh: make(chan Event, 20),
|
eventCh: make(chan Event, 20),
|
||||||
runningCh: make(chan struct{}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.initializeEstimate()
|
s.initializeEstimate()
|
||||||
@@ -173,11 +174,14 @@ func (s *StreamAllocator) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) Stop() {
|
func (s *StreamAllocator) Stop() {
|
||||||
s.chMu.Lock()
|
s.eventChMu.Lock()
|
||||||
defer s.chMu.Unlock()
|
if !s.isStopped.TrySet(true) {
|
||||||
|
s.eventChMu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
close(s.runningCh)
|
|
||||||
close(s.eventCh)
|
close(s.eventCh)
|
||||||
|
s.eventChMu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) OnStreamStateChange(f func(update *StreamStateUpdate) error) {
|
func (s *StreamAllocator) OnStreamStateChange(f func(update *StreamStateUpdate) error) {
|
||||||
@@ -302,14 +306,14 @@ func (s *StreamAllocator) onSendProbe(bytesToSend int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) postEvent(event Event) {
|
func (s *StreamAllocator) postEvent(event Event) {
|
||||||
s.chMu.RLock()
|
s.eventChMu.RLock()
|
||||||
defer s.chMu.RUnlock()
|
if s.isStopped.Get() {
|
||||||
|
s.eventChMu.RUnlock()
|
||||||
if !s.isRunning() {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.eventCh <- event
|
s.eventCh <- event
|
||||||
|
s.eventChMu.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) processEvents() {
|
func (s *StreamAllocator) processEvents() {
|
||||||
@@ -318,21 +322,12 @@ func (s *StreamAllocator) processEvents() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) isRunning() bool {
|
|
||||||
select {
|
|
||||||
case <-s.runningCh:
|
|
||||||
return false
|
|
||||||
default:
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StreamAllocator) ping() {
|
func (s *StreamAllocator) ping() {
|
||||||
ticker := time.NewTicker(time.Second)
|
ticker := time.NewTicker(time.Second)
|
||||||
|
|
||||||
for s.isRunning() {
|
for {
|
||||||
<-ticker.C
|
<-ticker.C
|
||||||
if !s.isRunning() {
|
if s.isStopped.Get() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user