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 static final ExponentialBackOffSchedulingStrategy INSTANCE = new ExponentialBackOffSchedulingStrategy(
65              DEFAULT_BACK_OFF_RATE, DEFAULT_INITIAL_EXPIRY, DEFAULT_MAX_EXPIRY);
66  
67      private final long backOffRate;
68      private final TimeValue initialExpiry;
69      private final TimeValue maxExpiry;
70  
71      public ExponentialBackOffSchedulingStrategy(
72              final long backOffRate,
73              final TimeValue initialExpiry,
74              final TimeValue maxExpiry) {
75          this.backOffRate = Args.notNegative(backOffRate, "BackOff rate");
76          this.initialExpiry = Args.notNull(initialExpiry, "Initial expiry");
77          this.maxExpiry = Args.notNull(maxExpiry, "Max expiry");
78      }
79  
80      public ExponentialBackOffSchedulingStrategy(final long backOffRate, final TimeValue initialExpiry) {
81          this(backOffRate, initialExpiry, DEFAULT_MAX_EXPIRY);
82      }
83  
84      public ExponentialBackOffSchedulingStrategy(final long backOffRate) {
85          this(backOffRate, DEFAULT_INITIAL_EXPIRY, DEFAULT_MAX_EXPIRY);
86      }
87  
88      public ExponentialBackOffSchedulingStrategy() {
89          this(DEFAULT_BACK_OFF_RATE, DEFAULT_INITIAL_EXPIRY, DEFAULT_MAX_EXPIRY);
90      }
91  
92      @Override
93      public TimeValue schedule(final int attemptNumber) {
94          return calculateDelay(attemptNumber);
95      }
96  
97      public long getBackOffRate() {
98          return backOffRate;
99      }
100 
101     public TimeValue getInitialExpiry() {
102         return initialExpiry;
103     }
104 
105     public TimeValue getMaxExpiry() {
106         return maxExpiry;
107     }
108 
109     protected TimeValue calculateDelay(final int consecutiveFailedAttempts) {
110         if (consecutiveFailedAttempts > 0) {
111             final long delay = (long) (initialExpiry.toMilliseconds() * Math.pow(backOffRate, consecutiveFailedAttempts - 1));
112             return TimeValue.ofMilliseconds(Math.min(delay, maxExpiry.toMilliseconds()));
113         }
114         return TimeValue.ZERO_MILLISECONDS;
115     }
116 
117 }