| 1 | // Copyright (c) The EfficientGo Authors. |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | // Initially copied from Cortex project. |
| 5 | |
| 6 | // Package backoff implements backoff timers which increases wait time on every retry, incredibly useful |
| 7 | // in distributed system timeout functionalities. |
| 8 | package backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/ |
| 9 | |
| 10 | import ( |
| 11 | "contextscip-go gomod github.com/golang/go/src go1.17 context/" |
| 12 | "fmtscip-go gomod github.com/golang/go/src go1.17 fmt/" |
| 13 | "math/randscip-go gomod github.com/golang/go/src go1.17 `math/rand`/" |
| 14 | "timescip-go gomod github.com/golang/go/src go1.17 time/" |
| 15 | ) |
| 16 | |
| 17 | // Config configures a Backoff. |
| 18 | type Configscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config# struct { |
| 19 | Minscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Min. timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# `yaml:"min_period"` // Start backoff at this level |
| 20 | Maxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Max. timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# `yaml:"max_period"` // Increase exponentially to this level |
| 21 | MaxRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#MaxRetries. int `yaml:"max_retries"` // Give up after this many; zero means infinite retries |
| 22 | } |
| 23 | |
| 24 | // Backoff implements exponential backoff with randomized wait times. |
| 25 | type Backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff# struct { |
| 26 | cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg. Configscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config# |
| 27 | ctxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#ctx. contextscip-go gomod github.com/golang/go/src go1.17 context/.Contextscip-go gomod github.com/golang/go/src go1.17 context/Context# |
| 28 | numRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#numRetries. int |
| 29 | nextDelayMinscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMin. timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# |
| 30 | nextDelayMaxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMax. timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# |
| 31 | } |
| 32 | |
| 33 | // New creates a Backoff object. Pass a Context that can also terminate the operation. |
| 34 | func Newscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/New().(ctx contextscip-go gomod github.com/golang/go/src go1.17 context/.Contextscip-go gomod github.com/golang/go/src go1.17 context/Context#, cfg Configscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#) *Backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff# { |
| 35 | return &Backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#{ |
| 36 | cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg.: cfg, |
| 37 | ctxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#ctx.: ctx, |
| 38 | nextDelayMinscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMin.: cfg.Minscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Min., |
| 39 | nextDelayMaxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMax.: doubleDurationscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/doubleDuration().(cfg.Minscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Min., cfg.Maxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Max.), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Reset the Backoff back to its initial condition. |
| 44 | func (b *Backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#) Resetscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#Reset().() { |
| 45 | b.numRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#numRetries. = 0 |
| 46 | b.nextDelayMinscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMin. = b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..Minscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Min. |
| 47 | b.nextDelayMaxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMax. = doubleDurationscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/doubleDuration().(b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..Minscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Min., b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..Maxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Max.) |
| 48 | } |
| 49 | |
| 50 | // Ongoing returns true if caller should keep going. |
| 51 | func (b *Backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#) Ongoingscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#Ongoing().() bool { |
| 52 | // Stop if Context has errored or max retry count is exceeded. |
| 53 | return b.ctxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#ctx..Errscip-go gomod github.com/golang/go/src go1.17 context/Context#Err().() == nil && (b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..MaxRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#MaxRetries. == 0 || b.numRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#numRetries. < b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..MaxRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#MaxRetries.) |
| 54 | } |
| 55 | |
| 56 | // Err returns the reason for terminating the backoff, or nil if it didn't terminate. |
| 57 | func (b *Backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#) Errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#Err().() error { |
| 58 | if b.ctxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#ctx..Errscip-go gomod github.com/golang/go/src go1.17 context/Context#Err().() != nil { |
| 59 | return b.ctxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#ctx..Errscip-go gomod github.com/golang/go/src go1.17 context/Context#Err().() |
| 60 | } |
| 61 | if b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..MaxRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#MaxRetries. != 0 && b.numRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#numRetries. >= b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..MaxRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#MaxRetries. { |
| 62 | return fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.17 fmt/Errorf().("terminated after %d retries", b.numRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#numRetries.) |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // NumRetries returns the number of retries so far. |
| 68 | func (b *Backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#) NumRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#NumRetries().() int { |
| 69 | return b.numRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#numRetries. |
| 70 | } |
| 71 | |
| 72 | // Wait sleeps for the backoff time then increases the retry count and backoff time. |
| 73 | // Returns immediately if Context is terminated. |
| 74 | func (b *Backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#) Waitscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#Wait().() { |
| 75 | // Increase the number of retries and get the next delay. |
| 76 | sleepTime := b.NextDelayscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#NextDelay().() |
| 77 | |
| 78 | if b.Ongoingscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#Ongoing().() { |
| 79 | select { |
| 80 | case <-b.ctxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#ctx..Donescip-go gomod github.com/golang/go/src go1.17 context/Context#Done().(): |
| 81 | case <-timescip-go gomod github.com/golang/go/src go1.17 time/.Afterscip-go gomod github.com/golang/go/src go1.17 time/After().(sleepTime): |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func (b *Backoffscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#) NextDelayscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#NextDelay().() timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# { |
| 87 | b.numRetriesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#numRetries.++ |
| 88 | |
| 89 | // Handle the edge case the min and max have the same value |
| 90 | // (or due to some misconfig max is < min). |
| 91 | if b.nextDelayMinscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMin. >= b.nextDelayMaxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMax. { |
| 92 | return b.nextDelayMinscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMin. |
| 93 | } |
| 94 | |
| 95 | // Add a jitter within the next exponential backoff range. |
| 96 | sleepTime := b.nextDelayMinscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMin. + timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration#(randscip-go gomod github.com/golang/go/src go1.17 `math/rand`/.Int63nscip-go gomod github.com/golang/go/src go1.17 `math/rand`/Int63n().(int64(b.nextDelayMaxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMax.-b.nextDelayMinscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMin.))) |
| 97 | |
| 98 | // Apply the exponential backoff to calculate the next jitter |
| 99 | // range, unless we've already reached the max. |
| 100 | if b.nextDelayMaxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMax. < b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..Maxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Max. { |
| 101 | b.nextDelayMinscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMin. = doubleDurationscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/doubleDuration().(b.nextDelayMinscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMin., b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..Maxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Max.) |
| 102 | b.nextDelayMaxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMax. = doubleDurationscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/doubleDuration().(b.nextDelayMaxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#nextDelayMax., b.cfgscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Backoff#cfg..Maxscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/Config#Max.) |
| 103 | } |
| 104 | |
| 105 | return sleepTime |
| 106 | } |
| 107 | |
| 108 | func doubleDurationscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/backoff`/doubleDuration().(value timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration#, max timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration#) timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# { |
| 109 | value = value * 2 |
| 110 | if value <= max { |
| 111 | return value |
| 112 | } |
| 113 | return max |
| 114 | } |