package cd.casic.framework.commons.util.retry;

import lombok.SneakyThrows;

/**
 * @author by mianbin
 * @Classname RetryUtils
 * @Description TODO
 * @Date 2025/3/18 11:28
 */
public class RetryUtils {
    @SneakyThrows
    public static <T> T execute(Action<T> action, Integer retryTime, Long retryPeriodMills) {
        try {
            return action.execute();
        } catch (Throwable ignored) {
            if (retryTime - 1 <= 0) {
                return action.fail(ignored);
            }
            Thread.sleep(retryPeriodMills);
            return execute(action, retryTime - 1, 500L);
        }
    }

    public interface Action<T> {

        T execute();

        default T fail(Throwable e) throws Throwable {
            throw e;
        }
    }
}
