Skip to content

Commit ea70fff

Browse files
committed
retry on failure
1 parent 12350f5 commit ea70fff

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

main.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ func main() {
164164
os.Exit(0)
165165
}
166166

167+
retryDelay := time.Second
168+
const maxRetryDelay = time.Minute
169+
retryAttempts := 0
170+
const maxRetryAttempts = 10
171+
167172
for {
168173
select {
169174
case <-signalCh:
@@ -173,6 +178,23 @@ func main() {
173178
request, err := newVaultRequest(http.MethodHead, vaultAddr+"/v1/sys/health", nil)
174179
if err != nil {
175180
log.Println(err)
181+
if checkInterval < 0 {
182+
retryAttempts++
183+
if retryAttempts >= maxRetryAttempts {
184+
log.Printf("Health check failed after %d attempts; exiting with failure", retryAttempts)
185+
os.Exit(1)
186+
}
187+
log.Printf(
188+
"Retrying health check in %s (exponential backoff), attempt %d/%d",
189+
retryDelay, retryAttempts, maxRetryAttempts,
190+
)
191+
time.Sleep(retryDelay)
192+
retryDelay *= 2
193+
if retryDelay > maxRetryDelay {
194+
retryDelay = maxRetryDelay
195+
}
196+
continue
197+
}
176198
time.Sleep(checkInterval)
177199
continue
178200
}
@@ -185,10 +207,51 @@ func main() {
185207

186208
if err != nil {
187209
log.Println(err)
210+
if checkInterval < 0 {
211+
retryAttempts++
212+
if retryAttempts >= maxRetryAttempts {
213+
log.Printf("Health check failed after %d attempts; exiting with failure", retryAttempts)
214+
os.Exit(1)
215+
}
216+
log.Printf(
217+
"Retrying health check in %s (exponential backoff), attempt %d/%d",
218+
retryDelay, retryAttempts, maxRetryAttempts,
219+
)
220+
time.Sleep(retryDelay)
221+
retryDelay *= 2
222+
if retryDelay > maxRetryDelay {
223+
retryDelay = maxRetryDelay
224+
}
225+
continue
226+
}
188227
time.Sleep(checkInterval)
189228
continue
190229
}
191230

231+
if checkInterval < 0 && response.StatusCode >= http.StatusInternalServerError && response.StatusCode < 600 {
232+
retryAttempts++
233+
if retryAttempts >= maxRetryAttempts {
234+
log.Printf(
235+
"Vault health returned %d and failed after %d attempts; exiting with failure",
236+
response.StatusCode, retryAttempts,
237+
)
238+
os.Exit(1)
239+
}
240+
log.Printf(
241+
"Vault health returned %d. Retrying in %s (exponential backoff), attempt %d/%d",
242+
response.StatusCode, retryDelay, retryAttempts, maxRetryAttempts,
243+
)
244+
time.Sleep(retryDelay)
245+
retryDelay *= 2
246+
if retryDelay > maxRetryDelay {
247+
retryDelay = maxRetryDelay
248+
}
249+
continue
250+
}
251+
252+
retryDelay = time.Second
253+
retryAttempts = 0
254+
192255
switch response.StatusCode {
193256
case 200:
194257
log.Println("Vault is initialized and unsealed.")

0 commit comments

Comments
 (0)