io.spiffe.workloadapi.retry.RetryHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-spiffe-core Show documentation
Show all versions of java-spiffe-core Show documentation
Core functionality to fetch, process and validate X.509 and JWT SVIDs and Bundles from the Workload API.
package io.spiffe.workloadapi.retry;
import java.time.Duration;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Provides methods to schedule the execution of retries based on a backoff policy.
*/
public class RetryHandler {
private final ScheduledExecutorService executor;
private final ExponentialBackoffPolicy exponentialBackoffPolicy;
private Duration nextDelay;
private int retryCount;
public RetryHandler(final ExponentialBackoffPolicy exponentialBackoffPolicy, final ScheduledExecutorService executor) {
this.nextDelay = exponentialBackoffPolicy.getInitialDelay();
this.exponentialBackoffPolicy = exponentialBackoffPolicy;
this.executor = executor;
}
/**
* Schedule to execute a Runnable, based on the backoff policy
* Updates the next delay and retries count.
*
* @param runnable the task to be scheduled for execution
*/
public void scheduleRetry(final Runnable runnable) {
if (exponentialBackoffPolicy.reachedMaxRetries(retryCount)) {
return;
}
executor.schedule(runnable, nextDelay.getSeconds(), TimeUnit.SECONDS);
nextDelay = exponentialBackoffPolicy.nextDelay(nextDelay);
retryCount++;
}
/**
* Returns true is a new retry should be performs, according the the Retry Policy.
* @return true is a new retry should be performs, according the the Retry Policy
*/
public boolean shouldRetry() {
return !exponentialBackoffPolicy.reachedMaxRetries(retryCount);
}
/**
* Reset state of RetryHandle to initial values.
*/
public void reset() {
nextDelay = exponentialBackoffPolicy.getInitialDelay();
retryCount = 0;
}
public Duration getNextDelay() {
return nextDelay;
}
public int getRetryCount() {
return retryCount;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy