diff --git a/pkg/config/redis.go b/pkg/config/redis.go index 3ff139db..88db519e 100644 --- a/pkg/config/redis.go +++ b/pkg/config/redis.go @@ -10,7 +10,6 @@ import ( "go.uber.org/zap" "net" "os" - "sync" "syscall" "time" ) @@ -45,17 +44,11 @@ func dialWithLogging(logger *zap.SugaredLogger) func(context.Context, string, st // dial behaves like net.Dialer#DialContext, but re-tries on syscall.ECONNREFUSED. return func(ctx context.Context, network, addr string) (conn net.Conn, err error) { var dl net.Dialer - var logFirstError sync.Once err = retry.WithBackoff( ctx, func(ctx context.Context) (err error) { conn, err = dl.DialContext(ctx, network, addr) - logFirstError.Do(func() { - if err != nil { - logger.Warnw("Can't connect to Redis. Retrying", zap.Error(err)) - } - }) return }, func(err error) bool { @@ -66,7 +59,14 @@ func dialWithLogging(logger *zap.SugaredLogger) func(context.Context, string, st return false }, backoff.NewExponentialWithJitter(1*time.Millisecond, 1*time.Second), - retry.Settings{Timeout: 5 * time.Minute}, + retry.Settings{ + Timeout: 5 * time.Minute, + OnError: func(_ time.Duration, _ uint64, err, lastErr error) { + if lastErr == nil || err.Error() != lastErr.Error() { + logger.Warnw("Can't connect to Redis. Retrying", zap.Error(err)) + } + }, + }, ) err = errors.Wrap(err, "can't connect to Redis") diff --git a/pkg/driver/driver.go b/pkg/driver/driver.go index 49e45bc6..31fba21e 100644 --- a/pkg/driver/driver.go +++ b/pkg/driver/driver.go @@ -9,7 +9,6 @@ import ( "github.com/icinga/icingadb/pkg/retry" "github.com/pkg/errors" "go.uber.org/zap" - "sync" "syscall" "time" ) @@ -25,23 +24,22 @@ type RetryConnector struct { // Connect implements part of the driver.Connector interface. func (c RetryConnector) Connect(ctx context.Context) (driver.Conn, error) { var conn driver.Conn - var logFirstError sync.Once err := errors.Wrap(retry.WithBackoff( ctx, func(ctx context.Context) (err error) { conn, err = c.Connector.Connect(ctx) - - logFirstError.Do(func() { - if err != nil { - c.driver.Logger.Warnw("Can't connect to database. Retrying", zap.Error(err)) - } - }) - return }, shouldRetry, backoff.NewExponentialWithJitter(time.Millisecond*128, time.Minute*1), - retry.Settings{Timeout: timeout}, + retry.Settings{ + Timeout: timeout, + OnError: func(_ time.Duration, _ uint64, err, lastErr error) { + if lastErr == nil || err.Error() != lastErr.Error() { + c.driver.Logger.Warnw("Can't connect to database. Retrying", zap.Error(err)) + } + }, + }, ), "can't connect to database") return conn, err }