Skip to content

Commit

Permalink
feat(java-client): add client retry policy
Browse files Browse the repository at this point in the history
  • Loading branch information
yujingwei committed Mar 1, 2024
1 parent 0d7614f commit c75e73a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ public RetryAction shouldRetry(int retries, long deadlineNanos, Duration timeout
if (now >= deadlineNanos) {
return new RetryAction(RetryDecision.FAIL, Duration.ZERO, "request deadline reached");
}
long timeoutMs = timeout.toMillis();
long retryDelayMs =
long timeoutNanos = timeout.toNanos();
long retryDelayNanos =
Math.min(
timeoutMs > 3 ? timeoutMs / 3 : 1, TimeUnit.NANOSECONDS.toMillis(deadlineNanos - now));
return new RetryAction(RetryDecision.RETRY, Duration.ofNanos(retryDelayMs), "");
timeoutNanos > 3000000 ? timeoutNanos / 3000000 : 1,
TimeUnit.NANOSECONDS.toNanos(deadlineNanos - now));
return new RetryAction(RetryDecision.RETRY, Duration.ofNanos(retryDelayNanos), "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ public RetryAction shouldRetry(int retries, long deadlineNanos, Duration timeout
if (now >= deadlineNanos) {
return new RetryAction(RetryDecision.FAIL, Duration.ZERO, "request deadline reached");
}
long normalIntervalMs =
retryBaseIntervalMs * RETRY_BACKOFF[Math.min(retries, RETRY_BACKOFF.length - 1)];
long normalIntervalNanos =
retryBaseIntervalMs * RETRY_BACKOFF[Math.min(retries, RETRY_BACKOFF.length - 1)] * 1000000;
// 1% possible jitter
long jitterNanos = (long) (normalIntervalMs * ThreadLocalRandom.current().nextFloat() * 0.01f);
long jitterNanos =
(long) (normalIntervalNanos * ThreadLocalRandom.current().nextFloat() * 0.01f);
long retryIntervalNanos =
Math.min(
normalIntervalMs + jitterNanos, TimeUnit.NANOSECONDS.toMillis(deadlineNanos - now));
normalIntervalNanos + jitterNanos, TimeUnit.NANOSECONDS.toNanos(deadlineNanos - now));
return new RetryAction(RetryDecision.RETRY, Duration.ofNanos(retryIntervalNanos), "");
}
}

0 comments on commit c75e73a

Please sign in to comment.