Added multiple attempts with backoff delay to datebase connection

This commit is contained in:
Fred Boniface
2024-04-14 21:21:13 +01:00
parent 86da330b91
commit f97bea78eb
2 changed files with 54 additions and 12 deletions

18
helpers/backoff.go Normal file
View File

@@ -0,0 +1,18 @@
package helpers
import (
"math"
"time"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
// Implements an exponential backoff strategy and sleeps for a duration calculated as 1 second to the power of (attempt - 1).
// The backoff time doubles with each attempt, starting from 1 second for the first attempt.
func BackoffDelay(attempt int) {
base := time.Second
backoff := base * time.Duration(math.Pow(2, float64(attempt-1)))
log.Info("Retry backoff", zap.Duration("delay", backoff))
time.Sleep(backoff)
}