Use ParticipantInfo version to ensure consistency (#399)

* Use ParticipantInfo version to ensure consistency

Deprecating time.Time and avoid locking unnecessarily

* properly adjust ulimit. update protocol

* Save initial version from params

* get rid of metadata field, use grants copy

* fix test
This commit is contained in:
David Zhao
2022-02-03 17:10:52 -08:00
committed by GitHub
parent 7bbd238188
commit 6f6d55345b
9 changed files with 59 additions and 53 deletions
+28 -13
View File
@@ -57,6 +57,7 @@ type ParticipantParams struct {
Logger logger.Logger
SimTracks map[uint32]SimulcastTrackInfo
Grants *auth.ClaimGrants
InitialVersion uint32
}
type ParticipantImpl struct {
@@ -105,6 +106,7 @@ type ParticipantImpl struct {
lock sync.RWMutex
once sync.Once
updateLock sync.Mutex
version uint32
// callbacks & handlers
onTrackPublished func(types.LocalParticipant, types.MediaTrack)
@@ -131,6 +133,7 @@ func NewParticipant(params ParticipantParams, perms *livekit.ParticipantPermissi
subscribedTracksSettings: make(map[livekit.TrackID]*livekit.UpdateTrackSettings),
disallowedSubscriptions: make(map[livekit.TrackID]livekit.ParticipantID),
connectedAt: time.Now(),
version: params.InitialVersion,
}
p.migrateState.Store(types.MigrateStateInit)
p.state.Store(livekit.ParticipantInfo_JOINING)
@@ -248,8 +251,7 @@ func (p *ParticipantImpl) ConnectedAt() time.Time {
// SetMetadata attaches metadata to the participant
func (p *ParticipantImpl) SetMetadata(metadata string) {
p.lock.Lock()
changed := p.metadata != metadata
p.metadata = metadata
changed := p.params.Grants.Metadata != metadata
p.params.Grants.Metadata = metadata
p.lock.Unlock()
@@ -293,13 +295,16 @@ func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo {
Sid: string(p.params.SID),
Identity: string(p.params.Identity),
Name: string(p.params.Name),
Metadata: p.metadata,
State: p.State(),
JoinedAt: p.ConnectedAt().Unix(),
Hidden: p.Hidden(),
Recorder: p.IsRecorder(),
Version: atomic.AddUint32(&p.version, 1),
}
info.Tracks = p.UpTrackManager.ToProto()
if p.params.Grants != nil {
info.Metadata = p.params.Grants.Metadata
}
return info
}
@@ -594,26 +599,36 @@ func (p *ParticipantImpl) SendJoinResponse(
})
}
func (p *ParticipantImpl) SendParticipantUpdate(participantsToUpdate []*livekit.ParticipantInfo, updatedAt time.Time) error {
if len(participantsToUpdate) == 1 {
p.updateLock.Lock()
defer p.updateLock.Unlock()
pi := participantsToUpdate[0]
func (p *ParticipantImpl) SendParticipantUpdate(participantsToUpdate []*livekit.ParticipantInfo) error {
p.updateLock.Lock()
validUpdates := make([]*livekit.ParticipantInfo, 0, len(participantsToUpdate))
for _, pi := range participantsToUpdate {
isValid := true
if val, ok := p.updateCache.Get(pi.Sid); ok {
if lastUpdatedAt, ok := val.(time.Time); ok {
if lastVersion, ok := val.(uint32); ok {
// this is a message delivered out of order, a more recent version of the message had already been
// sent.
if lastUpdatedAt.After(updatedAt) {
return nil
if pi.Version < lastVersion {
p.params.Logger.Debugw("skipping outdated participant update", "version", pi.Version, "lastVersion", lastVersion)
isValid = false
}
}
}
p.updateCache.Add(pi.Sid, updatedAt)
if isValid {
p.updateCache.Add(pi.Sid, pi.Version)
validUpdates = append(validUpdates, pi)
}
}
p.updateLock.Unlock()
if len(validUpdates) == 0 {
return nil
}
return p.writeMessage(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Update{
Update: &livekit.ParticipantUpdate{
Participants: participantsToUpdate,
Participants: validUpdates,
},
},
})