-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbackoff.go
More file actions
37 lines (33 loc) · 1.23 KB
/
backoff.go
File metadata and controls
37 lines (33 loc) · 1.23 KB
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
33
34
35
36
37
package lneto
import (
"runtime"
"time"
)
// Flag return values for a BackoffStrategy.
const (
BackoffFlagGosched = time.Duration(-1)
BackoffFlagNop = time.Duration(-2)
)
// BackoffStrategy is the abstraction of a backoff strategy for retrying an operation.
// It returns the amount of time to sleep for or a flag value:
// - Returns [BackoffFlagNop]: Signal no yielding function should be called.
// Useful for when the BackoffStrategy implements its own yield.
// - Returns [BackoffFlagGosched]: Signal [runtime.Gosched] should be called.
//
// Despite the name(changes welcome) consecutiveBackoffs starts at 0 and
// increments by 1 every time the operation is retried.
// See internal/backoff.go for a implementation example.
type BackoffStrategy func(consecutiveBackoffs uint) (sleepOrFlag time.Duration)
// Do applies the backoff strategy by calling backoff(consecutiveBackoffs)
// and then the corresponding yield function for the returned value. See [BackoffStrategy].
func (backoff BackoffStrategy) Do(consecutiveBackoffs uint) {
sleep := backoff(consecutiveBackoffs)
switch sleep {
case BackoffFlagNop:
// No yield. Yield implemented by backoff.
case BackoffFlagGosched:
runtime.Gosched()
default:
time.Sleep(sleep)
}
}