forked from storj/drpc
-
Notifications
You must be signed in to change notification settings - Fork 7
drpcstream: gate data-frame sends on the per-stream send window #71
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
suj-krishnan
wants to merge
1
commit into
main
Choose a base branch
from
sujatha/flow-control-send-gate
base: main
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.
+140
−0
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Copyright (C) 2026 Cockroach Labs. | ||
| // See LICENSE for copying information. | ||
|
|
||
| package drpcstream | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/zeebo/assert" | ||
| "github.com/zeebo/errs" | ||
|
|
||
| "storj.io/drpc/drpcwire" | ||
| ) | ||
|
|
||
| // newGateStream builds a stream writing to io.Discard with an explicit | ||
| // SplitSize so small payloads are a single frame. | ||
| func newGateStream(t *testing.T) *Stream { | ||
| mw := testMuxWriter(t) | ||
| return NewWithOptions(context.Background(), 1, mw, NewBufferPool(), Options{SplitSize: 64 << 10}) | ||
| } | ||
|
|
||
| // By default no send window is installed, so data writes are ungated | ||
| // (unlimited) and behavior is unchanged. | ||
| func TestStream_SendWindowDefaultUngated(t *testing.T) { | ||
| st := newGateStream(t) | ||
| assert.That(t, st.sendw == nil) | ||
| assert.NoError(t, st.RawWrite(drpcwire.KindMessage, []byte("hello"))) | ||
| } | ||
|
|
||
| // With a finite send window, a data write blocks until enough credit is | ||
| // granted. | ||
| func TestStream_SendWindowGatesDataWrite(t *testing.T) { | ||
| st := newGateStream(t) | ||
| st.sendw = newSendWindow(4) // 4 bytes of credit | ||
|
|
||
| done := make(chan error, 1) | ||
| go func() { done <- st.RawWrite(drpcwire.KindMessage, []byte("hello")) }() // 5 bytes > 4 | ||
|
|
||
| select { | ||
| case <-done: | ||
| t.Fatal("data write returned before sufficient credit") | ||
| case <-time.After(blockShort): | ||
| } | ||
|
|
||
| st.sendw.grant(1) // 4 + 1 = 5 >= 5 | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| assert.NoError(t, err) | ||
| case <-time.After(time.Second): | ||
| t.Fatal("data write did not complete after grant") | ||
| } | ||
| } | ||
|
|
||
| // Control kinds (here, invoke) are not flow-controlled: they proceed even with | ||
| // zero send credit. | ||
| func TestStream_SendWindowControlKindsBypassGate(t *testing.T) { | ||
| st := newGateStream(t) | ||
| st.sendw = newSendWindow(0) // no credit at all | ||
|
|
||
| assert.NoError(t, st.WriteInvoke("service.Method", nil)) | ||
| } | ||
|
|
||
| // SendCancel preempts a send parked on credit: it terminates (closing the | ||
| // window) before taking the write lock, so the parked write wakes, releases the | ||
| // lock, and the cancel frame goes out. | ||
| func TestStream_SendWindowSendCancelPreemptsParkedWrite(t *testing.T) { | ||
| st := newGateStream(t) | ||
| st.sendw = newSendWindow(0) // send will park immediately | ||
|
|
||
| done := make(chan error, 1) | ||
| go func() { done <- st.RawWrite(drpcwire.KindMessage, []byte("data")) }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| t.Fatal("data write returned before cancel") | ||
| case <-time.After(blockShort): | ||
| } | ||
|
|
||
| assert.NoError(t, st.SendCancel(context.Canceled)) | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| // Same error as a send parked in WriteFrame or a later send would see. | ||
| assert.That(t, errors.Is(err, io.EOF)) | ||
| case <-time.After(time.Second): | ||
| t.Fatal("parked data write was not preempted by SendCancel") | ||
| } | ||
|
|
||
| // A subsequent send observes the same error as the parked one. | ||
| assert.That(t, errors.Is(st.RawWrite(drpcwire.KindMessage, []byte("more")), io.EOF)) | ||
| } | ||
|
|
||
| // Terminating the stream wakes a send parked on credit. | ||
| func TestStream_SendWindowTerminateWakesParkedWrite(t *testing.T) { | ||
| st := newGateStream(t) | ||
| st.sendw = newSendWindow(0) // send will park immediately | ||
|
|
||
| done := make(chan error, 1) | ||
| go func() { done <- st.RawWrite(drpcwire.KindMessage, []byte("data")) }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| t.Fatal("data write returned before termination") | ||
| case <-time.After(blockShort): | ||
| } | ||
|
|
||
| st.Cancel(errs.New("boom")) | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| // Cancel pre-sets sigs.send to io.EOF; the window closes with it. | ||
| assert.That(t, errors.Is(err, io.EOF)) | ||
| case <-time.After(time.Second): | ||
| t.Fatal("parked data write did not wake on termination") | ||
| } | ||
| } |
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.
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.
s.Context()is useless here.closedoes the trick of aborting. I would recommend just keep thenand remove the context.