Class ForwardRetryPolicy

java.lang.Object
org.mockserver.mock.action.http.ForwardRetryPolicy

public final class ForwardRetryPolicy extends Object
Stateless retry logic for forwarded/proxied requests. Re-issues a request to its upstream up to maxRetries times when the previous attempt produced a transient failure — a connection-level exception, or an upstream response of 502/503/504 — provided the HTTP method is idempotent. Non-idempotent methods (POST, PATCH) are never retried, so a request is never silently executed twice.

The whole policy is inert when maxRetries <= 0: execute(java.lang.String, int, long, java.util.function.Supplier<java.util.concurrent.CompletableFuture<org.mockserver.model.HttpResponse>>) simply returns the first attempt's future, preserving the historical "forward exactly once" behaviour.

Retries are chained asynchronously off the supplied future (never blocking the event loop). A linear back-off (backoffMillis * attemptNumber) is applied between attempts via CompletableFuture.delayedExecutor(long, java.util.concurrent.TimeUnit, java.util.concurrent.Executor).

  • Method Details

    • isIdempotent

      public static boolean isIdempotent(String method)
      Whether a request using the given HTTP method may be retried.
      Parameters:
      method - the request method (case-insensitive); null/blank is treated as non-idempotent
    • isTransientFailure

      public static boolean isTransientFailure(HttpResponse response, Throwable throwable)
      Whether a completed attempt (response or throwable) should be retried. A non-null throwable is always transient; a non-null response is transient only when its status code is 502/503/504.
    • execute

      public static CompletableFuture<HttpResponse> execute(String method, int maxRetries, long backoffMillis, Supplier<CompletableFuture<HttpResponse>> attempt)
      Run attempt with retry. When maxRetries <= 0 or the method is non-idempotent the first attempt's future is returned unchanged. Otherwise a transient failure (exception or 502/503/504) triggers up to maxRetries further attempts with linear back-off; the future from the last attempt is returned once a non-transient result is produced or the retry budget is exhausted.
      Parameters:
      method - the request HTTP method (drives the idempotency check)
      maxRetries - maximum number of retries (0 disables retry)
      backoffMillis - base linear back-off between attempts in milliseconds
      attempt - supplies a fresh attempt future each time it is invoked
      Returns:
      a future completing with the final attempt's response