-
Notifications
You must be signed in to change notification settings - Fork 16
fix: drain entire queue per flush to avoid streaming tail lag #424
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,11 +134,10 @@ protected async Task Flush() | |
| _timeout = null; | ||
| } | ||
|
|
||
| var i = 0; | ||
|
|
||
| Queue<TypingActivity> informativeUpdates = new(); | ||
| var startCount = _queue.Count; | ||
|
|
||
| while (i <= 10 && _queue.TryDequeue(out var activity)) | ||
| while (_queue.TryDequeue(out var activity)) | ||
| { | ||
| if (activity is MessageActivity message) | ||
| { | ||
|
|
@@ -159,11 +158,10 @@ protected async Task Flush() | |
| informativeUpdates.Enqueue(typing); | ||
| } | ||
|
|
||
| i++; | ||
| _count++; | ||
| } | ||
|
|
||
| if (i == 0) return; | ||
| if (startCount == 0) return; | ||
|
||
|
|
||
| // Send informative updates | ||
| if (informativeUpdates.Count > 0) | ||
|
|
||
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.
ConcurrentQueue<T>.Countcan be O(n) and is an imprecise snapshot under concurrency. Since this value is only used to decide whether to return, it’s more efficient and accurate to avoidCountentirely and instead track a localdequeuedcounter (or a boolean flag) while draining.