Repository navigation
thriftbp: interrupt in-flight calls when their context is canceled #720
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
rokob
wants to merge
1
commit into
master
Choose a base branch
from
aw-thrift-timeout
base: master
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
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,162 @@ | ||
| package thriftbp | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/apache/thrift/lib/go/thrift" | ||
|
|
||
| "github.com/reddit/baseplate.go/clientpool" | ||
| ) | ||
|
|
||
| type cancellationTestClient struct { | ||
| open atomic.Bool | ||
|
|
||
| callStarted chan struct{} | ||
| callReturned chan struct{} | ||
| callUnblocked chan struct{} | ||
| closeStarted chan struct{} | ||
| allowClose chan struct{} | ||
|
|
||
| closeCalls atomic.Int32 | ||
| } | ||
|
|
||
| func newCancellationTestClient() *cancellationTestClient { | ||
| c := &cancellationTestClient{ | ||
| callStarted: make(chan struct{}), | ||
| callReturned: make(chan struct{}), | ||
| callUnblocked: make(chan struct{}), | ||
| closeStarted: make(chan struct{}), | ||
| allowClose: make(chan struct{}), | ||
| } | ||
| c.open.Store(true) | ||
| return c | ||
| } | ||
|
|
||
| func (c *cancellationTestClient) Call( | ||
| context.Context, | ||
| string, | ||
| thrift.TStruct, | ||
| thrift.TStruct, | ||
| ) (thrift.ResponseMeta, error) { | ||
| close(c.callStarted) | ||
| <-c.callUnblocked | ||
| close(c.callReturned) | ||
| return thrift.ResponseMeta{}, errors.New("connection closed") | ||
| } | ||
|
|
||
| func (c *cancellationTestClient) Close() error { | ||
| c.closeCalls.Add(1) | ||
| c.open.Store(false) | ||
| close(c.closeStarted) | ||
| close(c.callUnblocked) | ||
| <-c.allowClose | ||
| return nil | ||
| } | ||
|
|
||
| func (c *cancellationTestClient) IsOpen() bool { | ||
| return c.open.Load() | ||
| } | ||
|
|
||
| type cancellationTestPool struct { | ||
| client Client | ||
| released chan bool | ||
| discarded chan bool | ||
| } | ||
|
|
||
| func (p *cancellationTestPool) Get() (clientpool.Client, error) { | ||
| return p.client, nil | ||
| } | ||
|
|
||
| func (p *cancellationTestPool) Release(c clientpool.Client) error { | ||
| p.released <- c.IsOpen() | ||
| return nil | ||
| } | ||
|
|
||
| func (p *cancellationTestPool) Discard(c clientpool.Client) error { | ||
| p.discarded <- c.IsOpen() | ||
| return nil | ||
| } | ||
|
|
||
| func (*cancellationTestPool) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (*cancellationTestPool) NumActiveClients() int32 { | ||
| return 0 | ||
| } | ||
|
|
||
| func (*cancellationTestPool) NumAllocated() int32 { | ||
| return 1 | ||
| } | ||
|
|
||
| func (*cancellationTestPool) IsExhausted() bool { | ||
| return false | ||
| } | ||
|
|
||
| func TestPooledCallCancellationClosesAndDiscardsClient(t *testing.T) { | ||
| client := newCancellationTestClient() | ||
| pool := &cancellationTestPool{ | ||
| client: client, | ||
| released: make(chan bool, 1), | ||
| discarded: make(chan bool, 1), | ||
| } | ||
| p := &clientPool{ | ||
| Pool: pool, | ||
| slug: "cancellation-test", | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancelCause(context.Background()) | ||
| cause := errors.New("test cancellation cause") | ||
| result := make(chan error, 1) | ||
| go func() { | ||
| _, err := p.pooledCall(ctx, "method", nil, nil) | ||
| result <- err | ||
| }() | ||
|
|
||
| <-client.callStarted | ||
| cancel(cause) | ||
| <-client.closeStarted | ||
| <-client.callReturned | ||
|
|
||
| select { | ||
| case <-pool.released: | ||
| t.Fatal("client was released before the cancellation callback finished") | ||
| case err := <-result: | ||
| t.Fatalf("pooledCall returned before the cancellation callback finished: %v", err) | ||
| case <-time.After(50 * time.Millisecond): | ||
| } | ||
|
|
||
| close(client.allowClose) | ||
|
|
||
| select { | ||
| case err := <-result: | ||
| if !errors.Is(err, cause) { | ||
| t.Fatalf("pooledCall error = %v, want cancellation cause %v", err, cause) | ||
| } | ||
| case <-time.After(time.Second): | ||
| t.Fatal("pooledCall did not return after the cancellation callback finished") | ||
| } | ||
|
|
||
| select { | ||
| case open := <-pool.discarded: | ||
| if open { | ||
| t.Error("closed client was released as reusable") | ||
| } | ||
| default: | ||
| t.Fatal("client was not discarded") | ||
| } | ||
|
|
||
| select { | ||
| case <-pool.released: | ||
| t.Fatal("canceled client was released instead of discarded") | ||
| default: | ||
| } | ||
|
|
||
| if got := client.closeCalls.Load(); got != 1 { | ||
| t.Errorf("client Close calls = %d, want 1", got) | ||
| } | ||
| } |
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.
Could this cause a data race between a successful Thrift call but the context being cancelled? For example, the call succeeds, but before
pooledCallreturnsctxis cancelled. We then overrideerrwhich was nil to a non-nil error. Based on the description it sounds like we want to interrupt in-flight calls, but here it wouldn't be in-flight anymore, would it? At least it appears that this is changing the previous behavior.Maybe all this needs is to make sure that we only call this when
shouldCloseConnection(err)istrue?