Added multiple attempts with backoff delay to datebase connection
This commit is contained in:
18
helpers/backoff.go
Normal file
18
helpers/backoff.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user