com.algolia.utils.TaskUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of algoliasearch Show documentation
Show all versions of algoliasearch Show documentation
Java client for Algolia Search API
The newest version!
package com.algolia.utils;
import com.algolia.exceptions.*;
import java.util.function.IntUnaryOperator;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class TaskUtils {
private TaskUtils() {
// Empty.
}
public static final int DEFAULT_MAX_RETRIES = 50;
public static final IntUnaryOperator DEFAULT_TIMEOUT = (int retries) -> Math.min(retries * 200, 5000);
public static T retryUntil(Supplier func, Predicate validate, int maxRetries, IntUnaryOperator timeout)
throws AlgoliaRuntimeException {
int retryCount = 0;
while (retryCount < maxRetries) {
T resp = func.get();
if (validate.test(resp)) {
return resp;
}
try {
Thread.sleep(timeout.applyAsInt(retryCount));
} catch (InterruptedException ignored) {
// Restore interrupted state...
Thread.currentThread().interrupt();
}
retryCount++;
}
throw new AlgoliaRetriesExceededException("The maximum number of retries exceeded. (" + (retryCount + 1) + "/" + maxRetries + ")");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy