perf: fix spin lock in consume stream call - #6
Merged
Conversation
Contributor
📝 WalkthroughWalkthroughAdds a notify channel and WaitForAppend(ctx) to the log, updates server ConsumeStream to block on WaitForAppend when offsets are out of range instead of busy-waiting, and adds tests validating wait and non-busy behavior. Changes
Sequence DiagramsequenceDiagram
participant Client
participant ConsumeStream
participant CommitLog
participant Context
Client->>ConsumeStream: Open stream / request Consume(offset)
ConsumeStream->>CommitLog: Consume(offset)
CommitLog-->>ConsumeStream: ErrOffsetOutOfRange
ConsumeStream->>CommitLog: WaitForAppend(ctx)
Note over CommitLog: Blocks waiting on notify channel
par Append occurs elsewhere
CommitLog->>CommitLog: Append(record) — close & replace notify
CommitLog-->>ConsumeStream: notify (channel closed) — wake waiter
end
ConsumeStream->>CommitLog: Consume(offset)
CommitLog-->>ConsumeStream: Record
ConsumeStream->>Client: Send record
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
Devin-Yeung
force-pushed
the
consume-stream-perf
branch
from
February 6, 2026 18:00
a0ccf87 to
58fed6e
Compare
Contributor
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@internal/server/server.go`:
- Around line 141-155: The Consume/WaitForAppend sequence can miss an append
because WaitForAppend may start waiting after the append happened; fix by
capturing the commit-log notify channel before calling s.Consume so the wait
corresponds to the checked state: add a Notify() <-chan struct{} method to the
CommitLog interface and implement it on Log (returning the l.notify channel
under lock), then in server.go call ch := s.CommitLog.Notify() before record,
err := s.Consume(...); on api.ErrOffsetOutOfRange do a select that waits on
either ctx.Done() or ch (instead of calling WaitForAppend), looping on notify
wakeups until Consume succeeds. Ensure Notify() is documented and used where
WaitForAppend previously blocked to avoid the race.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary by CodeRabbit
New Features
Bug Fixes
Tests