added retryUtil for exponential backoff

This commit is contained in:
fahad 2020-06-15 01:53:11 +05:30
Родитель 4d6474c66a
Коммит 28d8705da3
1 изменённых файлов: 38 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,38 @@
package com.microsoft.azure.kusto.kafka.connect.sink;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class RetryUtil {
/**
* An arbitrary absolute maximum retry time.
*/
public static final long MAX_RETRY_TIME_MS = TimeUnit.HOURS.toMillis(24);
public static long computeExponentialBackOffWithJitter(int retryAttempts, long initialRetryBackoffMs) {
if (initialRetryBackoffMs < 0) {
return 0;
}
if (retryAttempts < 0) {
return initialRetryBackoffMs;
}
long maxRetryTime = computeBackOffTime(retryAttempts, initialRetryBackoffMs);
return ThreadLocalRandom.current().nextLong(0, maxRetryTime);
}
public static long computeBackOffTime(int retryAttempts, long initialRetryBackoffMs) {
if (initialRetryBackoffMs < 0) {
return 0;
}
if (retryAttempts <= 0) {
return initialRetryBackoffMs;
}
if (retryAttempts > 32) {
// This would overflow the exponential algorithm ...
return MAX_RETRY_TIME_MS;
}
long result = initialRetryBackoffMs << retryAttempts;
return result < 0L ? MAX_RETRY_TIME_MS : Math.min(MAX_RETRY_TIME_MS, result);
}
}