-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add WsAllowOutOfOrder config #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
calvwang9
wants to merge
3
commits into
v2-preview
Choose a base branch
from
feat/ws-allow-out-of-order
base: v2-preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package streams | ||
|
|
||
| const seenBufferSize = 32 | ||
|
|
||
| type Verdict int | ||
|
|
||
| const ( | ||
| Accept Verdict = iota | ||
| Duplicate | ||
| OutOfOrder | ||
| ) | ||
|
|
||
| type feedState struct { | ||
| watermark int64 | ||
| ring [seenBufferSize]int64 | ||
| set map[int64]struct{} | ||
| cursor int | ||
| count int | ||
| } | ||
|
|
||
| type FeedDeduplicator struct { | ||
| feeds map[string]*feedState | ||
| } | ||
|
|
||
| func NewFeedDeduplicator() *FeedDeduplicator { | ||
| return &FeedDeduplicator{feeds: make(map[string]*feedState)} | ||
| } | ||
|
|
||
| func (d *FeedDeduplicator) Check(feedID string, ts int64) Verdict { | ||
| fs := d.feeds[feedID] | ||
| if fs == nil { | ||
| fs = &feedState{set: make(map[int64]struct{}, seenBufferSize)} | ||
| d.feeds[feedID] = fs | ||
| } | ||
|
|
||
| if _, dup := fs.set[ts]; dup { | ||
| return Duplicate | ||
| } | ||
|
|
||
| if fs.count == seenBufferSize { | ||
| evict := fs.ring[fs.cursor] | ||
| delete(fs.set, evict) | ||
| } else { | ||
| fs.count++ | ||
| } | ||
| fs.ring[fs.cursor] = ts | ||
| fs.set[ts] = struct{}{} | ||
| fs.cursor = (fs.cursor + 1) % seenBufferSize | ||
|
|
||
| isOutOfOrder := fs.watermark > 0 && ts < fs.watermark | ||
| if ts > fs.watermark { | ||
| fs.watermark = ts | ||
| } | ||
|
|
||
| if isOutOfOrder { | ||
| return OutOfOrder | ||
| } | ||
| return Accept | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package streams | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestFeedDeduplicator_Accept(t *testing.T) { | ||
| d := NewFeedDeduplicator() | ||
| if v := d.Check("feed-1", 100); v != Accept { | ||
| t.Fatalf("expected Accept, got %d", v) | ||
| } | ||
| } | ||
|
|
||
| func TestFeedDeduplicator_Duplicate(t *testing.T) { | ||
| d := NewFeedDeduplicator() | ||
| d.Check("feed-1", 100) | ||
| if v := d.Check("feed-1", 100); v != Duplicate { | ||
| t.Fatalf("expected Duplicate, got %d", v) | ||
| } | ||
| } | ||
|
|
||
| func TestFeedDeduplicator_OutOfOrder(t *testing.T) { | ||
| d := NewFeedDeduplicator() | ||
| d.Check("feed-1", 200) | ||
| if v := d.Check("feed-1", 100); v != OutOfOrder { | ||
| t.Fatalf("expected OutOfOrder, got %d", v) | ||
| } | ||
| } | ||
|
|
||
| func TestFeedDeduplicator_OutOfOrderNotDuplicate(t *testing.T) { | ||
| d := NewFeedDeduplicator() | ||
| d.Check("feed-1", 200) | ||
| v := d.Check("feed-1", 100) | ||
| if v != OutOfOrder { | ||
| t.Fatalf("expected OutOfOrder for first OOO delivery, got %d", v) | ||
| } | ||
| if v := d.Check("feed-1", 100); v != Duplicate { | ||
| t.Fatalf("expected Duplicate for second OOO delivery, got %d", v) | ||
| } | ||
| } | ||
|
|
||
| func TestFeedDeduplicator_FIFOEviction(t *testing.T) { | ||
| d := NewFeedDeduplicator() | ||
| for i := int64(1); i <= seenBufferSize; i++ { | ||
| d.Check("feed-1", i) | ||
| } | ||
| d.Check("feed-1", 33) | ||
| // ts=2 (second inserted) should still be in the buffer | ||
| if v := d.Check("feed-1", 2); v != Duplicate { | ||
| t.Fatalf("expected ts=2 still in buffer, got %d", v) | ||
| } | ||
| // ts=1 (first inserted) was evicted by ts=33 | ||
| if v := d.Check("feed-1", 1); v == Duplicate { | ||
| t.Fatal("expected ts=1 to be evicted (FIFO), but got Duplicate") | ||
| } | ||
| } | ||
|
|
||
| func TestFeedDeduplicator_FIFOEvictsOldestInsertedNotSmallest(t *testing.T) { | ||
| d := NewFeedDeduplicator() | ||
| // Insert out of order: 100, 1, 2, 3, ..., 31 (total 32 entries) | ||
| d.Check("feed-1", 100) | ||
| for i := int64(1); i <= seenBufferSize-1; i++ { | ||
| d.Check("feed-1", i) | ||
| } | ||
| // Buffer is full. ts=100 was inserted first (oldest by insertion). | ||
| // Adding ts=999 should evict ts=100, NOT ts=1 (the smallest value). | ||
| d.Check("feed-1", 999) | ||
| // ts=1 should still be present (smallest value, but NOT oldest inserted) | ||
| if v := d.Check("feed-1", 1); v != Duplicate { | ||
| t.Fatalf("expected ts=1 (smallest value, but not oldest inserted) to remain, got %d", v) | ||
| } | ||
| // ts=100 should have been evicted (oldest inserted) | ||
| if v := d.Check("feed-1", 100); v == Duplicate { | ||
| t.Fatal("expected ts=100 (oldest inserted) to be evicted, but got Duplicate") | ||
| } | ||
| } | ||
|
|
||
| func TestFeedDeduplicator_IndependentFeeds(t *testing.T) { | ||
| d := NewFeedDeduplicator() | ||
| d.Check("feed-a", 100) | ||
| d.Check("feed-b", 100) | ||
|
|
||
| if v := d.Check("feed-a", 100); v != Duplicate { | ||
| t.Fatalf("expected Duplicate for feed-a, got %d", v) | ||
| } | ||
| if v := d.Check("feed-b", 100); v != Duplicate { | ||
| t.Fatalf("expected Duplicate for feed-b, got %d", v) | ||
| } | ||
| // Different feed, same ts is not a duplicate | ||
| if v := d.Check("feed-c", 100); v != Accept { | ||
| t.Fatalf("expected Accept for new feed-c, got %d", v) | ||
| } | ||
| } | ||
|
|
||
| func TestFeedDeduplicator_WatermarkZeroNotOutOfOrder(t *testing.T) { | ||
| d := NewFeedDeduplicator() | ||
| // First report at ts=0 should be Accept, not OutOfOrder | ||
| if v := d.Check("feed-1", 0); v != Accept { | ||
| t.Fatalf("expected Accept for first report at ts=0, got %d", v) | ||
| } | ||
| } | ||
|
|
||
| func TestFeedDeduplicator_HADuplicateAfterWatermarkAdvance(t *testing.T) { | ||
| d := NewFeedDeduplicator() | ||
| d.Check("feed-1", 100) // Accept | ||
| d.Check("feed-1", 200) // Accept, watermark -> 200 | ||
| // HA duplicate of ts=100 arrives from second connection | ||
| if v := d.Check("feed-1", 100); v != Duplicate { | ||
| t.Fatalf("expected Duplicate for HA retransmit, got %d", v) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice touch!