Fix handling of non-monotonic timestamps (#1304)

* Fix handling of non-monotonic timestamps

Timed version is inspired by Hybrid Clock. We used to have a mixed behavior
by using time.Time:
* during local comparisons, it does increment monotonically
* when deserializing remote timestamps, we lose that attribute

So it's possible for two requests to be sent in the same microsecond, and
for the latter one to be dropped.

To fix that behavior, I'm switching it to keeping timestamps to consolidate
that behavior, and accepting multiple updates in the same ms by incrementing ticks.

Also using @paulwe's idea of a version generator.
This commit is contained in:
David Zhao
2023-01-12 11:57:26 -08:00
committed by GitHub
parent a052ebd644
commit 17236799bb
7 changed files with 74 additions and 28 deletions
+13 -6
View File
@@ -3,7 +3,6 @@ package rtc
import (
"errors"
"sync"
"time"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
@@ -17,10 +16,12 @@ var (
)
type UpTrackManagerParams struct {
SID livekit.ParticipantID
Logger logger.Logger
SID livekit.ParticipantID
Logger logger.Logger
VersionGenerator utils.TimedVersionGenerator
}
// UpTrackManager manages all uptracks from a participant
type UpTrackManager struct {
params UpTrackManagerParams
@@ -203,6 +204,10 @@ func (u *UpTrackManager) UpdateSubscriptionPermission(
) error {
u.lock.Lock()
if timedVersion != nil {
// it's possible for permission updates to come from another node. In that case
// they would be the authority for this participant's permissions
// we do not want to initialize subscriptionPermissionVersion too early since if another machine is the
// owner for the data, we'd prefer to use their TimedVersion
if u.subscriptionPermissionVersion != nil {
tv := utils.NewTimedVersionFromProto(timedVersion)
// ignore older version
@@ -221,16 +226,18 @@ func (u *UpTrackManager) UpdateSubscriptionPermission(
u.lock.Unlock()
return nil
}
u.subscriptionPermissionVersion.Update(time.UnixMicro(timedVersion.UnixMicro))
u.subscriptionPermissionVersion.Update(tv)
} else {
u.subscriptionPermissionVersion = utils.NewTimedVersionFromProto(timedVersion)
}
} else {
// for requests coming from the current node, use local versions
tv := u.params.VersionGenerator.New()
// use current time as the new/updated version
if u.subscriptionPermissionVersion == nil {
u.subscriptionPermissionVersion = utils.NewTimedVersion(time.Now(), 0)
u.subscriptionPermissionVersion = tv
} else {
u.subscriptionPermissionVersion.Update(time.Now())
u.subscriptionPermissionVersion.Update(tv)
}
}