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.ThreadingBehavior;
39  import org.apache.hc.core5.http.ClassicHttpRequest;
40  import org.apache.hc.core5.http.ClassicHttpResponse;
41  import org.apache.hc.core5.http.HttpException;
42  import org.apache.hc.core5.util.Args;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  /**
47   * Request execution handler in the classic request execution chain
48   * that is responsible for execution of an {@link ConnectionBackoffStrategy}.
49   * <p>
50   * Further responsibilities such as communication with the opposite
51   * endpoint is delegated to the next executor in the request execution
52   * chain.
53   * </p>
54   *
55   * @since 4.3
56   */
57  @Contract(threading = ThreadingBehavior.STATELESS)
58  public final class BackoffStrategyExec implements ExecChainHandler {
59  
60      private static final Logger LOG = LoggerFactory.getLogger(BackoffStrategyExec.class);
61  
62      private final ConnectionBackoffStrategy connectionBackoffStrategy;
63      private final BackoffManager backoffManager;
64  
65      /**
66       * Constructs a {@code BackoffStrategyExec} with the specified
67       * {@link ConnectionBackoffStrategy} and {@link BackoffManager}.
68       *
69       * @param connectionBackoffStrategy the strategy to determine whether
70       *                                  to backoff based on the response or exception
71       * @param backoffManager            the manager responsible for applying backoff
72       *                                  and probing actions to the HTTP routes
73       */
74      public BackoffStrategyExec(
75              final ConnectionBackoffStrategy connectionBackoffStrategy,
76              final BackoffManager backoffManager) {
77          super();
78          Args.notNull(connectionBackoffStrategy, "Connection backoff strategy");
79          Args.notNull(backoffManager, "Backoff manager");
80          this.connectionBackoffStrategy = connectionBackoffStrategy;
81          this.backoffManager = backoffManager;
82      }
83  
84      @Override
85      public ClassicHttpResponse execute(
86              final ClassicHttpRequest request,
87              final ExecChain.Scope scope,
88              final ExecChain chain) throws IOException, HttpException {
89          Args.notNull(request, "HTTP request");
90          Args.notNull(scope, "Scope");
91          final HttpRoute route = scope.route;
92  
93          final ClassicHttpResponse response;
94          try {
95              response = chain.proceed(request, scope);
96          } catch (final IOException | HttpException ex) {
97              if (this.connectionBackoffStrategy.shouldBackoff(ex)) {
98                  if (LOG.isDebugEnabled()) {
99                      LOG.debug("Backing off route {} due to exception: {}", route, ex.getMessage());
100                 }
101                 this.backoffManager.backOff(route);
102             }
103             throw ex;
104         }
105         if (this.connectionBackoffStrategy.shouldBackoff(response)) {
106             if (LOG.isDebugEnabled()) {
107                 LOG.debug("Backing off route {} due to response status: {}", route, response.getCode());
108             }
109             this.backoffManager.backOff(route);
110         } else {
111             if (LOG.isDebugEnabled()) {
112                 LOG.debug("Probing route: {}", route);
113             }
114             this.backoffManager.probe(route);
115         }
116         return response;
117     }
118 
119 }