func VerifyDBConn(ctx context.Context, db *sql.DB, retries int, retryDelay time.Duration, timeout time.Duration) error {

	myFuncName := caller.GetFuncName()

	// TODO: Expose timeout as a config setting
	ctx, cancel := context.WithTimeout(ctx, timeout)
	defer cancel()

	var result error

	// initial attempt + number of specified retries
	attemptsAllowed := 1 + retries

	for attempt := 1; attempt <= attemptsAllowed; attempt++ {
		// the result from the last attempt is returned to the caller
		result = db.PingContext(ctx)

		switch {
		case result == nil:

			log.Debugf(
				"Successfully confirmed database connection after %d of %d attempts",
				attempt,
				attemptsAllowed,
			)

			// No further retries needed
			return nil

		// Check context explicitly in order to return as soon as possible
		case ctx.Err() != nil && result != nil:

			errMsg := fmt.Errorf(
				"%s: context cancelled or expired: %v; "+
					"aborting database connection verification after %d of %d attempts: %w",
				myFuncName,
				ctx.Err().Error(),
				attempt,
				attemptsAllowed,
				result,
			)
			return errMsg

		case result != nil:

			log.Warnf(
				"%s: attempt %d of %d to verify database connection failed: %v",
				myFuncName,
				attempt,
				attemptsAllowed,
				result,
			)

			log.Infof(
				"%s: applying retry delay of %v",
				myFuncName,
				retryDelay,
			)
			time.Sleep(retryDelay)

		}

	}

	return result

}
// VerifyDBConn is used to verify connectivity to a specified database. If the
// initial verification fails, a specified number of further attempts are made
// before an error is returned to signal that a database connection is
// unavailable.
VerifyDBConn is used to verify connectivity to a specified database. If the
initial verification fails, a specified number of further attempts are made
before an error is returned to signal that a database connection is
unavailable.
// VerifyDBConn is used to verify connectivity to a specified database . If the // initial verification fails a specified number of further attempts are made // before an error is returned to signal that a database connection is // unavailable .
func VerifyDBConn(ctx context.Context, db *sql.DB, retries int, retryDelay time.Duration, timeout time.Duration) error {
	myFuncName := caller.GetFuncName()
	ctx, cancel := context.WithTimeout(ctx, timeout)
	defer cancel()
	var result error
	attemptsAllowed := 1 + retries
	for attempt := 1; attempt <= attemptsAllowed; attempt++ {
		result = db.PingContext(ctx)
		switch {
		case result == nil:
			log.Debugf(
				"Successfully confirmed database connection after %d of %d attempts",
				attempt,
				attemptsAllowed,
			)
			return nil
		case ctx.Err() != nil && result != nil:
			errMsg := fmt.Errorf(
				"%s: context cancelled or expired: %v; "+
					"aborting database connection verification after %d of %d attempts: %w",
				myFuncName,
				ctx.Err().Error(),
				attempt,
				attemptsAllowed,
				result,
			)
			return errMsg
		case result != nil:
			log.Warnf(
				"%s: attempt %d of %d to verify database connection failed: %v",
				myFuncName,
				attempt,
				attemptsAllowed,
				result,
			)
			log.Infof(
				"%s: applying retry delay of %v",
				myFuncName,
				retryDelay,
			)
			time.Sleep(retryDelay)
		}
	}
	return result
}
func VerifyDBConn ( ctx context . Context , db * sql . DB , retries int , retryDelay time . Duration , timeout time . Duration ) error { myFuncName := caller . GetFuncName ( ) ctx , cancel := context . WithTimeout ( ctx , timeout ) defer cancel ( ) var result error attemptsAllowed := 1 + retries for attempt := 1 ; attempt <= attemptsAllowed ; attempt ++ { result = db . PingContext ( ctx ) switch { case result == nil : log . Debugf ( " " , attempt , attemptsAllowed , ) return nil case ctx . Err ( ) != nil && result != nil : errMsg := fmt . Errorf ( " " + " " , myFuncName , ctx . Err ( ) . Error ( ) , attempt , attemptsAllowed , result , ) return errMsg case result != nil : log . Warnf ( " " , myFuncName , attempt , attemptsAllowed , result , ) log . Infof ( " " , myFuncName , retryDelay , ) time . Sleep ( retryDelay ) } } return result }
VerifyDBConn is used to verify connectivity to a specified database.
VerifyDBConn is used to verify connectivity to a specified database .
// TODO: Expose timeout as a config setting // initial attempt + number of specified retries // the result from the last attempt is returned to the caller // No further retries needed // Check context explicitly in order to return as soon as possible