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;
29  
30  import java.io.IOException;
31  
32  import org.apache.hc.core5.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.http.HttpRequest;
35  import org.apache.hc.core5.http.HttpResponse;
36  import org.apache.hc.core5.http.protocol.HttpContext;
37  import org.apache.hc.core5.util.TimeValue;
38  
39  /**
40   * Strategy interface that allows API users to plug in their own logic to
41   * control whether or not a retry should automatically be done, how many times
42   * it should be done and so on.
43   *
44   * @since 5.0
45   */
46  @Contract(threading = ThreadingBehavior.STATELESS)
47  public interface HttpRequestRetryStrategy {
48  
49      /**
50       * Determines if a method should be retried after an I/O exception
51       * occurred during execution.
52       *
53       * @param request the request failed due to an I/O exception
54       * @param exception the exception that occurred
55       * @param execCount the number of times this method has been
56       *                  unsuccessfully executed
57       * @param context the context for the request execution
58       *
59       * @return {@code true} if the request should be retried, {@code false}
60       *         otherwise
61       */
62      boolean retryRequest(HttpRequest request, IOException exception, int execCount, HttpContext context);
63  
64      /**
65       * Determines if a method should be retried given the response from
66       * the target server.
67       *
68       * @param response the response from the target server
69       * @param execCount the number of times this method has been
70       *                  unsuccessfully executed
71       * @param context the context for the request execution
72       *
73       * @return {@code true} if the request should be retried, {@code false}
74       *         otherwise
75       */
76      boolean retryRequest(HttpResponse response, int execCount, HttpContext context);
77  
78  
79      /**
80       * Determines the retry interval between subsequent retries.
81       *
82       * @param request the request failed due to an I/O exception
83       * @param exception the exception that occurred
84       * @param execCount the number of times this method has been
85       *                  unsuccessfully executed
86       * @param context the context for the request execution
87       *
88       * @return the retry interval between subsequent retries
89       */
90      default TimeValue getRetryInterval(HttpRequest request, IOException exception, int execCount, HttpContext context) {
91          return TimeValue.ZERO_MILLISECONDS;
92      }
93  
94      /**
95       * Determines the retry interval between subsequent retries.
96       *
97       * @param response the response from the target server
98       * @param execCount the number of times this method has been
99       *                  unsuccessfully executed
100      * @param context the context for the request execution
101      *
102      * @return the retry interval between subsequent retries
103      */
104     TimeValue getRetryInterval(HttpResponse response, int execCount, HttpContext context);
105 
106 }