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.classic;
29  
30  import java.io.IOException;
31  
32  import org.apache.hc.client5.http.HttpRoute;
33  import org.apache.hc.client5.http.classic.BackoffManager;
34  import org.apache.hc.client5.http.classic.ConnectionBackoffStrategy;
35  import org.apache.hc.client5.http.classic.ExecChain;
36  import org.apache.hc.client5.http.classic.ExecChainHandler;
37  import org.apache.hc.core5.annotation.Contract;
38  import org.apache.hc.core5.annotation.Experimental;
39  import org.apache.hc.core5.annotation.ThreadingBehavior;
40  import org.apache.hc.core5.http.ClassicHttpRequest;
41  import org.apache.hc.core5.http.ClassicHttpResponse;
42  import org.apache.hc.core5.http.HttpException;
43  import org.apache.hc.core5.util.Args;
44  
45  /**
46   * Request execution handler in the classic request execution chain
47   * that is responsible for execution of an {@link ConnectionBackoffStrategy}.
48   * <p>
49   * Further responsibilities such as communication with the opposite
50   * endpoint is delegated to the next executor in the request execution
51   * chain.
52   * </p>
53   *
54   * @since 4.3
55   */
56  @Contract(threading = ThreadingBehavior.STATELESS)
57  @Experimental
58  public final class BackoffStrategyExec implements ExecChainHandler {
59  
60      private final ConnectionBackoffStrategy connectionBackoffStrategy;
61      private final BackoffManager backoffManager;
62  
63      public BackoffStrategyExec(
64              final ConnectionBackoffStrategy connectionBackoffStrategy,
65              final BackoffManager backoffManager) {
66          super();
67          Args.notNull(connectionBackoffStrategy, "Connection backoff strategy");
68          Args.notNull(backoffManager, "Backoff manager");
69          this.connectionBackoffStrategy = connectionBackoffStrategy;
70          this.backoffManager = backoffManager;
71      }
72  
73      @Override
74      public ClassicHttpResponse execute(
75              final ClassicHttpRequest request,
76              final ExecChain.Scope scope,
77              final ExecChain chain) throws IOException, HttpException {
78          Args.notNull(request, "HTTP request");
79          Args.notNull(scope, "Scope");
80          final HttpRoute route = scope.route;
81  
82          final ClassicHttpResponse response;
83          try {
84              response = chain.proceed(request, scope);
85          } catch (final IOException | HttpException ex) {
86              if (this.connectionBackoffStrategy.shouldBackoff(ex)) {
87                  this.backoffManager.backOff(route);
88              }
89              throw ex;
90          }
91          if (this.connectionBackoffStrategy.shouldBackoff(response)) {
92              this.backoffManager.backOff(route);
93          } else {
94              this.backoffManager.probe(route);
95          }
96          return response;
97      }
98  
99  }