retry: Set attempt's initial value to 1

This change simplifies the use of `attempt` as a number for reading in
log messages and `if`s. Also before, with `attempt` starting with `0`,
the second attempt would have been taken immediately, as our backoff
implementation returns `0` in this case.

Co-Authored-By: Alvar Penning <alvar.penning@icinga.com>
This commit is contained in:
Eric Lippmann 2024-04-09 16:02:15 +02:00
parent c2b449d3a6
commit 2b3a5d4229
5 changed files with 11 additions and 11 deletions

View file

@ -92,9 +92,9 @@ func dialWithLogging(dialer ctxDialerFunc, logger *logging.Logger) ctxDialerFunc
}
},
OnSuccess: func(elapsed time.Duration, attempt uint64, _ error) {
if attempt > 0 {
if attempt > 1 {
logger.Infow("Reconnected to Redis",
zap.Duration("after", elapsed), zap.Uint64("attempts", attempt+1))
zap.Duration("after", elapsed), zap.Uint64("attempts", attempt))
}
},
},

View file

@ -680,10 +680,10 @@ func (db *DB) getDefaultRetrySettings() retry.Settings {
}
},
OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) {
if attempt > 0 {
if attempt > 1 {
db.logger.Infow("Query retried successfully after error",
zap.Duration("after", elapsed),
zap.Uint64("attempts", attempt+1),
zap.Uint64("attempts", attempt),
zap.NamedError("recovered_error", lastErr))
}
},

View file

@ -66,9 +66,9 @@ func (c RetryConnector) Connect(ctx context.Context) (driver.Conn, error) {
OnSuccess: func(elapsed time.Duration, attempt uint64, _ error) {
telemetry.UpdateCurrentDbConnErr(nil)
if attempt > 0 {
if attempt > 1 {
c.logger.Infow("Reconnected to database",
zap.Duration("after", elapsed), zap.Uint64("attempts", attempt+1))
zap.Duration("after", elapsed), zap.Uint64("attempts", attempt))
}
},
},

View file

@ -390,7 +390,7 @@ func (h *HA) realize(
OnRetryableError: func(_ time.Duration, attempt uint64, err, lastErr error) {
if lastErr == nil || err.Error() != lastErr.Error() {
log := h.logger.Debugw
if attempt > 2 {
if attempt > 3 {
log = h.logger.Infow
}
@ -398,10 +398,10 @@ func (h *HA) realize(
}
},
OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) {
if attempt > 0 {
if attempt > 1 {
log := h.logger.Debugw
if attempt > 3 {
if attempt > 4 {
// We log errors with severity info starting from the fourth attempt, (see above)
// so we need to log success with severity info from the fifth attempt.
log = h.logger.Infow
@ -409,7 +409,7 @@ func (h *HA) realize(
log("Instance updated/inserted successfully after error",
zap.Duration("after", elapsed),
zap.Uint64("attempts", attempt+1),
zap.Uint64("attempts", attempt),
zap.NamedError("recovered_error", lastErr))
}
},

View file

@ -50,7 +50,7 @@ func WithBackoff(
}
start := time.Now()
for attempt := uint64(0); ; /* true */ attempt++ {
for attempt := uint64(1); ; /* true */ attempt++ {
prevErr := err
if err = retryableFunc(ctx); err == nil {