RetryingClient extends AbstractRetryingClient<HttpRequest, HttpResponse> implements HttpClient { public static RetryingClientBuilder builder(RetryRule retryRule) { return new RetryingClientBuilder(retryRule); } RetryingClient(HttpClient delegate, RetryRule retryRule, int totalMaxAttempts,
                   long responseTimeoutMillisForEachAttempt, boolean useRetryAfter);   RetryingClient(HttpClient delegate,
                   RetryRuleWithContent<HttpResponse> retryRuleWithContent, int totalMaxAttempts,
                   long responseTimeoutMillisForEachAttempt, boolean useRetryAfter, int maxContentLength); static RetryingClientBuilder builder(RetryRule retryRule); static RetryingClientBuilder builder(RetryRuleWithContent<HttpResponse> retryRuleWithContent); static RetryingClientBuilder builder(RetryRuleWithContent<HttpResponse> retryRuleWithContent,
                                                int maxContentLength); static Function<? super HttpClient, RetryingClient> newDecorator(RetryRule retryRule); static Function<? super HttpClient, RetryingClient> newDecorator(RetryRuleWithContent<HttpResponse> retryRuleWithContent); static Function<? super HttpClient, RetryingClient> newDecorator(RetryRule retryRule, int maxTotalAttempts); static Function<? super HttpClient, RetryingClient> newDecorator(RetryRuleWithContent<HttpResponse> retryRuleWithContent, int maxTotalAttempts); static Function<? super HttpClient, RetryingClient> newDecorator(RetryRule retryRule, int maxTotalAttempts, long responseTimeoutMillisForEachAttempt); static Function<? super HttpClient, RetryingClient> newDecorator(RetryRuleWithContent<HttpResponse> retryRuleWithContent, int maxTotalAttempts,
                 long responseTimeoutMillisForEachAttempt);  }
@Test void retryWhenStatusMatched() { final WebClient client = client(RetryRule.builder().onServerErrorStatus().onException().thenBackoff()); final AggregatedHttpResponse res = client.get("/503-then-success").aggregate().join(); assertThat(res.contentUtf8()).isEqualTo("Succeeded after retry"); }
@Test void retryWhenStatusMatchedWithContent() { final WebClient client = client(RetryRuleWithContent.<HttpResponse>builder() .onServerErrorStatus() .onException() .thenBackoff(), 10000, 0, 100); final AggregatedHttpResponse res = client.get("/503-then-success").aggregate().join(); assertThat(res.contentUtf8()).isEqualTo("Succeeded after retry"); }
@Test void retryWhenTrailerMatched() { final WebClient client = client(RetryRule.builder() .onResponseTrailers((unused, trailers) -> { return trailers.getInt("grpc-status", -1) != 0; }) .thenBackoff()); final AggregatedHttpResponse res = client.get("/trailers-then-success").aggregate().join(); assertThat(res.contentUtf8()).isEqualTo("Succeeded after retry"); }
@Test void propagateLastResponseWhenNextRetryIsAfterTimeout() { final WebClient client = client(RetryRule.builder() .onServerErrorStatus() .onException() .thenBackoff(Backoff.fixed(10000000))); final AggregatedHttpResponse res = client.get("/service-unavailable").aggregate().join(); assertThat(res.status()).isSameAs(HttpStatus.SERVICE_UNAVAILABLE); }
@Test void propagateLastResponseWhenExceedMaxAttempts() { final WebClient client = client( RetryRule.builder().onServerErrorStatus().onException().thenBackoff(Backoff.fixed(1)), 0, 0, 3); final AggregatedHttpResponse res = client.get("/service-unavailable").aggregate().join(); assertThat(res.status()).isSameAs(HttpStatus.SERVICE_UNAVAILABLE); }
@Test void retryWithContentOnResponseTimeout() { final Backoff backoff = Backoff.fixed(100); final RetryRuleWithContent<HttpResponse> strategy = RetryRuleWithContent.<HttpResponse>onResponse((unused, response) -> { return response.aggregate().thenApply(unused0 -> false); }).orElse(RetryRuleWithContent.onResponse((unused, response) -> { return response.aggregate().thenApply(unused0 -> false); })).orElse(RetryRuleWithContent.<HttpResponse>onResponse((unused, response) -> { return response.aggregate().thenApply(unused0 -> false); }).orElse(RetryRule.builder() .onException(ResponseTimeoutException.class) .thenBackoff(backoff))); final WebClient client = client(strategy, 0, 500, 100); final AggregatedHttpResponse res = client.get("/1sleep-then-success").aggregate().join(); assertThat(res.contentUtf8()).isEqualTo("Succeeded after retry"); }
@Test void retryWithRequestBody() { final WebClient client = client(RetryRule.builder() .onServerErrorStatus() .onException() .thenBackoff(Backoff.fixed(10))); final AggregatedHttpResponse res = client.post("/post-ping-pong", "bar").aggregate().join(); assertThat(res.contentUtf8()).isEqualTo("bar"); }