Skip to content
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

x-retry-attempt to StreamClientInterceptor #733

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions interceptors/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ func StreamClientInterceptor(optFuncs ...CallOption) grpc.StreamClientIntercepto
callOpts.onRetryCallback(parentCtx, attempt, lastErr)
}
var newStreamer grpc.ClientStream
newStreamer, lastErr = streamer(parentCtx, desc, cc, method, grpcOpts...)
newStreamer, lastErr = streamer(perStreamContext(parentCtx, callOpts, attempt), desc, cc, method, grpcOpts...)
if lastErr == nil {
retryingStreamer := &serverStreamingRetryingStream{
ClientStream: newStreamer,
callOpts: callOpts,
parentCtx: parentCtx,
streamerCall: func(ctx context.Context) (grpc.ClientStream, error) {
return streamer(ctx, desc, cc, method, grpcOpts...)
attempt++
return streamer(perStreamContext(ctx, callOpts, attempt), desc, cc, method, grpcOpts...)
},
}
return retryingStreamer, nil
Expand Down Expand Up @@ -296,6 +297,15 @@ func perCallContext(parentCtx context.Context, callOpts *options, attempt uint)
return ctx, cancel
}

func perStreamContext(parentCtx context.Context, callOpts *options, attempt uint) context.Context {
ctx := parentCtx
if attempt > 0 && callOpts.includeHeader {
mdClone := metadata.ExtractOutgoing(ctx).Clone().Set(AttemptMetadataKey, fmt.Sprintf("%d", attempt))
ctx = mdClone.ToOutgoing(ctx)
}
return ctx
}

func contextErrToGrpcErr(err error) error {
switch err {
case context.DeadlineExceeded:
Expand Down
Loading