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

add idempotency key #660

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions interceptors/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"sync"
"time"

"github.com/google/uuid"

Check failure on line 13 in interceptors/retry/retry.go

View workflow job for this annotation

GitHub Actions / Unit tests on Go 1.19.x / ubuntu-latest

missing go.sum entry for module providing package github.com/google/uuid (imported by github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry); to add:

Check failure on line 13 in interceptors/retry/retry.go

View workflow job for this annotation

GitHub Actions / Unit tests on Go 1.19.x / macos-latest

missing go.sum entry for module providing package github.com/google/uuid (imported by github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry); to add:

Check failure on line 13 in interceptors/retry/retry.go

View workflow job for this annotation

GitHub Actions / Unit tests on Go 1.20.x / ubuntu-latest

missing go.sum entry for module providing package github.com/google/uuid (imported by github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry); to add:

Check failure on line 13 in interceptors/retry/retry.go

View workflow job for this annotation

GitHub Actions / Unit tests on Go 1.20.x / macos-latest

missing go.sum entry for module providing package github.com/google/uuid (imported by github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry); to add:
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"golang.org/x/net/trace"
"google.golang.org/grpc"
Expand All @@ -20,6 +21,7 @@

const (
AttemptMetadataKey = "x-retry-attempt"
IdempotencyKey = "x-retry-idempotency-key"
)

// UnaryClientInterceptor returns a new retrying unary client interceptor.
Expand All @@ -36,11 +38,12 @@
return invoker(parentCtx, method, req, reply, cc, grpcOpts...)
}
var lastErr error
idempotencyKey := uuid.NewString()
for attempt := uint(0); attempt < callOpts.max; attempt++ {
if err := waitRetryBackoff(attempt, parentCtx, callOpts); err != nil {
return err
}
callCtx, cancel := perCallContext(parentCtx, callOpts, attempt)
callCtx, cancel := perCallContext(parentCtx, callOpts, attempt, idempotencyKey)
defer cancel() // Clean up potential resources.
lastErr = invoker(callCtx, method, req, reply, cc, grpcOpts...)
// TODO(mwitkow): Maybe dial and transport errors should be retriable?
Expand Down Expand Up @@ -285,15 +288,18 @@
return code == codes.DeadlineExceeded || code == codes.Canceled
}

func perCallContext(parentCtx context.Context, callOpts *options, attempt uint) (context.Context, context.CancelFunc) {
func perCallContext(parentCtx context.Context, callOpts *options, attempt uint, idempotencyKey string) (context.Context, context.CancelFunc) {
cancel := context.CancelFunc(func() {})

ctx := parentCtx
if callOpts.perCallTimeout != 0 {
ctx, cancel = context.WithTimeout(ctx, callOpts.perCallTimeout)
}
if attempt > 0 && callOpts.includeHeader {
mdClone := metadata.ExtractOutgoing(ctx).Clone().Set(AttemptMetadataKey, fmt.Sprintf("%d", attempt))
if callOpts.includeHeader {
mdClone := metadata.ExtractOutgoing(ctx).Clone().Set(IdempotencyKey, idempotencyKey)
if attempt > 0 {
mdClone.Set(AttemptMetadataKey, fmt.Sprintf("%d", attempt))
}
ctx = mdClone.ToOutgoing(ctx)
}
return ctx, cancel
Expand Down