/
backoff.go
32 lines (26 loc) · 1022 Bytes
/
backoff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package gue
import (
"time"
exp "github.com/vgarvardt/backoff"
)
// Backoff is the interface for backoff implementation that will be used to reschedule errored jobs to a later time.
// If the Backoff implementation returns negative duration - the job will be discarded.
type Backoff func(retries int) time.Duration
var (
// DefaultExponentialBackoff is the exponential Backoff implementation with default config applied
DefaultExponentialBackoff = NewExponentialBackoff(exp.Config{
BaseDelay: 1.0 * time.Second,
Multiplier: 1.6,
Jitter: 0.2,
MaxDelay: 1.0 * time.Hour,
})
// BackoffNever is the Backoff implementation that never returns errored job to the queue for retry,
// but discards it in case of the error.
BackoffNever = func(retries int) time.Duration {
return -1
}
)
// NewExponentialBackoff instantiates new exponential backoff implementation with config
func NewExponentialBackoff(cfg exp.Config) func(retries int) time.Duration {
return exp.Exponential{Config: cfg}.Backoff
}