Skip to content

Commit e1ed626

Browse files
committed
using cenkalti v4 for retry backoff with jitter
1 parent d6bb7af commit e1ed626

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

watch/view.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sync"
1313
"time"
1414

15+
"github.com/cenkalti/backoff/v4"
1516
dep "github.com/hashicorp/consul-template/dependency"
1617
)
1718

@@ -166,17 +167,26 @@ func (v *View) poll(viewCh chan<- *View, errCh chan<- error, serverErrCh chan<-
166167

167168
// Check if this is a TTL=0 error and use custom retry logic
168169
if _, isTTLZeroErr := err.(*dep.VaultTTLZeroError); isTTLZeroErr {
169-
// Use custom TTL=0 retry configuration
170+
// Use custom TTL=0 retry configuration with exponential backoff
170171
if dep.VaultTTLZeroMaxRetries > 0 && retries >= dep.VaultTTLZeroMaxRetries {
171172
retry = false
172173
sleep = 0
173174
} else {
174175
retry = true
175-
// Calculate exponential backoff: 250ms * 2^retry
176-
baseSleep := 250 * time.Millisecond
177-
sleep = time.Duration(1<<uint(retries)) * baseSleep
178-
// Cap at max backoff
179-
if dep.VaultTTLZeroMaxBackoff > 0 && sleep > dep.VaultTTLZeroMaxBackoff {
176+
// Use cenkalti/backoff for exponential backoff calculation
177+
b := backoff.NewExponentialBackOff()
178+
b.InitialInterval = 250 * time.Millisecond
179+
b.MaxInterval = dep.VaultTTLZeroMaxBackoff
180+
b.Multiplier = 2.0
181+
b.RandomizationFactor = 0.5 // Add jitter to prevent thundering herd
182+
b.MaxElapsedTime = 0 // No time limit, only retry count matters
183+
b.Reset()
184+
185+
// Calculate backoff for current retry attempt
186+
for i := 0; i < retries; i++ {
187+
sleep = b.NextBackOff()
188+
}
189+
if sleep == backoff.Stop {
180190
sleep = dep.VaultTTLZeroMaxBackoff
181191
}
182192
}

0 commit comments

Comments
 (0)