@@ -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