Keep track of pending reconciles to avoid duplicate queueReconcile (#1474)
This commit is contained in:
@@ -1059,7 +1059,7 @@ func (p *ParticipantImpl) setupSubscriptionManager() {
|
|||||||
Telemetry: p.params.Telemetry,
|
Telemetry: p.params.Telemetry,
|
||||||
OnTrackSubscribed: p.onTrackSubscribed,
|
OnTrackSubscribed: p.onTrackSubscribed,
|
||||||
OnTrackUnsubscribed: p.onTrackUnsubscribed,
|
OnTrackUnsubscribed: p.onTrackUnsubscribed,
|
||||||
OnSubcriptionError: p.onSubscriptionError,
|
OnSubscriptionError: p.onSubscriptionError,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ type SubscriptionManagerParams struct {
|
|||||||
TrackResolver types.MediaTrackResolver
|
TrackResolver types.MediaTrackResolver
|
||||||
OnTrackSubscribed func(subTrack types.SubscribedTrack)
|
OnTrackSubscribed func(subTrack types.SubscribedTrack)
|
||||||
OnTrackUnsubscribed func(subTrack types.SubscribedTrack)
|
OnTrackUnsubscribed func(subTrack types.SubscribedTrack)
|
||||||
OnSubcriptionError func(trackID livekit.TrackID)
|
OnSubscriptionError func(trackID livekit.TrackID)
|
||||||
Telemetry telemetry.TelemetryService
|
Telemetry telemetry.TelemetryService
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,21 +58,24 @@ type SubscriptionManager struct {
|
|||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
subscriptions map[livekit.TrackID]*trackSubscription
|
subscriptions map[livekit.TrackID]*trackSubscription
|
||||||
subscribedTo map[livekit.ParticipantID]map[livekit.TrackID]struct{}
|
subscribedTo map[livekit.ParticipantID]map[livekit.TrackID]struct{}
|
||||||
reconcileCh chan livekit.TrackID
|
// keeps track of tracks that are already queued for reconcile to avoid duplicating reconcile requests
|
||||||
closeCh chan struct{}
|
pendingReconcile map[livekit.TrackID]struct{}
|
||||||
doneCh chan struct{}
|
reconcileCh chan livekit.TrackID
|
||||||
|
closeCh chan struct{}
|
||||||
|
doneCh chan struct{}
|
||||||
|
|
||||||
onSubscribeStatusChanged func(publisherID livekit.ParticipantID, subscribed bool)
|
onSubscribeStatusChanged func(publisherID livekit.ParticipantID, subscribed bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriptionManager(params SubscriptionManagerParams) *SubscriptionManager {
|
func NewSubscriptionManager(params SubscriptionManagerParams) *SubscriptionManager {
|
||||||
m := &SubscriptionManager{
|
m := &SubscriptionManager{
|
||||||
params: params,
|
params: params,
|
||||||
subscriptions: make(map[livekit.TrackID]*trackSubscription),
|
subscriptions: make(map[livekit.TrackID]*trackSubscription),
|
||||||
subscribedTo: make(map[livekit.ParticipantID]map[livekit.TrackID]struct{}),
|
subscribedTo: make(map[livekit.ParticipantID]map[livekit.TrackID]struct{}),
|
||||||
reconcileCh: make(chan livekit.TrackID, 50),
|
pendingReconcile: make(map[livekit.TrackID]struct{}),
|
||||||
closeCh: make(chan struct{}),
|
reconcileCh: make(chan livekit.TrackID, 50),
|
||||||
doneCh: make(chan struct{}),
|
closeCh: make(chan struct{}),
|
||||||
|
doneCh: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
go m.reconcileWorker()
|
go m.reconcileWorker()
|
||||||
@@ -307,7 +310,7 @@ func (m *SubscriptionManager) reconcileSubscription(s *trackSubscription) {
|
|||||||
"attempt", numAttempts,
|
"attempt", numAttempts,
|
||||||
)
|
)
|
||||||
s.maybeRecordError(m.params.Telemetry, m.params.Participant.ID(), err, false)
|
s.maybeRecordError(m.params.Telemetry, m.params.Participant.ID(), err, false)
|
||||||
m.params.OnSubcriptionError(s.trackID)
|
m.params.OnSubscriptionError(s.trackID)
|
||||||
} else {
|
} else {
|
||||||
s.logger.Debugw("failed to subscribe, retrying",
|
s.logger.Debugw("failed to subscribe, retrying",
|
||||||
"error", err,
|
"error", err,
|
||||||
@@ -343,13 +346,20 @@ func (m *SubscriptionManager) reconcileSubscription(s *trackSubscription) {
|
|||||||
if s.durationSinceStart() > subscriptionTimeout {
|
if s.durationSinceStart() > subscriptionTimeout {
|
||||||
s.logger.Errorw("track not bound after timeout", nil)
|
s.logger.Errorw("track not bound after timeout", nil)
|
||||||
s.maybeRecordError(m.params.Telemetry, m.params.Participant.ID(), ErrTrackNotBound, false)
|
s.maybeRecordError(m.params.Telemetry, m.params.Participant.ID(), ErrTrackNotBound, false)
|
||||||
m.params.OnSubcriptionError(s.trackID)
|
m.params.OnSubscriptionError(s.trackID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// trigger an immediate reconcilation, when trackID is empty, will reconcile all subscriptions
|
// trigger an immediate reconciliation, when trackID is empty, will reconcile all subscriptions
|
||||||
func (m *SubscriptionManager) queueReconcile(trackID livekit.TrackID) {
|
func (m *SubscriptionManager) queueReconcile(trackID livekit.TrackID) {
|
||||||
|
m.lock.Lock()
|
||||||
|
if _, ok := m.pendingReconcile[trackID]; ok {
|
||||||
|
// already reconciled
|
||||||
|
m.lock.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.lock.Unlock()
|
||||||
select {
|
select {
|
||||||
case m.reconcileCh <- trackID:
|
case m.reconcileCh <- trackID:
|
||||||
default:
|
default:
|
||||||
@@ -369,9 +379,10 @@ func (m *SubscriptionManager) reconcileWorker() {
|
|||||||
case <-reconcileTicker.C:
|
case <-reconcileTicker.C:
|
||||||
m.reconcileSubscriptions()
|
m.reconcileSubscriptions()
|
||||||
case trackID := <-m.reconcileCh:
|
case trackID := <-m.reconcileCh:
|
||||||
m.lock.RLock()
|
m.lock.Lock()
|
||||||
s := m.subscriptions[trackID]
|
s := m.subscriptions[trackID]
|
||||||
m.lock.RUnlock()
|
delete(m.pendingReconcile, trackID)
|
||||||
|
m.lock.Unlock()
|
||||||
if s != nil {
|
if s != nil {
|
||||||
m.reconcileSubscription(s)
|
m.reconcileSubscription(s)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func TestSubscribe(t *testing.T) {
|
|||||||
sm.params.OnTrackSubscribed = func(subTrack types.SubscribedTrack) {
|
sm.params.OnTrackSubscribed = func(subTrack types.SubscribedTrack) {
|
||||||
subCount.Add(1)
|
subCount.Add(1)
|
||||||
}
|
}
|
||||||
sm.params.OnSubcriptionError = func(trackID livekit.TrackID) {
|
sm.params.OnSubscriptionError = func(trackID livekit.TrackID) {
|
||||||
failed.Store(true)
|
failed.Store(true)
|
||||||
}
|
}
|
||||||
numParticipantSubscribed := atomic.Int32{}
|
numParticipantSubscribed := atomic.Int32{}
|
||||||
@@ -115,7 +115,7 @@ func TestSubscribe(t *testing.T) {
|
|||||||
resolver := newTestResolver(false, true, "pub", "pubID")
|
resolver := newTestResolver(false, true, "pub", "pubID")
|
||||||
sm.params.TrackResolver = resolver.Resolve
|
sm.params.TrackResolver = resolver.Resolve
|
||||||
failed := atomic.Bool{}
|
failed := atomic.Bool{}
|
||||||
sm.params.OnSubcriptionError = func(trackID livekit.TrackID) {
|
sm.params.OnSubscriptionError = func(trackID livekit.TrackID) {
|
||||||
failed.Store(true)
|
failed.Store(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ func TestSubscribe(t *testing.T) {
|
|||||||
resolver := newTestResolver(true, true, "pub", "pubID")
|
resolver := newTestResolver(true, true, "pub", "pubID")
|
||||||
sm.params.TrackResolver = resolver.Resolve
|
sm.params.TrackResolver = resolver.Resolve
|
||||||
failed := atomic.Bool{}
|
failed := atomic.Bool{}
|
||||||
sm.params.OnSubcriptionError = func(trackID livekit.TrackID) {
|
sm.params.OnSubscriptionError = func(trackID livekit.TrackID) {
|
||||||
failed.Store(true)
|
failed.Store(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,6 +278,7 @@ func TestSubscribeStatusChanged(t *testing.T) {
|
|||||||
|
|
||||||
require.Equal(t, int32(1), numParticipantSubscribed.Load())
|
require.Equal(t, int32(1), numParticipantSubscribed.Load())
|
||||||
require.Equal(t, int32(0), numParticipantUnsubscribed.Load())
|
require.Equal(t, int32(0), numParticipantUnsubscribed.Load())
|
||||||
|
require.True(t, sm.IsSubscribedTo("pubID"))
|
||||||
|
|
||||||
// now unsubscribe track2, no event should be fired
|
// now unsubscribe track2, no event should be fired
|
||||||
sm.UnsubscribeFromTrack("track2")
|
sm.UnsubscribeFromTrack("track2")
|
||||||
@@ -292,6 +293,7 @@ func TestSubscribeStatusChanged(t *testing.T) {
|
|||||||
return !s1.needsUnsubscribe()
|
return !s1.needsUnsubscribe()
|
||||||
}, subSettleTimeout, subCheckInterval, "track1 should be unsubscribed")
|
}, subSettleTimeout, subCheckInterval, "track1 should be unsubscribed")
|
||||||
require.Equal(t, int32(1), numParticipantUnsubscribed.Load())
|
require.Equal(t, int32(1), numParticipantUnsubscribed.Load())
|
||||||
|
require.False(t, sm.IsSubscribedTo("pubID"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// clients may send update subscribed settings prior to subscription events coming through
|
// clients may send update subscribed settings prior to subscription events coming through
|
||||||
@@ -334,7 +336,7 @@ func newTestSubscriptionManager(t *testing.T) *SubscriptionManager {
|
|||||||
Logger: logger.GetLogger(),
|
Logger: logger.GetLogger(),
|
||||||
OnTrackSubscribed: func(subTrack types.SubscribedTrack) {},
|
OnTrackSubscribed: func(subTrack types.SubscribedTrack) {},
|
||||||
OnTrackUnsubscribed: func(subTrack types.SubscribedTrack) {},
|
OnTrackUnsubscribed: func(subTrack types.SubscribedTrack) {},
|
||||||
OnSubcriptionError: func(trackID livekit.TrackID) {},
|
OnSubscriptionError: func(trackID livekit.TrackID) {},
|
||||||
TrackResolver: func(identity livekit.ParticipantIdentity, trackID livekit.TrackID) types.MediaResolverResult {
|
TrackResolver: func(identity livekit.ParticipantIdentity, trackID livekit.TrackID) types.MediaResolverResult {
|
||||||
return types.MediaResolverResult{}
|
return types.MediaResolverResult{}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user