feat: SubscriptionManager to consolidate subscription handling (#1317)
Added a new manager to handle all subscription needs. Implemented using reconciler pattern. The goals are: improve subscription resilience by separating desired state and current state reduce complexity of synchronous processing better detect failures with the ability to trigger full reconnect
This commit is contained in:
@@ -30,19 +30,16 @@ type ParticipantSupervisor struct {
|
||||
lock sync.RWMutex
|
||||
isPublisherConnected bool
|
||||
publications map[livekit.TrackID]*trackMonitor
|
||||
subscriptions map[livekit.TrackID]*trackMonitor
|
||||
|
||||
isStopped atomic.Bool
|
||||
|
||||
onPublicationError func(trackID livekit.TrackID)
|
||||
onSubscriptionError func(trackID livekit.TrackID)
|
||||
onPublicationError func(trackID livekit.TrackID)
|
||||
}
|
||||
|
||||
func NewParticipantSupervisor(params ParticipantSupervisorParams) *ParticipantSupervisor {
|
||||
p := &ParticipantSupervisor{
|
||||
params: params,
|
||||
publications: make(map[livekit.TrackID]*trackMonitor),
|
||||
subscriptions: make(map[livekit.TrackID]*trackMonitor),
|
||||
params: params,
|
||||
publications: make(map[livekit.TrackID]*trackMonitor),
|
||||
}
|
||||
|
||||
go p.checkState()
|
||||
@@ -68,20 +65,6 @@ func (p *ParticipantSupervisor) getOnPublicationError() func(trackID livekit.Tra
|
||||
return p.onPublicationError
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) OnSubscriptionError(f func(trackID livekit.TrackID)) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
p.onSubscriptionError = f
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) getOnSubscriptionError() func(trackID livekit.TrackID) {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
|
||||
return p.onSubscriptionError
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) SetPublisherPeerConnectionConnected(isConnected bool) {
|
||||
p.lock.Lock()
|
||||
p.isPublisherConnected = isConnected
|
||||
@@ -139,55 +122,6 @@ func (p *ParticipantSupervisor) ClearPublishedTrack(trackID livekit.TrackID, pub
|
||||
p.lock.RUnlock()
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) UpdateSubscription(trackID livekit.TrackID, isSubscribe bool, sourceTrack types.MediaTrack) {
|
||||
p.lock.Lock()
|
||||
sm, ok := p.subscriptions[trackID]
|
||||
if !ok {
|
||||
sm = &trackMonitor{
|
||||
opMon: NewSubscriptionMonitor(SubscriptionMonitorParams{TrackID: trackID, Logger: p.params.Logger}),
|
||||
}
|
||||
p.subscriptions[trackID] = sm
|
||||
}
|
||||
sm.opMon.PostEvent(
|
||||
types.OperationMonitorEventUpdateSubscription,
|
||||
SubscriptionOpParams{
|
||||
SourceTrack: sourceTrack,
|
||||
IsSubscribe: isSubscribe,
|
||||
},
|
||||
)
|
||||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) SetSubscribedTrack(trackID livekit.TrackID, subTrack types.SubscribedTrack, sourceTrack types.MediaTrack) {
|
||||
p.lock.RLock()
|
||||
sm, ok := p.subscriptions[trackID]
|
||||
if ok {
|
||||
sm.opMon.PostEvent(
|
||||
types.OperationMonitorEventSetSubscribedTrack,
|
||||
UpdateSubscribedTrackParams{
|
||||
SourceTrack: sourceTrack,
|
||||
SubscribedTrack: subTrack,
|
||||
},
|
||||
)
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) ClearSubscribedTrack(trackID livekit.TrackID, subTrack types.SubscribedTrack, sourceTrack types.MediaTrack) {
|
||||
p.lock.RLock()
|
||||
sm, ok := p.subscriptions[trackID]
|
||||
if ok {
|
||||
sm.opMon.PostEvent(
|
||||
types.OperationMonitorEventClearSubscribedTrack,
|
||||
UpdateSubscribedTrackParams{
|
||||
SourceTrack: sourceTrack,
|
||||
SubscribedTrack: subTrack,
|
||||
},
|
||||
)
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) checkState() {
|
||||
ticker := time.NewTicker(monitorInterval)
|
||||
defer ticker.Stop()
|
||||
@@ -196,7 +130,6 @@ func (p *ParticipantSupervisor) checkState() {
|
||||
<-ticker.C
|
||||
|
||||
p.checkPublications()
|
||||
p.checkSubscriptions()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,39 +168,3 @@ func (p *ParticipantSupervisor) checkPublications() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) checkSubscriptions() {
|
||||
var erroredSubscriptions []livekit.TrackID
|
||||
var removableSubscriptions []livekit.TrackID
|
||||
p.lock.RLock()
|
||||
for trackID, sm := range p.subscriptions {
|
||||
if err := sm.opMon.Check(); err != nil {
|
||||
if sm.err == nil {
|
||||
p.params.Logger.Errorw("supervisor error on subscription", err, "trackID", trackID)
|
||||
sm.err = err
|
||||
erroredSubscriptions = append(erroredSubscriptions, trackID)
|
||||
}
|
||||
} else {
|
||||
if sm.err != nil {
|
||||
p.params.Logger.Infow("supervisor subscription recovered", "trackID", trackID)
|
||||
sm.err = err
|
||||
}
|
||||
if sm.opMon.IsIdle() {
|
||||
removableSubscriptions = append(removableSubscriptions, trackID)
|
||||
}
|
||||
}
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
|
||||
p.lock.Lock()
|
||||
for _, trackID := range removableSubscriptions {
|
||||
delete(p.subscriptions, trackID)
|
||||
}
|
||||
p.lock.Unlock()
|
||||
|
||||
if onSubscriptionError := p.getOnSubscriptionError(); onSubscriptionError != nil {
|
||||
for _, trackID := range erroredSubscriptions {
|
||||
onSubscriptionError(trackID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,189 +0,0 @@
|
||||
package supervisor
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gammazero/deque"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
transitionWaitDuration = 20 * time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
errSubscribeTimeout = errors.New("subscribe time out")
|
||||
errUnsubscribeTimeout = errors.New("unsubscribe time out")
|
||||
)
|
||||
|
||||
type transition struct {
|
||||
isSubscribe bool
|
||||
at time.Time
|
||||
}
|
||||
|
||||
type subscriptionOps struct {
|
||||
desiredTransitions deque.Deque
|
||||
|
||||
subscribedTrack types.SubscribedTrack
|
||||
}
|
||||
|
||||
type SubscriptionOpParams struct {
|
||||
SourceTrack types.MediaTrack
|
||||
IsSubscribe bool
|
||||
}
|
||||
|
||||
type UpdateSubscribedTrackParams struct {
|
||||
SourceTrack types.MediaTrack
|
||||
SubscribedTrack types.SubscribedTrack
|
||||
}
|
||||
|
||||
type SubscriptionMonitorParams struct {
|
||||
TrackID livekit.TrackID
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type SubscriptionMonitor struct {
|
||||
params SubscriptionMonitorParams
|
||||
|
||||
lock sync.RWMutex
|
||||
subscriptionOpsBySource map[types.MediaTrack]*subscriptionOps
|
||||
}
|
||||
|
||||
func NewSubscriptionMonitor(params SubscriptionMonitorParams) *SubscriptionMonitor {
|
||||
s := &SubscriptionMonitor{
|
||||
params: params,
|
||||
subscriptionOpsBySource: make(map[types.MediaTrack]*subscriptionOps),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) PostEvent(ome types.OperationMonitorEvent, omd types.OperationMonitorData) {
|
||||
switch ome {
|
||||
case types.OperationMonitorEventUpdateSubscription:
|
||||
s.updateSubscription(omd.(SubscriptionOpParams))
|
||||
case types.OperationMonitorEventSetSubscribedTrack:
|
||||
s.setSubscribedTrack(omd.(UpdateSubscribedTrackParams))
|
||||
case types.OperationMonitorEventClearSubscribedTrack:
|
||||
s.clearSubscribedTrack(omd.(UpdateSubscribedTrackParams))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) updateSubscription(params SubscriptionOpParams) {
|
||||
if params.IsSubscribe {
|
||||
prometheus.AddSubscribeAttempt(params.SourceTrack.Kind().String())
|
||||
}
|
||||
|
||||
s.lock.Lock()
|
||||
|
||||
so := s.getOrCreateSubscriptionOpsForSource(params.SourceTrack)
|
||||
so.desiredTransitions.PushBack(
|
||||
&transition{
|
||||
isSubscribe: params.IsSubscribe,
|
||||
at: time.Now(),
|
||||
},
|
||||
)
|
||||
s.update()
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) setSubscribedTrack(params UpdateSubscribedTrackParams) {
|
||||
s.lock.Lock()
|
||||
so := s.getOrCreateSubscriptionOpsForSource(params.SourceTrack)
|
||||
so.subscribedTrack = params.SubscribedTrack
|
||||
s.update()
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) clearSubscribedTrack(params UpdateSubscribedTrackParams) {
|
||||
s.lock.Lock()
|
||||
so := s.getOrCreateSubscriptionOpsForSource(params.SourceTrack)
|
||||
if so.subscribedTrack == params.SubscribedTrack {
|
||||
so.subscribedTrack = nil
|
||||
} else {
|
||||
s.params.Logger.Errorw("supervisor: mismatched subscribed track on clear", nil, "trackID", s.params.TrackID)
|
||||
}
|
||||
|
||||
s.update()
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) Check() error {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
for _, so := range s.subscriptionOpsBySource {
|
||||
var tx *transition
|
||||
if so.desiredTransitions.Len() > 0 {
|
||||
tx = so.desiredTransitions.Front().(*transition)
|
||||
}
|
||||
|
||||
if tx == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if time.Since(tx.at) > transitionWaitDuration {
|
||||
// timed out waiting for transition
|
||||
if tx.isSubscribe {
|
||||
return errSubscribeTimeout
|
||||
} else {
|
||||
return errUnsubscribeTimeout
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// give more time for transition to happen
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) IsIdle() bool {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
return len(s.subscriptionOpsBySource) == 0
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) getOrCreateSubscriptionOpsForSource(sourceTrack types.MediaTrack) *subscriptionOps {
|
||||
so := s.subscriptionOpsBySource[sourceTrack]
|
||||
if so == nil {
|
||||
so = &subscriptionOps{}
|
||||
so.desiredTransitions.SetMinCapacity(4)
|
||||
s.subscriptionOpsBySource[sourceTrack] = so
|
||||
}
|
||||
|
||||
return so
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) update() {
|
||||
for sourceTrack, so := range s.subscriptionOpsBySource {
|
||||
for {
|
||||
var tx *transition
|
||||
if so.desiredTransitions.Len() > 0 {
|
||||
tx = so.desiredTransitions.PopFront().(*transition)
|
||||
}
|
||||
|
||||
if tx == nil {
|
||||
break
|
||||
}
|
||||
|
||||
if (tx.isSubscribe && so.subscribedTrack == nil) || (!tx.isSubscribe && so.subscribedTrack != nil) {
|
||||
// put it back as the condition is not satisfied
|
||||
so.desiredTransitions.PushFront(tx)
|
||||
break
|
||||
}
|
||||
|
||||
if tx.isSubscribe && so.subscribedTrack != nil {
|
||||
prometheus.AddSubscribeSuccess(so.subscribedTrack.MediaTrack().Kind().String())
|
||||
}
|
||||
|
||||
if so.desiredTransitions.Len() == 0 && so.subscribedTrack == nil {
|
||||
delete(s.subscriptionOpsBySource, sourceTrack)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user