Move KetchSystem.delay to FileUtils.

This will provide exponential backoff with jitter to other JGit
components too.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Change-Id: Idd44e3bbaef6d71134ce2e3f7d405f35e7397cbd
This commit is contained in:
Han-Wen Nienhuys 2019-11-13 07:12:05 -08:00
parent 566a46e9ec
commit 74bfec4112
3 changed files with 30 additions and 24 deletions

View File

@ -77,6 +77,7 @@
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.SystemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -532,9 +533,8 @@ private void retryLater(ReplicaPushRequest req) {
queued.add(0, new ReplicaPushRequest(this, cmds));
if (!waitingForRetry()) {
long delay = KetchSystem.delay(
lastRetryMillis,
minRetryMillis, maxRetryMillis);
long delay = FileUtils
.delay(lastRetryMillis, minRetryMillis, maxRetryMillis);
if (log.isDebugEnabled()) {
log.debug("Retrying {} after {} ms", //$NON-NLS-1$
describeForLog(), Long.valueOf(delay));

View File

@ -350,25 +350,4 @@ private DefaultExecutorHolder() {
}
}
/**
* Compute a delay in a {@code min..max} interval with random jitter.
*
* @param last
* amount of delay waited before the last attempt. This is used
* to seed the next delay interval. Should be 0 if there was no
* prior delay.
* @param min
* shortest amount of allowable delay between attempts.
* @param max
* longest amount of allowable delay between attempts.
* @return new amount of delay to wait before the next attempt.
*/
static long delay(long last, long min, long max) {
long r = Math.max(0, last * 3 - min);
if (r > 0) {
int c = (int) Math.min(r + 1, Integer.MAX_VALUE);
r = RNG.nextInt(c);
}
return Math.max(Math.min(min + r, max), min);
}
}

View File

@ -73,6 +73,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Pattern;
import org.eclipse.jgit.internal.JGitText;
@ -87,6 +88,8 @@
public class FileUtils {
private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class);
private static final Random RNG = new Random();
/**
* Option to delete given {@code File}
*/
@ -986,4 +989,28 @@ public static void touch(Path f) throws IOException {
}
Files.setLastModifiedTime(f, FileTime.from(Instant.now()));
}
/**
* Compute a delay in a {@code min..max} interval with random jitter.
*
* @param last
* amount of delay waited before the last attempt. This is used
* to seed the next delay interval. Should be 0 if there was no
* prior delay.
* @param min
* shortest amount of allowable delay between attempts.
* @param max
* longest amount of allowable delay between attempts.
* @return new amount of delay to wait before the next attempt.
*
* @since 5.6
*/
public static long delay(long last, long min, long max) {
long r = Math.max(0, last * 3 - min);
if (r > 0) {
int c = (int) Math.min(r + 1, Integer.MAX_VALUE);
r = RNG.nextInt(c);
}
return Math.max(Math.min(min + r, max), min);
}
}