View Javadoc

1   /*
2    * ====================================================================
3    *
4    *  Licensed to the Apache Software Foundation (ASF) under one or more
5    *  contributor license agreements.  See the NOTICE file distributed with
6    *  this work for additional information regarding copyright ownership.
7    *  The ASF licenses this file to You under the Apache License, Version 2.0
8    *  (the "License"); you may not use this file except in compliance with
9    *  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, software
14   *  distributed under the License is distributed on an "AS IS" BASIS,
15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *  See the License for the specific language governing permissions and
17   *  limitations under the License.
18   * ====================================================================
19   *
20   * This software consists of voluntary contributions made by many
21   * individuals on behalf of the Apache Software Foundation.  For more
22   * information on the Apache Software Foundation, please see
23   * <http://www.apache.org/>.
24   *
25   */
26  package org.apache.http.impl.client;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import org.apache.http.client.BackoffManager;
32  import org.apache.http.conn.routing.HttpRoute;
33  import org.apache.http.pool.ConnPoolControl;
34  
35  /**
36   * <p>The <code>AIMDBackoffManager</code> applies an additive increase,
37   * multiplicative decrease (AIMD) to managing a dynamic limit to
38   * the number of connections allowed to a given host. You may want
39   * to experiment with the settings for the cooldown periods and the
40   * backoff factor to get the adaptive behavior you want.</p>
41   *
42   * <p>Generally speaking, shorter cooldowns will lead to more steady-state
43   * variability but faster reaction times, while longer cooldowns
44   * will lead to more stable equilibrium behavior but slower reaction
45   * times.</p>
46   *
47   * <p>Similarly, higher backoff factors promote greater
48   * utilization of available capacity at the expense of fairness
49   * among clients. Lower backoff factors allow equal distribution of
50   * capacity among clients (fairness) to happen faster, at the
51   * expense of having more server capacity unused in the short term.</p>
52   *
53   * @since 4.2
54   */
55  public class AIMDBackoffManager implements BackoffManager {
56  
57      private final ConnPoolControl<HttpRoute> connPerRoute;
58      private final Clock clock;
59      private final Map<HttpRoute,Long> lastRouteProbes;
60      private final Map<HttpRoute,Long> lastRouteBackoffs;
61      private long coolDown = 5 * 1000L;
62      private double backoffFactor = 0.5;
63      private int cap = 2; // Per RFC 2616 sec 8.1.4
64  
65      /**
66       * Creates an <code>AIMDBackoffManager</code> to manage
67       * per-host connection pool sizes represented by the
68       * given {@link ConnPoolControl}.
69       * @param connPerRoute per-host routing maximums to
70       *   be managed
71       */
72      public AIMDBackoffManager(ConnPoolControl<HttpRoute> connPerRoute) {
73          this(connPerRoute, new SystemClock());
74      }
75  
76      AIMDBackoffManager(ConnPoolControl<HttpRoute> connPerRoute, Clock clock) {
77          this.clock = clock;
78          this.connPerRoute = connPerRoute;
79          this.lastRouteProbes = new HashMap<HttpRoute,Long>();
80          this.lastRouteBackoffs = new HashMap<HttpRoute,Long>();
81      }
82  
83      public void backOff(HttpRoute route) {
84          synchronized(connPerRoute) {
85              int curr = connPerRoute.getMaxPerRoute(route);
86              Long lastUpdate = getLastUpdate(lastRouteBackoffs, route);
87              long now = clock.getCurrentTime();
88              if (now - lastUpdate.longValue() < coolDown) return;
89              connPerRoute.setMaxPerRoute(route, getBackedOffPoolSize(curr));
90              lastRouteBackoffs.put(route, Long.valueOf(now));
91          }
92      }
93  
94      private int getBackedOffPoolSize(int curr) {
95          if (curr <= 1) return 1;
96          return (int)(Math.floor(backoffFactor * curr));
97      }
98  
99      public void probe(HttpRoute route) {
100         synchronized(connPerRoute) {
101             int curr = connPerRoute.getMaxPerRoute(route);
102             int max = (curr >= cap) ? cap : curr + 1;
103             Long lastProbe = getLastUpdate(lastRouteProbes, route);
104             Long lastBackoff = getLastUpdate(lastRouteBackoffs, route);
105             long now = clock.getCurrentTime();
106             if (now - lastProbe.longValue() < coolDown || now - lastBackoff.longValue() < coolDown)
107                 return;
108             connPerRoute.setMaxPerRoute(route, max);
109             lastRouteProbes.put(route, Long.valueOf(now));
110         }
111     }
112 
113     private Long getLastUpdate(Map<HttpRoute,Long> updates, HttpRoute route) {
114         Long lastUpdate = updates.get(route);
115         if (lastUpdate == null) lastUpdate = Long.valueOf(0L);
116         return lastUpdate;
117     }
118 
119     /**
120      * Sets the factor to use when backing off; the new
121      * per-host limit will be roughly the current max times
122      * this factor. <code>Math.floor</code> is applied in the
123      * case of non-integer outcomes to ensure we actually
124      * decrease the pool size. Pool sizes are never decreased
125      * below 1, however. Defaults to 0.5.
126      * @param d must be between 0.0 and 1.0, exclusive.
127      */
128     public void setBackoffFactor(double d) {
129         if (d <= 0.0 || d >= 1.0) {
130             throw new IllegalArgumentException("backoffFactor must be 0.0 < f < 1.0");
131         }
132         backoffFactor = d;
133     }
134 
135     /**
136      * Sets the amount of time, in milliseconds, to wait between
137      * adjustments in pool sizes for a given host, to allow
138      * enough time for the adjustments to take effect. Defaults
139      * to 5000L (5 seconds).
140      * @param l must be positive
141      */
142     public void setCooldownMillis(long l) {
143         if (coolDown <= 0) {
144             throw new IllegalArgumentException("cooldownMillis must be positive");
145         }
146         coolDown = l;
147     }
148 
149     /**
150      * Sets the absolute maximum per-host connection pool size to
151      * probe up to; defaults to 2 (the default per-host max).
152      * @param cap must be >= 1
153      */
154     public void setPerHostConnectionCap(int cap) {
155         if (cap < 1) {
156             throw new IllegalArgumentException("perHostConnectionCap must be >= 1");
157         }
158         this.cap = cap;
159     }
160 
161 }