Skip to content

Commit

Permalink
add random jitter
Browse files Browse the repository at this point in the history
  • Loading branch information
andresfsilva committed Nov 24, 2021
1 parent f510376 commit 910759b
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.slf4j.LoggerFactory;
import uk.ac.ebi.ampt2d.commons.accession.utils.exceptions.ExponentialBackOffMaxRetriesRuntimeException;

import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;

/**
Expand All @@ -32,6 +33,8 @@ public abstract class ExponentialBackOff {

static int DEFAULT_TOTAL_ATTEMPTS = 7;
private static int DEFAULT_TIME_BASE = 1000;
private static int MAX_JITTER = 1000;
private static int MIN_JITTER = 100;

public static void execute(Runnable function) {
execute(function, DEFAULT_TOTAL_ATTEMPTS, DEFAULT_TIME_BASE);
Expand Down Expand Up @@ -78,7 +81,7 @@ public static <T> T execute(Supplier<T> function, int totalAttempts, int timeBas

private static void doWait(int valueInTheSeries, int timeBase) {
try {
Thread.sleep(valueInTheSeries * timeBase);
Thread.sleep(valueInTheSeries * timeBase + ThreadLocalRandom.current().nextInt(MIN_JITTER, MAX_JITTER));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 910759b

Please sign in to comment.