-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package backoff | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// BackOffContext is a backoff policy that stops retrying after the context | ||
// is canceled. | ||
type BackOffContext interface { | ||
BackOff | ||
Context() context.Context | ||
} | ||
|
||
type backOffContext struct { | ||
BackOff | ||
ctx context.Context | ||
} | ||
|
||
// WithContext returns BackOffContext with context ctx | ||
// | ||
// ctx must not be nil | ||
func WithContext(b BackOff, ctx context.Context) BackOffContext { | ||
if ctx == nil { | ||
panic("nil context") | ||
} | ||
|
||
if b, ok := b.(*backOffContext); ok { | ||
return &backOffContext{ | ||
BackOff: b.BackOff, | ||
ctx: ctx, | ||
} | ||
} | ||
|
||
return &backOffContext{ | ||
BackOff: b, | ||
ctx: ctx, | ||
} | ||
} | ||
|
||
func ensureContext(b BackOff) BackOffContext { | ||
if cb, ok := b.(BackOffContext); ok { | ||
return cb | ||
} | ||
return WithContext(b, context.Background()) | ||
} | ||
|
||
func (b *backOffContext) Context() context.Context { | ||
return b.ctx | ||
} | ||
|
||
func (b *backOffContext) NextBackOff() time.Duration { | ||
select { | ||
case <-b.Context().Done(): | ||
return Stop | ||
default: | ||
return b.BackOff.NextBackOff() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package backoff | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestContext(t *testing.T) { | ||
b := NewConstantBackOff(time.Millisecond) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
cb := WithContext(b, ctx) | ||
|
||
if cb.Context() != ctx { | ||
t.Error("invalid context") | ||
} | ||
|
||
cancel() | ||
|
||
if cb.NextBackOff() != Stop { | ||
t.Error("invalid next back off") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters