525 Implementation Notes - adamcfraser/cbnotes GitHub Wiki

Usages of sequence_id.Before()

We've modified the sequence_id.Before() calculation to use LowSeq instead of Seq when LowSeq is present. Validated that this is the correct usage for the existing usages of Before:

changes.go.193: Checks whether the incoming Since value used for retrieval from the channel caches is earlier than the sequence the user was added to the channel (seqAddedAt). If so, sends all of the channel to the user. If Since has LowSeq set lower than seqAddedAt, it makes sense to send the whole channel (we might not have seen the doc that added them the last time around)

changes.go.209: When deciding whether to create a new entry for the user object changing. If incoming Since is before the user sequence (i.e. when the user was changed). should be good to use LowSeq here if it's below the user sequence.

changes.go.248: Finding the current entry with the minimum sequence. Should just be comparison of sequences in the channel caches, which don't have a LowSeq set (it's only on values sent to/from the client, not in the caches)

changes.go.272: If minSeq comes back earlier than Since, skips. minSeq won't have lowSeq. If since has lowSeq, we want to use it.

MissedSequenceQueue

MissedSequenceQueue tracks late-arriving sequences that get skipped by the normal changes processing. When _addPendingLogs runs, if the MaxChannelLogPendingWaitTime is exceeded (or MaxChannelLogPendingCount), it adds the blocking sequence (c.nextSequence) to the missed queue and increments c.nextSequence.

MissedSequenceQueue doesn't need to doing sorting on input, as new entries can always be appended to the end (c.nextSequence is always increasing), so it's implemented as a slice of uint64. When a missed sequence arrives, we do a binary search in the slice to find and remove.

FoundSequenceList

When a previously skipped sequence finally arrives in the feed, we need to remove from MissedSequenceQueue and send to any connected clients. FoundSequenceList tracks these.

Considered using a listener/broadcast model to update all currently connected clients that a found sequence had arrived, but had performance concerns about this approach when we have a very large number of connected clients.

Instead, decided to maintain a single FoundSequenceList, and have