-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackoff.go
More file actions
75 lines (69 loc) · 2.2 KB
/
backoff.go
File metadata and controls
75 lines (69 loc) · 2.2 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package tcppool
import "github.com/meliadamian17/tcppool/internal/backoff"
// NewExponentialBackoff creates a new exponential backoff strategy.
// The delay between retries doubles with each attempt until reaching the maximum delay.
//
// Parameters:
// - baseDelay: The initial delay in seconds for the first retry.
// - maxDelay: The maximum delay in seconds for retries.
//
// Returns:
// - A backoff.Backoff implementation using exponential backoff.
func NewExponentialBackoff(baseDelay, maxDelay uint) backoff.Backoff {
return &backoff.ExponentialBackoff{
Base: baseDelay,
MaxDelay: maxDelay,
}
}
// NewFibonacciBackoff creates a new Fibonacci backoff strategy.
// The delay between retries follows the Fibonacci sequence until reaching the maximum delay.
//
// Parameters:
// - maxDelay: The maximum delay in seconds for retries.
//
// Returns:
// - A backoff.Backoff implementation using Fibonacci backoff.
func NewFibonacciBackoff(maxDelay uint) backoff.Backoff {
return &backoff.FibonacciBackoff{
MaxDelay: maxDelay,
}
}
// NewFixedBackoff creates a new fixed backoff strategy.
// The delay between retries remains constant.
//
// Parameters:
// - interval: The fixed delay in seconds between retries.
//
// Returns:
// - A backoff.Backoff implementation using fixed backoff.
func NewFixedBackoff(interval uint) backoff.Backoff {
return &backoff.FixedBackoff{
Interval: interval,
}
}
// NewLinearBackoff creates a new linear backoff strategy.
// The delay between retries increases linearly with each attempt.
//
// Parameters:
// - scalar: The constant value added to the delay for each retry.
//
// Returns:
// - A backoff.Backoff implementation using linear backoff.
func NewLinearBackoff(scalar uint) backoff.Backoff {
return &backoff.LinearBackoff{
Scalar: scalar,
}
}
// NewPolynomialBackoff creates a new polynomial backoff strategy.
// The delay between retries follows a polynomial growth pattern.
//
// Parameters:
// - exponent: The exponent used for calculating delay growth.
//
// Returns:
// - A backoff.Backoff implementation using polynomial backoff.
func NewPolynomialBackoff(exponent uint) backoff.Backoff {
return &backoff.PolynomialBackoff{
Exponent: exponent,
}
}