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  package org.apache.hc.client5.http.impl.schedule;
28  
29  import org.apache.hc.client5.http.schedule.SchedulingStrategy;
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  import org.apache.hc.core5.util.Args;
33  import org.apache.hc.core5.util.TimeValue;
34  
35  /**
36   * An implementation that backs off exponentially based on the number of
37   * consecutive failed attempts. It uses the following defaults:
38   * <pre>
39   *      no delay in case it was never tried or didn't fail so far
40   *     6 s delay for one failed attempt (= {@link #getInitialExpiry()})
41   *    60 s delay for two failed attempts
42   *  10 min delay for three failed attempts
43   * 100 min delay for four failed attempts
44   *   ~16 h delay for five failed attempts
45   *    24 h delay for six or more failed attempts (= {@link #getMaxExpiry()})
46   * </pre>
47   *
48   * The following equation is used to calculate the delay for a specific pending operation:
49   * <pre>
50   *     delay = {@link #getInitialExpiry()} * Math.pow({@link #getBackOffRate()},
51   *     {@code consecutiveFailedAttempts} - 1))
52   * </pre>
53   * The resulting delay won't exceed {@link #getMaxExpiry()}.
54   *
55   * @since 5.0
56   */
57  @Contract(threading = ThreadingBehavior.STATELESS)
58  public class ExponentialBackOffSchedulingStrategy implements SchedulingStrategy {
59  
60      public static final long DEFAULT_BACK_OFF_RATE = 10;
61      public static final TimeValue DEFAULT_INITIAL_EXPIRY = TimeValue.ofSeconds(6);
62      public static final TimeValue DEFAULT_MAX_EXPIRY = TimeValue.ofSeconds(86400);
63  
64      private final long backOffRate;
65      private final TimeValue initialExpiry;
66      private final TimeValue maxExpiry;
67  
68      public ExponentialBackOffSchedulingStrategy(
69              final long backOffRate,
70              final TimeValue initialExpiry,
71              final TimeValue maxExpiry) {
72          this.backOffRate = Args.notNegative(backOffRate, "BackOff rate");
73          this.initialExpiry = Args.notNull(initialExpiry, "Initial expiry");
74          this.maxExpiry = Args.notNull(maxExpiry, "Max expiry");
75      }
76  
77      public ExponentialBackOffSchedulingStrategy(final long backOffRate, final TimeValue initialExpiry) {
78          this(backOffRate, initialExpiry, DEFAULT_MAX_EXPIRY);
79      }
80  
81      public ExponentialBackOffSchedulingStrategy(final long backOffRate) {
82          this(backOffRate, DEFAULT_INITIAL_EXPIRY, DEFAULT_MAX_EXPIRY);
83      }
84  
85      public ExponentialBackOffSchedulingStrategy() {
86          this(DEFAULT_BACK_OFF_RATE, DEFAULT_INITIAL_EXPIRY, DEFAULT_MAX_EXPIRY);
87      }
88  
89      @Override
90      public TimeValue schedule(final int attemptNumber) {
91          return calculateDelay(attemptNumber);
92      }
93  
94      public long getBackOffRate() {
95          return backOffRate;
96      }
97  
98      public TimeValue getInitialExpiry() {
99          return initialExpiry;
100     }
101 
102     public TimeValue getMaxExpiry() {
103         return maxExpiry;
104     }
105 
106     protected TimeValue calculateDelay(final int consecutiveFailedAttempts) {
107         if (consecutiveFailedAttempts > 0) {
108             final long delay = (long) (initialExpiry.toMilliseconds() * Math.pow(backOffRate, consecutiveFailedAttempts - 1));
109             return TimeValue.ofMilliseconds(Math.min(delay, maxExpiry.toMilliseconds()));
110         }
111         return TimeValue.ZERO_MILLISECONDS;
112     }
113 
114 }