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