Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
import org.apache.hc.client5.http.impl.DefaultRequestReExecutionStrategy;
import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel;
import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
import org.apache.hc.client5.testing.extension.async.TestAsyncClient;
Expand Down Expand Up @@ -81,7 +81,7 @@ void testGiveUpAfterOneRetry() throws Exception {
final HttpHost target = startServer();

configureClient(builder -> builder
.setRetryStrategy(new DefaultHttpRequestRetryStrategy(1, TimeValue.ofSeconds(1))));
.setReExecutionStrategy(new DefaultRequestReExecutionStrategy(1, TimeValue.ofSeconds(1))));
final TestAsyncClient client = startClient();

final Future<SimpleHttpResponse> future = client.execute(
Expand All @@ -100,7 +100,7 @@ void testDoNotGiveUpEasily() throws Exception {
final HttpHost target = startServer();

configureClient(builder -> builder
.setRetryStrategy(new DefaultHttpRequestRetryStrategy(5, TimeValue.ofSeconds(1))));
.setReExecutionStrategy(new DefaultRequestReExecutionStrategy(5, TimeValue.ofSeconds(1))));
final TestAsyncClient client = startClient();

final Future<SimpleHttpResponse> future = client.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Collection;

import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.RequestReExecutionStrategy;
import org.apache.hc.client5.http.UserTokenHandler;
import org.apache.hc.client5.http.auth.AuthSchemeFactory;
import org.apache.hc.client5.http.auth.CredentialsProvider;
Expand Down Expand Up @@ -146,8 +146,8 @@ public TestAsyncClientBuilder setDefaultHeaders(final Collection<? extends Heade
}

@Override
public TestAsyncClientBuilder setRetryStrategy(final HttpRequestRetryStrategy retryStrategy) {
this.clientBuilder.setRetryStrategy(retryStrategy);
public TestAsyncClientBuilder setReExecutionStrategy(final RequestReExecutionStrategy reExecutionStrategy) {
this.clientBuilder.setRequestReExecutionStrategy(reExecutionStrategy);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Collection;

import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.RequestReExecutionStrategy;
import org.apache.hc.client5.http.UserTokenHandler;
import org.apache.hc.client5.http.auth.AuthSchemeFactory;
import org.apache.hc.client5.http.auth.CredentialsProvider;
Expand Down Expand Up @@ -95,7 +95,7 @@ default TestAsyncClientBuilder setDefaultHeaders(Collection<? extends Header> de
throw new UnsupportedOperationException("Operation not supported by " + getProtocolLevel());
}

default TestAsyncClientBuilder setRetryStrategy(HttpRequestRetryStrategy retryStrategy) {
default TestAsyncClientBuilder setReExecutionStrategy(RequestReExecutionStrategy reExecutionStrategy) {
throw new UnsupportedOperationException("Operation not supported by " + getProtocolLevel());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Collection;

import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.RequestReExecutionStrategy;
import org.apache.hc.client5.http.UserTokenHandler;
import org.apache.hc.client5.http.auth.AuthSchemeFactory;
import org.apache.hc.client5.http.auth.CredentialsProvider;
Expand Down Expand Up @@ -119,8 +119,8 @@ public TestClientBuilder setDefaultHeaders(final Collection<? extends Header> de
}

@Override
public TestClientBuilder setRetryStrategy(final HttpRequestRetryStrategy retryStrategy) {
this.clientBuilder.setRetryStrategy(retryStrategy);
public TestClientBuilder setReExecutionStrategy(final RequestReExecutionStrategy reExecutionStrategy) {
this.clientBuilder.setRequestReExecutionStrategy(reExecutionStrategy);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Collection;

import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.RequestReExecutionStrategy;
import org.apache.hc.client5.http.UserTokenHandler;
import org.apache.hc.client5.http.auth.AuthSchemeFactory;
import org.apache.hc.client5.http.auth.CredentialsProvider;
Expand Down Expand Up @@ -77,7 +77,7 @@ default TestClientBuilder setDefaultHeaders(Collection<? extends Header> default
throw new UnsupportedOperationException("Operation not supported by " + getProtocolLevel());
}

default TestClientBuilder setRetryStrategy(HttpRequestRetryStrategy retryStrategy) {
default TestClientBuilder setReExecutionStrategy(RequestReExecutionStrategy reExecutionStrategy) {
throw new UnsupportedOperationException("Operation not supported by " + getProtocolLevel());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.RequestReExecutionStrategy;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
Expand Down Expand Up @@ -141,39 +142,31 @@ void testAutoGeneratedHeaders() throws Exception {

final HttpRequestInterceptor interceptor = (request, entityDetails, context) -> request.addHeader("my-header", "stuff");

final HttpRequestRetryStrategy requestRetryStrategy = new HttpRequestRetryStrategy() {
final RequestReExecutionStrategy requestRetryStrategy = new RequestReExecutionStrategy() {

@Override
public boolean retryRequest(
public Optional<TimeValue> reExecute(
final HttpRequest request,
final IOException exception,
final int executionCount,
final HttpContext context) {
return true;
return Optional.of(TimeValue.ZERO_MILLISECONDS);
}

@Override
public boolean retryRequest(
public Optional<TimeValue> reExecute(
final HttpResponse response,
final int executionCount,
final HttpContext context) {
return false;
}

@Override
public TimeValue getRetryInterval(
final HttpResponse response,
final int executionCount,
final HttpContext context) {
return TimeValue.ofSeconds(1L);
return Optional.empty();
}

};

configureClient(builder -> builder
.addRequestInterceptorFirst(interceptor)
.setRequestExecutor(new FaultyHttpRequestExecutor("Oppsie"))
.setRetryStrategy(requestRetryStrategy)
.setReExecutionStrategy(requestRetryStrategy)
);
final TestClient client = client();

Expand All @@ -199,38 +192,30 @@ void testNonRepeatableEntity() throws Exception {
configureServer(bootstrap -> bootstrap.register("*", new SimpleService()));
final HttpHost target = startServer();

final HttpRequestRetryStrategy requestRetryStrategy = new HttpRequestRetryStrategy() {
final RequestReExecutionStrategy requestRetryStrategy = new RequestReExecutionStrategy() {

@Override
public boolean retryRequest(
public Optional<TimeValue> reExecute(
final HttpRequest request,
final IOException exception,
final int executionCount,
final HttpContext context) {
return true;
}

@Override
public boolean retryRequest(
final HttpResponse response,
final int executionCount,
final HttpContext context) {
return false;
return Optional.of(TimeValue.ZERO_MILLISECONDS);
}

@Override
public TimeValue getRetryInterval(
public Optional<TimeValue> reExecute(
final HttpResponse response,
final int executionCount,
final HttpContext context) {
return TimeValue.ofSeconds(1L);
return Optional.empty();
}

};

configureClient(builder -> builder
.setRequestExecutor(new FaultyHttpRequestExecutor("a message showing that this failed"))
.setRetryStrategy(requestRetryStrategy)
.setReExecutionStrategy(requestRetryStrategy)
);
final TestClient client = client();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.concurrent.atomic.AtomicInteger;
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;

import org.apache.hc.client5.http.impl.DefaultRequestReExecutionStrategy;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.testing.classic.RandomHandler;
import org.apache.hc.client5.testing.classic.ServiceUnavailableDecorator;
Expand Down Expand Up @@ -79,7 +80,7 @@ void testGiveUpAfterOneRetry() throws Exception {
final HttpHost target = startServer();

configureClient(builder -> builder
.setRetryStrategy(new DefaultHttpRequestRetryStrategy(1, TimeValue.ofSeconds(1))));
.setReExecutionStrategy(new DefaultRequestReExecutionStrategy(1, TimeValue.ofSeconds(1))));
final TestClient client = client();

final HttpClientContext context = HttpClientContext.create();
Expand All @@ -100,7 +101,7 @@ void testDoNotGiveUpEasily() throws Exception {
final HttpHost target = startServer();

configureClient(builder -> builder
.setRetryStrategy(new DefaultHttpRequestRetryStrategy(5, TimeValue.ofSeconds(1))));
.setReExecutionStrategy(new DefaultRequestReExecutionStrategy(5, TimeValue.ofSeconds(1))));
final TestClient client = client();

final HttpClientContext context = HttpClientContext.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
import org.apache.hc.client5.http.impl.DefaultRequestReExecutionStrategy;
import org.apache.hc.client5.http.impl.LaxRedirectStrategy;
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
import org.apache.hc.client5.http.protocol.HttpClientContext;
Expand Down Expand Up @@ -655,7 +655,7 @@ public void handle(final ClassicHttpRequest request,
@Test
void testRetryUponRedirect() throws Exception {
configureClient(builder -> builder
.setRetryStrategy(new DefaultHttpRequestRetryStrategy(
.setReExecutionStrategy(new DefaultRequestReExecutionStrategy(
3,
TimeValue.ofSeconds(1),
Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package org.apache.hc.client5.http;

import java.io.IOException;
import java.util.Optional;

import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
Expand All @@ -42,8 +43,11 @@
* it should be done and so on.
*
* @since 5.0
*
* @deprecated Use {@link RequestReExecutionStrategy}.
*/
@Contract(threading = ThreadingBehavior.STATELESS)
@Deprecated
public interface HttpRequestRetryStrategy {

/**
Expand Down Expand Up @@ -103,4 +107,37 @@ default TimeValue getRetryInterval(HttpRequest request, IOException exception, i
*/
TimeValue getRetryInterval(HttpResponse response, int execCount, HttpContext context);

static RequestReExecutionStrategy adaptor(final HttpRequestRetryStrategy retryStrategy) {

return retryStrategy != null ? new RequestReExecutionStrategy() {

@Override
public Optional<TimeValue> reExecute(
final HttpRequest request,
final IOException exception,
final int execCount,
final HttpContext context) {
if (retryStrategy.retryRequest(request, exception, execCount, context)) {
return Optional.ofNullable(retryStrategy.getRetryInterval(request, exception, execCount, context));
} else {
return Optional.empty();
}
}

@Override
public Optional<TimeValue> reExecute(
final HttpResponse response,
final int execCount,
final HttpContext context) {
if (retryStrategy.retryRequest(response, execCount, context)) {
return Optional.ofNullable(retryStrategy.getRetryInterval(response, execCount, context));
} else {
return Optional.empty();
}
}

} : null;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.hc.client5.http;

import java.io.IOException;
import java.util.Optional;

import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.util.TimeValue;

/**
* Strategy interface to determine if the request should automatically reexecuted
* in case of an I/O error or a recoverable response status code.
*
* @since 5.6
*/
@Contract(threading = ThreadingBehavior.STATELESS)
public interface RequestReExecutionStrategy {

/**
* Determines if a method should be re-executed after an I/O exception
* occurred during execution.
*
* @param request the request failed due to an I/O exception
* @param exception the exception that occurred
* @param execCount the number of times this method has been
* unsuccessfully executed
* @param context the context for the request execution
*
* @return a time interval to pause before the request re-execution
* or {@link Optional#empty()} if the request is not to be re-executed
*/
Optional<TimeValue> reExecute(HttpRequest request, IOException exception, int execCount, HttpContext context);

/**
* Determines if a method should be re-executed given the response from
* the target server.
*
* @param response the response from the target server
* @param execCount the number of times this method has been
* unsuccessfully executed
* @param context the context for the request execution
*
* @return a time interval to pause before the request re-execution
* or {@link Optional#empty()} if the request is not to be re-executed
*/
Optional<TimeValue> reExecute(HttpResponse response, int execCount, HttpContext context);

}
Loading