Package org.mockserver.mock.action.http
Class ForwardRetryPolicy
java.lang.Object
org.mockserver.mock.action.http.ForwardRetryPolicy
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 Summary
Modifier and TypeMethodDescriptionstatic CompletableFuture<HttpResponse>execute(String method, int maxRetries, long backoffMillis, Supplier<CompletableFuture<HttpResponse>> attempt) Runattemptwith retry.static booleanisIdempotent(String method) Whether a request using the given HTTP method may be retried.static booleanisTransientFailure(HttpResponse response, Throwable throwable) Whether a completed attempt (response or throwable) should be retried.
-
Method Details
-
isIdempotent
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
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) Runattemptwith retry. WhenmaxRetries <= 0or the method is non-idempotent the first attempt's future is returned unchanged. Otherwise a transient failure (exception or 502/503/504) triggers up tomaxRetriesfurther 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 millisecondsattempt- supplies a fresh attempt future each time it is invoked- Returns:
- a future completing with the final attempt's response
-