Skip to content

Commit 7602d5e

Browse files
committed
retry on failure (#10)
1 parent 12350f5 commit 7602d5e

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

.github/workflows/lint-test-build-push.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: build-push
22
on:
33
push:
4+
workflow_dispatch:
45
jobs:
56
test:
67
permissions:

main.go

Lines changed: 42 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,30 @@ 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+
retryDelay = time.Second
232+
retryAttempts = 0
233+
192234
switch response.StatusCode {
193235
case 200:
194236
log.Println("Vault is initialized and unsealed.")

0 commit comments

Comments
 (0)