Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 4 additions & 6 deletions pkg/middleware/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ func NewRPCRetryInterceptor(opt RetryOptions) psrpc.ClientRPCInterceptor {
err = retry(opt, ctx.Done(), func(timeout time.Duration) error {
nextOpts := opts
if timeout > 0 {
nextOpts = make([]psrpc.RequestOption, len(opts)+1)
copy(nextOpts, opts)
nextOpts[len(opts)] = psrpc.WithRequestTimeout(timeout)
// options apply in order, so a timeout from the caller wins over the attempt timeout
nextOpts = append([]psrpc.RequestOption{psrpc.WithRequestTimeout(timeout)}, opts...)
}

res, err = next(ctx, req, nextOpts...)
Expand Down Expand Up @@ -141,9 +140,8 @@ func (s *streamRetryInterceptor) Send(msg proto.Message, opts ...psrpc.StreamOpt
return retry(s.opt, nil, func(timeout time.Duration) error {
nextOpts := opts
if timeout > 0 {
nextOpts = make([]psrpc.StreamOption, len(opts)+1)
copy(nextOpts, opts)
nextOpts[len(opts)] = psrpc.WithTimeout(timeout)
// options apply in order, so a timeout from the caller wins over the attempt timeout
nextOpts = append([]psrpc.StreamOption{psrpc.WithTimeout(timeout)}, opts...)
}

return s.StreamHandler.Send(msg, nextOpts...)
Expand Down
14 changes: 14 additions & 0 deletions pkg/middleware/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ func TestRetryBackoff(t *testing.T) {
require.Equal(t, ro.Timeout, timeouts[0])
})

t.Run("TestCallerTimeoutKept", func(t *testing.T) {
ro.IsRecoverable = func(err error) bool { return true }
ri := NewRPCRetryInterceptor(ro)

errs := make([]error, 3)
for i := range errs {
errs[i] = errors.New("test error")
}
h := ri(psrpc.RPCInfo{}, getClientRpcHandler(errs))
h(context.Background(), nil, psrpc.WithRequestTimeout(time.Second))

require.Equal(t, []time.Duration{time.Second, time.Second, time.Second}, timeouts)
})

t.Run("TestCustomParameters", func(t *testing.T) {
lastTry := time.Now()

Expand Down
Loading