xcapdevila commented on code in PR #3328: URL: https://github.com/apache/httpcomponents-client/pull/3328#discussion_r4776166112
########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/HttpRequestRetryStrategyAdaptor.java: ########## @@ -0,0 +1,69 @@ +/* + * ==================================================================== + * 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.impl; + +import java.io.IOException; +import java.util.Optional; + +import org.apache.hc.client5.http.RequestRetryStrategy; +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.Internal; +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; + +@Internal +@Contract(threading = ThreadingBehavior.STATELESS) +@SuppressWarnings("deprecation") +public class HttpRequestRetryStrategyAdaptor implements RequestRetryStrategy { + + private final org.apache.hc.client5.http.HttpRequestRetryStrategy strategy; + + public HttpRequestRetryStrategyAdaptor(final org.apache.hc.client5.http.HttpRequestRetryStrategy strategy) { + this.strategy = strategy; + } + + @Override + public Optional<TimeValue> retryRequest(final HttpRequest request, final IOException exception, final int execCount, final HttpContext context) { + if (strategy.retryRequest(request, exception, execCount, context)) { + return Optional.of(strategy.getRetryInterval(request, exception, execCount, context)); + } + return Optional.empty(); + } + + @Override + public Optional<TimeValue> retryRequest(final HttpRequest request, final HttpResponse response, final int execCount, final HttpContext context) { + if (strategy.retryRequest(response, execCount, context)) { + return Optional.of(strategy.getRetryInterval(response, execCount, context)); + } + return Optional.empty(); Review Comment: Thanks @ok2c! Guarding in `HttpRequestRetryExec` is even better since it covers any `RequestRetryStrategy` implementation. Just two small spots left in `d3dccf3109`: 1. `HttpRequestRetryExec` L159 (`IOException` branch) still uses `final TimeValue delay = retryPeriod.get();` without the `TimeValue.isPositive(...)` check added on L128 (unlike `AsyncHttpRequestRetryExec` L193). 2. `HttpRequestRetryStrategyAdaptor` (L56, L64) still calls `Optional.of(...)` directly, which would throw `NPE` before reaching the exec handler if a legacy `HttpRequestRetryStrategy#getRetryInterval` returns `null` (the suggestion above on the adaptor still applies if you want to guard that too). ########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultRequestRetryStrategy.java: ########## @@ -0,0 +1,243 @@ +/* + * ==================================================================== + * 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.impl; + +import java.io.IOException; +import java.io.InterruptedIOException; +import java.net.ConnectException; +import java.net.NoRouteToHostException; +import java.net.UnknownHostException; +import java.time.Instant; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import javax.net.ssl.SSLException; + +import org.apache.hc.client5.http.RequestRetryStrategy; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.protocol.HttpClientContext; +import org.apache.hc.client5.http.utils.DateUtils; +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.ThreadingBehavior; +import org.apache.hc.core5.concurrent.CancellableDependency; +import org.apache.hc.core5.http.ConnectionClosedException; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.Method; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.apache.hc.core5.util.Args; +import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; + +/** + * Default implementation of the {@link RequestRetryStrategy} interface. + * + * @since 5.7 + */ +@Contract(threading = ThreadingBehavior.STATELESS) +public class DefaultRequestRetryStrategy implements RequestRetryStrategy { + + /** + * Default instance of {@link DefaultRequestRetryStrategy}. + */ + public static final DefaultRequestRetryStrategy INSTANCE = new DefaultRequestRetryStrategy(1, TimeValue.ofSeconds(1)); + + private static final List<Class<? extends IOException>> NON_RETRIABLE_ERRORS_LIST = + Arrays.asList( + InterruptedIOException.class, + UnknownHostException.class, + ConnectException.class, + ConnectionClosedException.class, + NoRouteToHostException.class, + SSLException.class); + private static final Set<Class<? extends IOException>> NON_RETRIABLE_ERRORS_SET = + new HashSet<>(NON_RETRIABLE_ERRORS_LIST); + + /** + * Maximum number of allowed retries + */ + private final int maxRetries; + + /** + * Retry interval between subsequent retries + */ + private final TimeValue defaultRetryInterval; + + /** + * Create the HTTP request retry strategy using the following list of + * non-retriable exception:<br> + * <ul> + * <li>{@link InterruptedIOException}</li> + * <li>{@link UnknownHostException}</li> + * <li>{@link ConnectException}</li> + * <li>{@link ConnectionClosedException}</li> + * <li>{@link NoRouteToHostException}</li> + * <li>{@link SSLException}</li> + * </ul> + * and retriable HTTP status codes:<br> + * <ul> + * <li>SC_TOO_MANY_REQUESTS (429)</li> + * <li>SC_SERVICE_UNAVAILABLE (503)</li> + * </ul> + * @param maxRetries how many times to retry; 0 means no retries + * @param defaultRetryInterval the default retry interval between + * subsequent retries if the {@code Retry-After} header is not set + * or invalid. + */ + public DefaultRequestRetryStrategy( + final int maxRetries, + final TimeValue defaultRetryInterval) { + Args.notNegative(maxRetries, "maxRetries"); + Args.notNull(defaultRetryInterval, "defaultRetryInterval"); + Args.check(TimeValue.isNonNegative(defaultRetryInterval), "Default retry interval is negative"); + this.maxRetries = maxRetries; + this.defaultRetryInterval = defaultRetryInterval; + } + + @Override + public Optional<TimeValue> retryRequest( + final HttpRequest request, + final IOException exception, + final int execCount, + final HttpContext context) { + Args.notNull(request, "request"); + Args.notNull(exception, "exception"); + + if (execCount > this.maxRetries) { + // Do not retry if over max retries + return Optional.empty(); + } + if (request instanceof CancellableDependency && ((CancellableDependency) request).isCancelled()) { + return Optional.empty(); + } + if (handleAsRetriable(exception) && handleAsIdempotent(request)) { + return Optional.of(defaultRetryInterval); + } Review Comment: Fair point on both counts @ok2c — keeping `responseTimeout` strictly as a guard against untrusted server-supplied `Retry-After` values, and defaulting to `1 s` in `DefaultRequestRetryStrategy.INSTANCE`, makes sense for 5.7. One small API ergonomics suggestion while `DefaultRequestRetryStrategy` is still new: since a single `defaultRetryInterval` now governs both `IOException` retries and headerless `429`/`503` responses, callers who still want `0 ms` on I/O retries (e.g. classic HTTP/1.1 clients hitting `NoHttpResponseException` on half-closed pooled connections reused within the `2 s` `validateAfterInactivity` window) cannot pass `TimeValue.ZERO_MILLISECONDS` without also making headerless `429`/`503` retry at `0 ms`. Would you consider adding an overload `DefaultRequestRetryStrategy(int maxRetries, TimeValue defaultIoRetryInterval, TimeValue defaultResponseRetryInterval)` — plus a default no-arg `public DefaultRequestRetryStrategy()` like `DefaultHttpRequestRetryStrategy` had for subclasses? Totally your call either way, the PR looks great! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
