Skip to content

Commit

Permalink
retry: Set attempt's initial value to 1
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
lippserd and oxzi committed Apr 9, 2024
1 parent f215bc5 commit 8d187be
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/config/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/icingadb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/icingadb/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
},
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/icingadb/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,26 +390,26 @@ 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
}

log("Can't update or insert instance. Retrying", zap.Error(err))
}
},
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
}

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))
}
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8d187be

Please sign in to comment.