View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.impl;
29  
30  import java.io.IOException;
31  import java.io.InterruptedIOException;
32  import java.net.ConnectException;
33  import java.net.NoRouteToHostException;
34  import java.net.UnknownHostException;
35  import java.util.Arrays;
36  import java.util.Collection;
37  import java.util.Date;
38  import java.util.HashSet;
39  import java.util.Set;
40  
41  import javax.net.ssl.SSLException;
42  
43  import org.apache.hc.client5.http.HttpRequestRetryStrategy;
44  import org.apache.hc.client5.http.utils.DateUtils;
45  import org.apache.hc.core5.annotation.Contract;
46  import org.apache.hc.core5.annotation.ThreadingBehavior;
47  import org.apache.hc.core5.concurrent.CancellableDependency;
48  import org.apache.hc.core5.http.ConnectionClosedException;
49  import org.apache.hc.core5.http.Header;
50  import org.apache.hc.core5.http.HttpHeaders;
51  import org.apache.hc.core5.http.HttpRequest;
52  import org.apache.hc.core5.http.HttpResponse;
53  import org.apache.hc.core5.http.HttpStatus;
54  import org.apache.hc.core5.http.Method;
55  import org.apache.hc.core5.http.RequestNotExecutedException;
56  import org.apache.hc.core5.http.protocol.HttpContext;
57  import org.apache.hc.core5.util.Args;
58  import org.apache.hc.core5.util.TimeValue;
59  
60  /**
61   * Default implementation of the {@link HttpRequestRetryStrategy} interface.
62   *
63   * @since 5.0
64   */
65  @Contract(threading = ThreadingBehavior.STATELESS)
66  public class DefaultHttpRequestRetryStrategy implements HttpRequestRetryStrategy {
67  
68      public static final DefaultHttpRequestRetryStrategytRetryStrategy.html#DefaultHttpRequestRetryStrategy">DefaultHttpRequestRetryStrategy INSTANCE = new DefaultHttpRequestRetryStrategy();
69  
70      /**
71       * Maximum number of allowed retries
72       */
73      private final int maxRetries;
74  
75      /**
76       * Retry interval between subsequent retries
77       */
78      private final TimeValue defaultRetryInterval;
79  
80      /**
81       * Derived {@code IOExceptions} which shall not be retried
82       */
83      private final Set<Class<? extends IOException>> nonRetriableIOExceptionClasses;
84  
85      /**
86       * HTTP status codes which shall be retried
87       */
88      private final Set<Integer> retriableCodes;
89  
90      protected DefaultHttpRequestRetryStrategy(
91              final int maxRetries,
92              final TimeValue defaultRetryInterval,
93              final Collection<Class<? extends IOException>> clazzes,
94              final Collection<Integer> codes) {
95          Args.notNegative(maxRetries, "maxRetries");
96          Args.notNegative(defaultRetryInterval.getDuration(), "defaultRetryInterval");
97          this.maxRetries = maxRetries;
98          this.defaultRetryInterval = defaultRetryInterval;
99          this.nonRetriableIOExceptionClasses = new HashSet<>(clazzes);
100         this.retriableCodes = new HashSet<>(codes);
101     }
102 
103     /**
104      * Create the HTTP request retry strategy using the following list of
105      * non-retriable I/O exception classes:<br>
106      * <ul>
107      * <li>InterruptedIOException</li>
108      * <li>UnknownHostException</li>
109      * <li>ConnectException</li>
110      * <li>ConnectionClosedException</li>
111      * <li>NoRouteToHostException</li>
112      * <li>SSLException</li>
113      * </ul>
114      *
115      * and retriable HTTP status codes:<br>
116      * <ul>
117      * <li>SC_TOO_MANY_REQUESTS (429)</li>
118      * <li>SC_SERVICE_UNAVAILABLE (503)</li>
119      * </ul>
120      *
121      * @param maxRetries how many times to retry; 0 means no retries
122      * @param defaultRetryInterval the default retry interval between
123      * subsequent retries if the {@code Retry-After} header is not set
124      * or invalid.
125      */
126     public DefaultHttpRequestRetryStrategy(
127             final int maxRetries,
128             final TimeValue defaultRetryInterval) {
129         this(maxRetries, defaultRetryInterval,
130                 Arrays.asList(
131                         InterruptedIOException.class,
132                         UnknownHostException.class,
133                         ConnectException.class,
134                         ConnectionClosedException.class,
135                         NoRouteToHostException.class,
136                         SSLException.class),
137                 Arrays.asList(
138                         HttpStatus.SC_TOO_MANY_REQUESTS,
139                         HttpStatus.SC_SERVICE_UNAVAILABLE));
140     }
141 
142     /**
143      * Create the HTTP request retry strategy with a max retry count of 1,
144      * default retry interval of 1 second, and using the following list of
145      * non-retriable I/O exception classes:<br>
146      * <ul>
147      * <li>InterruptedIOException</li>
148      * <li>UnknownHostException</li>
149      * <li>ConnectException</li>
150      * <li>ConnectionClosedException</li>
151      * <li>SSLException</li>
152      * </ul>
153      *
154      * and retriable HTTP status codes:<br>
155      * <ul>
156      * <li>SC_TOO_MANY_REQUESTS (429)</li>
157      * <li>SC_SERVICE_UNAVAILABLE (503)</li>
158      * </ul>
159      */
160     public DefaultHttpRequestRetryStrategy() {
161         this(1, TimeValue.ofSeconds(1L));
162     }
163 
164     @Override
165     public boolean retryRequest(
166             final HttpRequest request,
167             final IOException exception,
168             final int execCount,
169             final HttpContext context) {
170         Args.notNull(request, "request");
171         Args.notNull(exception, "exception");
172 
173         if (execCount > this.maxRetries) {
174             // Do not retry if over max retries
175             return false;
176         }
177         if (this.nonRetriableIOExceptionClasses.contains(exception.getClass())) {
178             return false;
179         } else {
180             if (exception instanceof RequestNotExecutedException) {
181                 return true;
182             }
183             for (final Class<? extends IOException> rejectException : this.nonRetriableIOExceptionClasses) {
184                 if (rejectException.isInstance(exception)) {
185                     return false;
186                 }
187             }
188         }
189         if (request instanceof CancellableDependency && ((CancellableDependency) request).isCancelled()) {
190             return false;
191         }
192 
193         // Retry if the request is considered idempotent
194         return handleAsIdempotent(request);
195     }
196 
197     @Override
198     public boolean retryRequest(
199             final HttpResponse response,
200             final int execCount,
201             final HttpContext context) {
202         Args.notNull(response, "response");
203 
204         return execCount <= this.maxRetries && retriableCodes.contains(response.getCode());
205     }
206 
207     @Override
208     public TimeValue getRetryInterval(
209             final HttpResponse response,
210             final int execCount,
211             final HttpContext context) {
212         Args.notNull(response, "response");
213 
214         final Header header = response.getFirstHeader(HttpHeaders.RETRY_AFTER);
215         TimeValue retryAfter = null;
216         if (header != null) {
217             final String value = header.getValue();
218             try {
219                 retryAfter = TimeValue.ofSeconds(Long.parseLong(value));
220             } catch (final NumberFormatException ignore) {
221                 final Date retryAfterDate = DateUtils.parseDate(value);
222                 if (retryAfterDate != null) {
223                     retryAfter =
224                             TimeValue.ofMilliseconds(retryAfterDate.getTime() - System.currentTimeMillis());
225                 }
226             }
227 
228             if (TimeValue.isPositive(retryAfter)) {
229                 return retryAfter;
230             }
231         }
232         return this.defaultRetryInterval;
233     }
234 
235     protected boolean handleAsIdempotent(final HttpRequest request) {
236         return Method.isIdempotent(request.getMethod());
237     }
238 
239 }