Skip to content

Commit 5f1bbb0

Browse files
committed
added delay backoff logic for jdk http sender
1 parent a12b804 commit 5f1bbb0

File tree

1 file changed

+11
-14
lines changed
  • exporters/sender/jdk/src/main/java/io/opentelemetry/exporter/sender/jdk/internal

1 file changed

+11
-14
lines changed

exporters/sender/jdk/src/main/java/io/opentelemetry/exporter/sender/jdk/internal/JdkHttpSender.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,11 @@ HttpResponse<byte[]> sendInternal(Marshaler marshaler) throws IOException {
213213
do {
214214
if (attempt > 0) {
215215
// Compute and sleep for backoff
216-
long upperBoundNanos = Math.min(nextBackoffNanos, retryPolicy.getMaxBackoff().toNanos());
217-
long backoffNanos = ThreadLocalRandom.current().nextLong(upperBoundNanos);
218-
nextBackoffNanos = (long) (nextBackoffNanos * retryPolicy.getBackoffMultiplier());
216+
long currentBackoffNanos =
217+
Math.min(nextBackoffNanos, retryPolicy.getMaxBackoff().toNanos());
218+
long backoffNanos =
219+
(long) (ThreadLocalRandom.current().nextDouble(0.8d, 1.2d) * currentBackoffNanos);
220+
nextBackoffNanos = (long) (currentBackoffNanos * retryPolicy.getBackoffMultiplier());
219221
try {
220222
TimeUnit.NANOSECONDS.sleep(backoffNanos);
221223
} catch (InterruptedException e) {
@@ -227,16 +229,11 @@ HttpResponse<byte[]> sendInternal(Marshaler marshaler) throws IOException {
227229
break;
228230
}
229231
}
230-
231-
attempt++;
232+
httpResponse = null;
233+
exception = null;
232234
requestBuilder.timeout(Duration.ofNanos(timeoutNanos - (System.nanoTime() - startTimeNanos)));
233235
try {
234236
httpResponse = sendRequest(requestBuilder, byteBufferPool);
235-
} catch (IOException e) {
236-
exception = e;
237-
}
238-
239-
if (httpResponse != null) {
240237
boolean retryable = retryableStatusCodes.contains(httpResponse.statusCode());
241238
if (logger.isLoggable(Level.FINER)) {
242239
logger.log(
@@ -251,8 +248,8 @@ HttpResponse<byte[]> sendInternal(Marshaler marshaler) throws IOException {
251248
if (!retryable) {
252249
return httpResponse;
253250
}
254-
}
255-
if (exception != null) {
251+
} catch (IOException e) {
252+
exception = e;
256253
boolean retryable = retryExceptionPredicate.test(exception);
257254
if (logger.isLoggable(Level.FINER)) {
258255
logger.log(
@@ -265,10 +262,10 @@ HttpResponse<byte[]> sendInternal(Marshaler marshaler) throws IOException {
265262
exception);
266263
}
267264
if (!retryable) {
268-
throw exception;
265+
throw e;
269266
}
270267
}
271-
} while (attempt < retryPolicy.getMaxAttempts());
268+
} while (++attempt < retryPolicy.getMaxAttempts());
272269

273270
if (httpResponse != null) {
274271
return httpResponse;

0 commit comments

Comments
 (0)