Skip to content

Commit 1631292

Browse files
committed
refine
1 parent 64870c0 commit 1631292

File tree

2 files changed

+24
-47
lines changed

2 files changed

+24
-47
lines changed

backoff.go

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type BackoffConfig struct {
1616
type ExponentialBackoffConfig struct {
1717
BackoffConfig
1818
Multiplier float64 // Multiplier for exponential growth
19+
JitterType JitterType
1920
}
2021

2122
// LinearBackoffConfig contains configuration for linear backoff
@@ -36,25 +37,8 @@ const (
3637
EqualJitter
3738
)
3839

39-
// ExponentialBackoffWithJitterConfig contains configuration for exponential backoff with jitter
40-
type ExponentialBackoffWithJitterConfig struct {
41-
ExponentialBackoffConfig
42-
JitterType JitterType
43-
}
44-
4540
// NewExponentialBackoff creates a new exponential backoff function
4641
func NewExponentialBackoff(config ExponentialBackoffConfig) BackoffDelayFunc {
47-
return func(attempt int) time.Duration {
48-
delay := time.Duration(float64(config.BaseDelay) * math.Pow(config.Multiplier, float64(attempt)))
49-
if delay > config.MaxDelay {
50-
delay = config.MaxDelay
51-
}
52-
return delay
53-
}
54-
}
55-
56-
// NewExponentialBackoffWithJitter creates a new exponential backoff function with jitter
57-
func NewExponentialBackoffWithJitter(config ExponentialBackoffWithJitterConfig) BackoffDelayFunc {
5842
return func(attempt int) time.Duration {
5943
baseDelay := time.Duration(float64(config.BaseDelay) * math.Pow(config.Multiplier, float64(attempt)))
6044
if baseDelay > config.MaxDelay {
@@ -104,31 +88,28 @@ func DefaultExponentialBackoff() BackoffDelayFunc {
10488
MaxDelay: 5 * time.Second,
10589
},
10690
Multiplier: 2.0,
91+
JitterType: NoJitter,
10792
})
10893
}
10994

11095
func DefaultExponentialBackoffWithFullJitter() BackoffDelayFunc {
111-
return NewExponentialBackoffWithJitter(ExponentialBackoffWithJitterConfig{
112-
ExponentialBackoffConfig: ExponentialBackoffConfig{
113-
BackoffConfig: BackoffConfig{
114-
BaseDelay: 100 * time.Millisecond,
115-
MaxDelay: 5 * time.Second,
116-
},
117-
Multiplier: 2.0,
96+
return NewExponentialBackoff(ExponentialBackoffConfig{
97+
BackoffConfig: BackoffConfig{
98+
BaseDelay: 100 * time.Millisecond,
99+
MaxDelay: 5 * time.Second,
118100
},
101+
Multiplier: 2.0,
119102
JitterType: FullJitter,
120103
})
121104
}
122105

123106
func DefaultExponentialBackoffWithEqualJitter() BackoffDelayFunc {
124-
return NewExponentialBackoffWithJitter(ExponentialBackoffWithJitterConfig{
125-
ExponentialBackoffConfig: ExponentialBackoffConfig{
126-
BackoffConfig: BackoffConfig{
127-
BaseDelay: 100 * time.Millisecond,
128-
MaxDelay: 5 * time.Second,
129-
},
130-
Multiplier: 2.0,
107+
return NewExponentialBackoff(ExponentialBackoffConfig{
108+
BackoffConfig: BackoffConfig{
109+
BaseDelay: 100 * time.Millisecond,
110+
MaxDelay: 5 * time.Second,
131111
},
112+
Multiplier: 2.0,
132113
JitterType: EqualJitter,
133114
})
134115
}

backoff_test.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,15 @@ func TestExponentialBackoff(t *testing.T) {
4545
func TestExponentialBackoffWithFullJitter(t *testing.T) {
4646
t.Parallel()
4747

48-
config := pgsql.ExponentialBackoffWithJitterConfig{
49-
ExponentialBackoffConfig: pgsql.ExponentialBackoffConfig{
50-
BackoffConfig: pgsql.BackoffConfig{
51-
BaseDelay: 100 * time.Millisecond,
52-
MaxDelay: 1 * time.Second,
53-
},
54-
Multiplier: 2.0,
48+
config := pgsql.ExponentialBackoffConfig{
49+
BackoffConfig: pgsql.BackoffConfig{
50+
BaseDelay: 100 * time.Millisecond,
51+
MaxDelay: 1 * time.Second,
5552
},
53+
Multiplier: 2.0,
5654
JitterType: pgsql.FullJitter,
5755
}
58-
backoff := pgsql.NewExponentialBackoffWithJitter(config)
56+
backoff := pgsql.NewExponentialBackoff(config)
5957

6058
// Test that jitter introduces randomness
6159
var delays []time.Duration
@@ -88,17 +86,15 @@ func TestExponentialBackoffWithFullJitter(t *testing.T) {
8886
func TestExponentialBackoffWithEqualJitter(t *testing.T) {
8987
t.Parallel()
9088

91-
config := pgsql.ExponentialBackoffWithJitterConfig{
92-
ExponentialBackoffConfig: pgsql.ExponentialBackoffConfig{
93-
BackoffConfig: pgsql.BackoffConfig{
94-
BaseDelay: 100 * time.Millisecond,
95-
MaxDelay: 1 * time.Second,
96-
},
97-
Multiplier: 2.0,
89+
config := pgsql.ExponentialBackoffConfig{
90+
BackoffConfig: pgsql.BackoffConfig{
91+
BaseDelay: 100 * time.Millisecond,
92+
MaxDelay: 1 * time.Second,
9893
},
94+
Multiplier: 2.0,
9995
JitterType: pgsql.EqualJitter,
10096
}
101-
backoff := pgsql.NewExponentialBackoffWithJitter(config)
97+
backoff := pgsql.NewExponentialBackoff(config)
10298

10399
delay := backoff(2)
104100

0 commit comments

Comments
 (0)