summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHan-Wen Nienhuys <hanwen@google.com>2019-11-13 07:12:05 -0800
committerHan-Wen Nienhuys <hanwen@google.com>2019-11-13 15:14:09 -0800
commit74bfec411223bdfdd0d60aa912d420d9a4447375 (patch)
treeb5e6be2ed5d917f88a071a6dbb24fc7715e558c9
parent566a46e9ec6abed9521ed74fc14a5734cfd033af (diff)
downloadjgit-74bfec411223bdfdd0d60aa912d420d9a4447375.tar.gz
jgit-74bfec411223bdfdd0d60aa912d420d9a4447375.zip
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
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java6
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java21
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java27
3 files changed, 30 insertions, 24 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java
index 0e8377dd02..52c8f29ddc 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java
@@ -77,6 +77,7 @@ import org.eclipse.jgit.lib.Repository;
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 @@ public abstract class KetchReplica {
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));
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java
index d1d4f67d86..fd334f149a 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java
@@ -350,25 +350,4 @@ public class KetchSystem {
}
}
- /**
- * 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);
- }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
index 4d791e470a..e026e9274f 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
@@ -73,6 +73,7 @@ import java.time.Instant;
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 @@ import org.slf4j.LoggerFactory;
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 class FileUtils {
}
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);
+ }
}