Skip to content

Commit 9b0cfc8

Browse files
committed
Add Backoff.ErrCause() instead
Signed-off-by: Marco Pracucci <marco@pracucci.com>
1 parent 88aff0f commit 9b0cfc8

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
* [CHANGE] Removed unused `time.Duration` parameter from `ShouldLog()` function in `middleware.OptionalLogging` interface. #513
7777
* [CHANGE] Changed `ShouldLog()` function signature in `middleware.OptionalLogging` interface to `ShouldLog(context.Context) (bool, string)`: the returned `string` contains an optional reason. When reason is valued, `GRPCServerLog` adds `(<reason>)` suffix to the error. #514
7878
* [CHANGE] Cache: Remove superfluous `cache.RemoteCacheClient` interface and unify all caches using the `cache.Cache` interface. #520
79-
* [CHANGE] Backoff: `Backoff.Err()` now returns the context cancellation cause when provided to the context passed to `Backoff`. #538
8079
* [FEATURE] Cache: Add support for configuring a Redis cache backend. #268 #271 #276
8180
* [FEATURE] Add support for waiting on the rate limiter using the new `WaitN` method. #279
8281
* [FEATURE] Add `log.BufferedLogger` type. #338
@@ -214,6 +213,7 @@
214213
* [ENHANCEMENT] SpanProfiler: do less work on unsampled traces. #528
215214
* [ENHANCEMENT] Log Middleware: if the trace is not sampled, log its ID as `trace_id_unsampled` instead of `trace_id`. #529
216215
* [EHNANCEMENT] httpgrpc: httpgrpc Server can now use error message from special HTTP header when converting HTTP response to an error. This is useful when HTTP response body contains binary data that doesn't form valid utf-8 string, otherwise grpc would fail to marshal returned error. #531
216+
* [CHANGE] Backoff: added `Backoff.ErrCause()` which is like `Backoff.Err()` but returns the context cause if backoff is terminated because the context has been canceled. #538
217217
* [BUGFIX] spanlogger: Support multiple tenant IDs. #59
218218
* [BUGFIX] Memberlist: fixed corrupted packets when sending compound messages with more than 255 messages or messages bigger than 64KB. #85
219219
* [BUGFIX] Ring: `ring_member_ownership_percent` and `ring_tokens_owned` metrics are not updated on scale down. #109

backoff/backoff.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,25 @@ func (b *Backoff) Ongoing() bool {
5555
}
5656

5757
// Err returns the reason for terminating the backoff, or nil if it didn't terminate.
58-
// If backoff is terminated because the context has been canceled, then this function
59-
// returns the context cancellation cause.
6058
func (b *Backoff) Err() error {
6159
if b.ctx.Err() != nil {
62-
return context.Cause(b.ctx)
60+
return b.ctx.Err()
6361
}
6462
if b.cfg.MaxRetries != 0 && b.numRetries >= b.cfg.MaxRetries {
6563
return fmt.Errorf("terminated after %d retries", b.numRetries)
6664
}
6765
return nil
6866
}
6967

68+
// ErrCause is like Err() but returns the context cause if backoff is terminated because the
69+
// context has been canceled.
70+
func (b *Backoff) ErrCause() error {
71+
if b.ctx.Err() != nil {
72+
return context.Cause(b.ctx)
73+
}
74+
return b.Err()
75+
}
76+
7077
// NumRetries returns the number of retries so far
7178
func (b *Backoff) NumRetries() int {
7279
return b.numRetries

backoff/backoff_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,44 +109,49 @@ func TestBackoff_Err(t *testing.T) {
109109
cause := errors.New("my cause")
110110

111111
tests := map[string]struct {
112-
ctx func(*testing.T) context.Context
113-
expectedErr error
112+
ctx func(*testing.T) context.Context
113+
expectedErr error
114+
expectedErrCause error
114115
}{
115-
"should return context.DeadlineExceeded when context deadline exceeded without cause": {
116+
"context deadline exceeded without cause": {
116117
ctx: func(t *testing.T) context.Context {
117118
ctx, cancel := context.WithDeadline(context.Background(), time.Now())
118119
t.Cleanup(cancel)
119120

120121
return ctx
121122
},
122-
expectedErr: context.DeadlineExceeded,
123+
expectedErr: context.DeadlineExceeded,
124+
expectedErrCause: context.DeadlineExceeded,
123125
},
124-
"should return cause when context deadline exceeded with cause": {
126+
"context deadline exceeded with cause": {
125127
ctx: func(t *testing.T) context.Context {
126128
ctx, cancel := context.WithDeadlineCause(context.Background(), time.Now(), cause)
127129
t.Cleanup(cancel)
128130

129131
return ctx
130132
},
131-
expectedErr: cause,
133+
expectedErr: context.DeadlineExceeded,
134+
expectedErrCause: cause,
132135
},
133-
"should return context.Canceled when context is canceled without cause": {
136+
"context is canceled without cause": {
134137
ctx: func(_ *testing.T) context.Context {
135138
ctx, cancel := context.WithCancel(context.Background())
136139
cancel()
137140

138141
return ctx
139142
},
140-
expectedErr: context.Canceled,
143+
expectedErr: context.Canceled,
144+
expectedErrCause: context.Canceled,
141145
},
142-
"should return cause when context is canceled with cause": {
146+
"context is canceled with cause": {
143147
ctx: func(_ *testing.T) context.Context {
144148
ctx, cancel := context.WithCancelCause(context.Background())
145149
cancel(cause)
146150

147151
return ctx
148152
},
149-
expectedErr: cause,
153+
expectedErr: context.Canceled,
154+
expectedErrCause: cause,
150155
},
151156
}
152157

@@ -160,6 +165,7 @@ func TestBackoff_Err(t *testing.T) {
160165
}, time.Second, 10*time.Millisecond)
161166

162167
require.Equal(t, testData.expectedErr, b.Err())
168+
require.Equal(t, testData.expectedErrCause, b.ErrCause())
163169
})
164170
}
165171
}

0 commit comments

Comments
 (0)