-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
interop: Replace context.Background() with passed ctx #6827
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #6827 +/- ##
==========================================
+ Coverage 83.40% 83.75% +0.35%
==========================================
Files 285 287 +2
Lines 30879 30928 +49
==========================================
+ Hits 25754 25904 +150
+ Misses 4053 3963 -90
+ Partials 1072 1061 -11 |
interop/client/client.go
Outdated
@@ -267,103 +269,103 @@ func main() { | |||
tc := testgrpc.NewTestServiceClient(conn) | |||
switch *testCase { | |||
case "empty_unary": | |||
interop.DoEmptyUnaryCall(tc) | |||
interop.DoEmptyUnaryCall(tc, ctx) |
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.
Please make ctx
the first parameter according to common Go style.
interop/test_utils.go
Outdated
start := time.Now() | ||
ctx, cancel := context.WithDeadline(context.Background(), overallDeadline) | ||
ctx, cancel := context.WithDeadline(ctx, overallDeadline) |
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.
Ideally the caller would do this and not pass overallDeadline
, and check ctx.Err()
instead of time.Now.After(overallDeadline)
. If you don't mind making that change, too. Or we can do it separately if it turns out to be too much.
Thanks!
This PR is labeled as requiring an update from the reporter, and no update has been received after 6 days. If no update is provided in the next 7 days, this issue will be automatically closed. |
hi @dfawley, could you please reopen the PR? |
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.
Sorry for the delay. Mostly LGTM; just one thing (inline).
interop/test_utils.go
Outdated
@@ -724,9 +724,9 @@ func doOneSoakIteration(ctx context.Context, tc testgrpc.TestServiceClient, rese | |||
// If resetChannel is false, then each RPC will be performed on tc. Otherwise, each RPC will be performed on a new | |||
// stub that is created with the provided server address and dial options. | |||
// TODO(mohanli-ml): Create SoakTestOptions as a parameter for this method. | |||
func DoSoakTest(tc testgrpc.TestServiceClient, serverAddr string, dopts []grpc.DialOption, resetChannel bool, soakIterations int, maxFailures int, soakRequestSize int, soakResponseSize int, perIterationMaxAcceptableLatency time.Duration, minTimeBetweenRPCs time.Duration, overallDeadline time.Time) { | |||
func DoSoakTest(ctx context.Context, cancel context.CancelFunc, tc testgrpc.TestServiceClient, serverAddr string, dopts []grpc.DialOption, resetChannel bool, soakIterations int, maxFailures int, soakRequestSize int, soakResponseSize int, perIterationMaxAcceptableLatency time.Duration, minTimeBetweenRPCs time.Duration) { |
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.
You should not pass a CancelFunc
. If this function needs it, it should do ctx, cancel := context.WithCancel(ctx); defer cancel();
to avoid cancelling the parent's context.
But in this case, I don't think it needs it anyway. The defer cancel()
should be done in the caller, not by this function.
thanks @dfawley, I've pushed the change in the latest commit one query - is the use of |
Derived contexts cannot cancel their parents/siblings and cannot be cancelled via a method on the context itself (by design). The thing that creates a context should be the only thing able to cancel it. |
interop/client/client.go
Outdated
@@ -358,10 +358,12 @@ func main() { | |||
interop.DoPickFirstUnary(ctx, tc) | |||
logger.Infoln("PickFirstUnary done") | |||
case "rpc_soak": | |||
interop.DoSoakTest(ctxWithDeadline, cancel, tc, serverAddr, opts, false /* resetChannel */, *soakIterations, *soakMaxFailures, *soakRequestSize, *soakResponseSize, time.Duration(*soakPerIterationMaxAcceptableLatencyMs)*time.Millisecond, time.Duration(*soakMinTimeMsBetweenRPCs)*time.Millisecond) | |||
interop.DoSoakTest(ctxWithDeadline, tc, serverAddr, opts, false /* resetChannel */, *soakIterations, *soakMaxFailures, *soakRequestSize, *soakResponseSize, time.Duration(*soakPerIterationMaxAcceptableLatencyMs)*time.Millisecond, time.Duration(*soakMinTimeMsBetweenRPCs)*time.Millisecond) | |||
cancel() |
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.
cancel
should be called by a defer
right after the context is created (line 271).
interop/xds_federation/client.go
Outdated
@@ -122,7 +122,8 @@ func main() { | |||
go func(c clientConfig) { | |||
overallDeadline := time.Now().Add(time.Duration(*soakOverallTimeoutSeconds) * time.Second) | |||
ctxWithDeadline, cancel := context.WithDeadline(ctx, overallDeadline) |
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.
Similar: it's best to defer cancel()
here instead (nearly always).
The main time you wouldn't defer
is if the context is created inside a loop, because you don't want to stack up a very large number of defer
s.
thank you for the clarifications @dfawley! I've pushed the changes to the branch |
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.
LGTM; just a couple minor style things remain.
interop/client/client.go
Outdated
overallDeadline := time.Now().Add(time.Duration(*soakOverallTimeoutSeconds) * time.Second) | ||
ctxWithDeadline, cancel := context.WithDeadline(ctx, overallDeadline) |
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.
Sorry, I missed this before: please use the more ergonomic WithTimeout
instead:
ctxWithDeadline, cancel := context.WithTimeout(ctx, *soakOverallTimeoutSeconds * time.Second)
interop/test_utils.go
Outdated
outCtx := metadata.NewOutgoingContext(ctx, metadata.MD{"authorization": []string{kv["authorization"]}}) | ||
reply, err := tc.UnaryCall(outCtx, req) |
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.
This can just shadow or replace ctx
instead of making a new thing called outCtx
.
interop/test_utils.go
Outdated
// Testing with UnaryCall. | ||
pl := ClientNewPayload(testpb.PayloadType_COMPRESSABLE, 1) | ||
req := &testpb.SimpleRequest{ | ||
ResponseType: testpb.PayloadType_COMPRESSABLE, | ||
ResponseSize: int32(1), | ||
Payload: pl, | ||
} | ||
ctx := metadata.NewOutgoingContext(context.Background(), customMetadata) | ||
outCtx := metadata.NewOutgoingContext(ctx, customMetadata) |
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.
As above; just shadow (:=
) or overwrite (=
) ctx
.
interop/xds_federation/client.go
Outdated
for i := range clients { | ||
wg.Add(1) | ||
go func(c clientConfig) { | ||
interop.DoSoakTest(c.tc, c.uri, c.opts, resetChannel, *soakIterations, *soakMaxFailures, *soakRequestSize, *soakResponseSize, time.Duration(*soakPerIterationMaxAcceptableLatencyMs)*time.Millisecond, time.Duration(*soakMinTimeMsBetweenRPCs)*time.Millisecond, time.Now().Add(time.Duration(*soakOverallTimeoutSeconds)*time.Second)) | ||
overallDeadline := time.Now().Add(time.Duration(*soakOverallTimeoutSeconds) * time.Second) | ||
ctxWithDeadline, cancel := context.WithDeadline(ctx, overallDeadline) |
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.
As above, please use WithTimeout
instead of WithDeadline
.
This PR is labeled as requiring an update from the reporter, and no update has been received after 6 days. If no update is provided in the next 7 days, this issue will be automatically closed. |
hi @dfawley, thanks for the feedback - I'd missed the obvious use of |
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.
Thank you for the PR!
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.
LGTM! Thanks @Aditya-Sood
Fixes #6763
RELEASE NOTES: none