diff options
| author | 2023-05-09 19:19:48 +0200 | |
|---|---|---|
| committer | 2023-05-09 18:19:48 +0100 | |
| commit | 6392e00653d3b81062ef60d8ae2fa2621873533f (patch) | |
| tree | 761d0ff445c2c6a85020cecdc58f92ae1cf78513 /vendor/github.com/cenkalti/backoff/v4/context.go | |
| parent | [bugfix] Don't try to get user when serializing local instance account (#1757) (diff) | |
| download | gotosocial-6392e00653d3b81062ef60d8ae2fa2621873533f.tar.xz | |
feat: initial tracing support (#1623)
Diffstat (limited to 'vendor/github.com/cenkalti/backoff/v4/context.go')
| -rw-r--r-- | vendor/github.com/cenkalti/backoff/v4/context.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/vendor/github.com/cenkalti/backoff/v4/context.go b/vendor/github.com/cenkalti/backoff/v4/context.go new file mode 100644 index 000000000..48482330e --- /dev/null +++ b/vendor/github.com/cenkalti/backoff/v4/context.go @@ -0,0 +1,62 @@ +package backoff + +import ( + "context" + "time" +) + +// BackOffContext is a backoff policy that stops retrying after the context +// is canceled. +type BackOffContext interface { // nolint: golint + BackOff + Context() context.Context +} + +type backOffContext struct { + BackOff + ctx context.Context +} + +// WithContext returns a BackOffContext with context ctx +// +// ctx must not be nil +func WithContext(b BackOff, ctx context.Context) BackOffContext { // nolint: golint + 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 getContext(b BackOff) context.Context { + if cb, ok := b.(BackOffContext); ok { + return cb.Context() + } + if tb, ok := b.(*backOffTries); ok { + return getContext(tb.delegate) + } + return context.Background() +} + +func (b *backOffContext) Context() context.Context { + return b.ctx +} + +func (b *backOffContext) NextBackOff() time.Duration { + select { + case <-b.ctx.Done(): + return Stop + default: + return b.BackOff.NextBackOff() + } +} |
