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.time.Instant;
36  import java.util.Arrays;
37  import java.util.Collection;
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.protocol.HttpContext;
56  import org.apache.hc.core5.util.Args;
57  import org.apache.hc.core5.util.TimeValue;
58  
59  /**
60   * Default implementation of the {@link HttpRequestRetryStrategy} interface.
61   *
62   * @since 5.0
63   */
64  @Contract(threading = ThreadingBehavior.STATELESS)
65  public class DefaultHttpRequestRetryStrategy implements HttpRequestRetryStrategy {
66  
67      /**
68       * Default instance of {@link DefaultHttpRequestRetryStrategy}.
69       */
70      public static final DefaultHttpRequestRetryStrategy INSTANCE = new DefaultHttpRequestRetryStrategy();
71  
72      /**
73       * Maximum number of allowed retries
74       */
75      private final int maxRetries;
76  
77      /**
78       * Retry interval between subsequent retries
79       */
80      private final TimeValue defaultRetryInterval;
81  
82      /**
83       * Derived {@code IOExceptions} which shall not be retried
84       */
85      private final Set<Class<? extends IOException>> nonRetriableIOExceptionClasses;
86  
87      /**
88       * HTTP status codes which shall be retried
89       */
90      private final Set<Integer> retriableCodes;
91  
92      protected DefaultHttpRequestRetryStrategy(
93              final int maxRetries,
94              final TimeValue defaultRetryInterval,
95              final Collection<Class<? extends IOException>> clazzes,
96              final Collection<Integer> codes) {
97          Args.notNegative(maxRetries, "maxRetries");
98          Args.notNegative(defaultRetryInterval.getDuration(), "defaultRetryInterval");
99          this.maxRetries = maxRetries;
100         this.defaultRetryInterval = defaultRetryInterval;
101         this.nonRetriableIOExceptionClasses = new HashSet<>(clazzes);
102         this.retriableCodes = new HashSet<>(codes);
103     }
104 
105     /**
106      * Create the HTTP request retry strategy using the following list of
107      * non-retriable I/O exception classes:<br>
108      * <ul>
109      * <li>InterruptedIOException</li>
110      * <li>UnknownHostException</li>
111      * <li>ConnectException</li>
112      * <li>ConnectionClosedException</li>
113      * <li>NoRouteToHostException</li>
114      * <li>SSLException</li>
115      * </ul>
116      *
117      * and retriable HTTP status codes:<br>
118      * <ul>
119      * <li>SC_TOO_MANY_REQUESTS (429)</li>
120      * <li>SC_SERVICE_UNAVAILABLE (503)</li>
121      * </ul>
122      *
123      * @param maxRetries how many times to retry; 0 means no retries
124      * @param defaultRetryInterval the default retry interval between
125      * subsequent retries if the {@code Retry-After} header is not set
126      * or invalid.
127      */
128     public DefaultHttpRequestRetryStrategy(
129             final int maxRetries,
130             final TimeValue defaultRetryInterval) {
131         this(maxRetries, defaultRetryInterval,
132                 Arrays.asList(
133                         InterruptedIOException.class,
134                         UnknownHostException.class,
135                         ConnectException.class,
136                         ConnectionClosedException.class,
137                         NoRouteToHostException.class,
138                         SSLException.class),
139                 Arrays.asList(
140                         HttpStatus.SC_TOO_MANY_REQUESTS,
141                         HttpStatus.SC_SERVICE_UNAVAILABLE));
142     }
143 
144     /**
145      * Create the HTTP request retry strategy with a max retry count of 1,
146      * default retry interval of 1 second, and using the following list of
147      * non-retriable I/O exception classes:<br>
148      * <ul>
149      * <li>InterruptedIOException</li>
150      * <li>UnknownHostException</li>
151      * <li>ConnectException</li>
152      * <li>ConnectionClosedException</li>
153      * <li>SSLException</li>
154      * </ul>
155      *
156      * and retriable HTTP status codes:<br>
157      * <ul>
158      * <li>SC_TOO_MANY_REQUESTS (429)</li>
159      * <li>SC_SERVICE_UNAVAILABLE (503)</li>
160      * </ul>
161      */
162     public DefaultHttpRequestRetryStrategy() {
163         this(1, TimeValue.ofSeconds(1L));
164     }
165 
166     @Override
167     public boolean retryRequest(
168             final HttpRequest request,
169             final IOException exception,
170             final int execCount,
171             final HttpContext context) {
172         Args.notNull(request, "request");
173         Args.notNull(exception, "exception");
174 
175         if (execCount > this.maxRetries) {
176             // Do not retry if over max retries
177             return false;
178         }
179         if (this.nonRetriableIOExceptionClasses.contains(exception.getClass())) {
180             return false;
181         } else {
182             for (final Class<? extends IOException> rejectException : this.nonRetriableIOExceptionClasses) {
183                 if (rejectException.isInstance(exception)) {
184                     return false;
185                 }
186             }
187         }
188         if (request instanceof CancellableDependency && ((CancellableDependency) request).isCancelled()) {
189             return false;
190         }
191 
192         // Retry if the request is considered idempotent
193         return handleAsIdempotent(request);
194     }
195 
196     @Override
197     public boolean retryRequest(
198             final HttpResponse response,
199             final int execCount,
200             final HttpContext context) {
201         Args.notNull(response, "response");
202 
203         return execCount <= this.maxRetries && retriableCodes.contains(response.getCode());
204     }
205 
206     @Override
207     public TimeValue getRetryInterval(
208             final HttpResponse response,
209             final int execCount,
210             final HttpContext context) {
211         Args.notNull(response, "response");
212 
213         final Header header = response.getFirstHeader(HttpHeaders.RETRY_AFTER);
214         TimeValue retryAfter = null;
215         if (header != null) {
216             final String value = header.getValue();
217             try {
218                 retryAfter = TimeValue.ofSeconds(Long.parseLong(value));
219             } catch (final NumberFormatException ignore) {
220                 final Instant retryAfterDate = DateUtils.parseStandardDate(value);
221                 if (retryAfterDate != null) {
222                     retryAfter =
223                             TimeValue.ofMilliseconds(retryAfterDate.toEpochMilli() - System.currentTimeMillis());
224                 }
225             }
226 
227             if (TimeValue.isPositive(retryAfter)) {
228                 return retryAfter;
229             }
230         }
231         return this.defaultRetryInterval;
232     }
233 
234     protected boolean handleAsIdempotent(final HttpRequest request) {
235         return Method.isIdempotent(request.getMethod());
236     }
237 
238 }