@Test void ignoreAlreadyCompletedRequest() { Request<?> request = mock(Request.class); when(request.completed()).thenReturn(true); RetryOrchestrator.maybeRetry(null, request, RetryReason.UNKNOWN); verify(request, times(1)).completed(); verifyNoMoreInteractions(request); }
public static void maybeRetry(final CoreContext ctx, final Request<? extends Response> request, final RetryReason reason) { if (request.completed()) { return; } if (reason.alwaysRetry()) { retryWithDuration(ctx, request, controlledBackoff(request.context().retryAttempts()), reason); return; } request.retryStrategy().shouldRetry(request, reason).whenComplete((retryAction, throwable) -> { if (throwable != null) { ctx.environment().eventBus().publish( new RequestNotRetriedEvent(Event.Severity.INFO, request.getClass(), request.context(), reason, throwable) ); } Optional<Duration> duration = retryAction.duration(); if (duration.isPresent()) { final Duration cappedDuration = capDuration(duration.get(), request); retryWithDuration(ctx, request, cappedDuration, reason); } else { Event.Severity severity = request instanceof UnmonitoredRequest ? Event.Severity.DEBUG : Event.Severity.INFO; ctx.environment().eventBus().publish( new RequestNotRetriedEvent(severity, request.getClass(), request.context(), reason, null) ); request.cancel(CancellationReason.noMoreRetries(reason)); } }); }
RetryOrchestrator { public static void maybeRetry(final CoreContext ctx, final Request<? extends Response> request, final RetryReason reason) { if (request.completed()) { return; } if (reason.alwaysRetry()) { retryWithDuration(ctx, request, controlledBackoff(request.context().retryAttempts()), reason); return; } request.retryStrategy().shouldRetry(request, reason).whenComplete((retryAction, throwable) -> { if (throwable != null) { ctx.environment().eventBus().publish( new RequestNotRetriedEvent(Event.Severity.INFO, request.getClass(), request.context(), reason, throwable) ); } Optional<Duration> duration = retryAction.duration(); if (duration.isPresent()) { final Duration cappedDuration = capDuration(duration.get(), request); retryWithDuration(ctx, request, cappedDuration, reason); } else { Event.Severity severity = request instanceof UnmonitoredRequest ? Event.Severity.DEBUG : Event.Severity.INFO; ctx.environment().eventBus().publish( new RequestNotRetriedEvent(severity, request.getClass(), request.context(), reason, null) ); request.cancel(CancellationReason.noMoreRetries(reason)); } }); } }
RetryOrchestrator { public static void maybeRetry(final CoreContext ctx, final Request<? extends Response> request, final RetryReason reason) { if (request.completed()) { return; } if (reason.alwaysRetry()) { retryWithDuration(ctx, request, controlledBackoff(request.context().retryAttempts()), reason); return; } request.retryStrategy().shouldRetry(request, reason).whenComplete((retryAction, throwable) -> { if (throwable != null) { ctx.environment().eventBus().publish( new RequestNotRetriedEvent(Event.Severity.INFO, request.getClass(), request.context(), reason, throwable) ); } Optional<Duration> duration = retryAction.duration(); if (duration.isPresent()) { final Duration cappedDuration = capDuration(duration.get(), request); retryWithDuration(ctx, request, cappedDuration, reason); } else { Event.Severity severity = request instanceof UnmonitoredRequest ? Event.Severity.DEBUG : Event.Severity.INFO; ctx.environment().eventBus().publish( new RequestNotRetriedEvent(severity, request.getClass(), request.context(), reason, null) ); request.cancel(CancellationReason.noMoreRetries(reason)); } }); }  }
RetryOrchestrator { public static void maybeRetry(final CoreContext ctx, final Request<? extends Response> request, final RetryReason reason) { if (request.completed()) { return; } if (reason.alwaysRetry()) { retryWithDuration(ctx, request, controlledBackoff(request.context().retryAttempts()), reason); return; } request.retryStrategy().shouldRetry(request, reason).whenComplete((retryAction, throwable) -> { if (throwable != null) { ctx.environment().eventBus().publish( new RequestNotRetriedEvent(Event.Severity.INFO, request.getClass(), request.context(), reason, throwable) ); } Optional<Duration> duration = retryAction.duration(); if (duration.isPresent()) { final Duration cappedDuration = capDuration(duration.get(), request); retryWithDuration(ctx, request, cappedDuration, reason); } else { Event.Severity severity = request instanceof UnmonitoredRequest ? Event.Severity.DEBUG : Event.Severity.INFO; ctx.environment().eventBus().publish( new RequestNotRetriedEvent(severity, request.getClass(), request.context(), reason, null) ); request.cancel(CancellationReason.noMoreRetries(reason)); } }); }  static void maybeRetry(final CoreContext ctx, final Request<? extends Response> request,
                                final RetryReason reason); @Stability.Internal static Duration capDuration(final Duration uncappedDuration, final Request<? extends Response> request); }
RetryOrchestrator { public static void maybeRetry(final CoreContext ctx, final Request<? extends Response> request, final RetryReason reason) { if (request.completed()) { return; } if (reason.alwaysRetry()) { retryWithDuration(ctx, request, controlledBackoff(request.context().retryAttempts()), reason); return; } request.retryStrategy().shouldRetry(request, reason).whenComplete((retryAction, throwable) -> { if (throwable != null) { ctx.environment().eventBus().publish( new RequestNotRetriedEvent(Event.Severity.INFO, request.getClass(), request.context(), reason, throwable) ); } Optional<Duration> duration = retryAction.duration(); if (duration.isPresent()) { final Duration cappedDuration = capDuration(duration.get(), request); retryWithDuration(ctx, request, cappedDuration, reason); } else { Event.Severity severity = request instanceof UnmonitoredRequest ? Event.Severity.DEBUG : Event.Severity.INFO; ctx.environment().eventBus().publish( new RequestNotRetriedEvent(severity, request.getClass(), request.context(), reason, null) ); request.cancel(CancellationReason.noMoreRetries(reason)); } }); }  static void maybeRetry(final CoreContext ctx, final Request<? extends Response> request,
                                final RetryReason reason); @Stability.Internal static Duration capDuration(final Duration uncappedDuration, final Request<? extends Response> request);  }