|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.util; |
| 18 | + |
| 19 | +/** |
| 20 | + * Implementation of {@link BackOff} that increases the back off period for each |
| 21 | + * retry attempt. When the interval has reached the {@link #setMaxInterval(long) |
| 22 | + * max interval}, it is no longer increased. Stops retrying once the |
| 23 | + * {@link #setMaxElapsedTime(long) max elapsed time} has been reached. |
| 24 | + * |
| 25 | + * <p>Example: The default interval is {@value #DEFAULT_INITIAL_INTERVAL}ms, default |
| 26 | + * multiplier is {@value #DEFAULT_MULTIPLIER} and the default max interval is |
| 27 | + * {@value #DEFAULT_MAX_INTERVAL}. For 10 attempts the sequence will be |
| 28 | + * as follows: |
| 29 | + * |
| 30 | + * <pre> |
| 31 | + * request# back off |
| 32 | + * |
| 33 | + * 1 2000 |
| 34 | + * 2 3000 |
| 35 | + * 3 4500 |
| 36 | + * 4 6750 |
| 37 | + * 5 10125 |
| 38 | + * 6 15187 |
| 39 | + * 7 22780 |
| 40 | + * 8 30000 |
| 41 | + * 9 30000 |
| 42 | + * 10 30000 |
| 43 | + * </pre> |
| 44 | + * |
| 45 | + * Note that the default max elapsed time is {@link Long#MAX_VALUE}. Use |
| 46 | + * {@link #setMaxElapsedTime(long)} to limit the maximum number of time |
| 47 | + * that an instance should accumulate before returning {@link BackOff#STOP}. |
| 48 | + * |
| 49 | + * @author Stephane Nicoll |
| 50 | + * @since 4.1 |
| 51 | + */ |
| 52 | +public class ExponentialBackOff implements BackOff { |
| 53 | + |
| 54 | + /** |
| 55 | + * The default initial interval. |
| 56 | + */ |
| 57 | + public static final long DEFAULT_INITIAL_INTERVAL = 2000L; |
| 58 | + |
| 59 | + /** |
| 60 | + * The default multiplier (increases the interval by 50%). |
| 61 | + */ |
| 62 | + public static final double DEFAULT_MULTIPLIER = 1.5; |
| 63 | + |
| 64 | + /** |
| 65 | + * The default maximum back off time. |
| 66 | + */ |
| 67 | + public static final long DEFAULT_MAX_INTERVAL = 30000L; |
| 68 | + |
| 69 | + /** |
| 70 | + * The default maximum elapsed time. |
| 71 | + */ |
| 72 | + public static final long DEFAULT_MAX_ELAPSED_TIME = Long.MAX_VALUE; |
| 73 | + |
| 74 | + |
| 75 | + private long initialInterval = DEFAULT_INITIAL_INTERVAL; |
| 76 | + |
| 77 | + private double multiplier = DEFAULT_MULTIPLIER; |
| 78 | + |
| 79 | + private long maxInterval = DEFAULT_MAX_INTERVAL; |
| 80 | + |
| 81 | + private long maxElapsedTime = DEFAULT_MAX_ELAPSED_TIME; |
| 82 | + |
| 83 | + private long currentInterval = -1; |
| 84 | + |
| 85 | + private long currentElapsedTime = 0; |
| 86 | + |
| 87 | + /** |
| 88 | + * Create an instance with the default settings. |
| 89 | + * @see #DEFAULT_INITIAL_INTERVAL |
| 90 | + * @see #DEFAULT_MULTIPLIER |
| 91 | + * @see #DEFAULT_MAX_INTERVAL |
| 92 | + * @see #DEFAULT_MAX_ELAPSED_TIME |
| 93 | + */ |
| 94 | + public ExponentialBackOff() { |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Create an instance. |
| 99 | + * @param initialInterval the initial interval in milliseconds |
| 100 | + * @param multiplier the multiplier (should be equal or higher to 1) |
| 101 | + */ |
| 102 | + public ExponentialBackOff(long initialInterval, double multiplier) { |
| 103 | + checkMultiplier(multiplier); |
| 104 | + this.initialInterval = initialInterval; |
| 105 | + this.multiplier = multiplier; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * The initial interval in milliseconds. |
| 110 | + */ |
| 111 | + public void setInitialInterval(long initialInterval) { |
| 112 | + this.initialInterval = initialInterval; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * The value to multiply the current interval with for each retry attempt. |
| 117 | + */ |
| 118 | + public void setMultiplier(double multiplier) { |
| 119 | + checkMultiplier(multiplier); |
| 120 | + this.multiplier = multiplier; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * The maximum back off time. |
| 125 | + */ |
| 126 | + public void setMaxInterval(long maxInterval) { |
| 127 | + this.maxInterval = maxInterval; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * The maximum elapsed time in milliseconds after which a call to |
| 132 | + * {@link #nextBackOff()} returns {@link BackOff#STOP}. |
| 133 | + */ |
| 134 | + public void setMaxElapsedTime(long maxElapsedTime) { |
| 135 | + this.maxElapsedTime = maxElapsedTime; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public long nextBackOff() { |
| 140 | + if (currentElapsedTime >= maxElapsedTime) { |
| 141 | + return BackOff.STOP; |
| 142 | + } |
| 143 | + |
| 144 | + long nextInterval = computeNextInterval(); |
| 145 | + currentElapsedTime += nextInterval; |
| 146 | + return nextInterval; |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void reset() { |
| 152 | + this.currentInterval = -1; |
| 153 | + this.currentElapsedTime = 0; |
| 154 | + } |
| 155 | + |
| 156 | + private long computeNextInterval() { |
| 157 | + if (this.currentInterval >= this.maxInterval) { |
| 158 | + return this.maxInterval; |
| 159 | + } |
| 160 | + else if (this.currentInterval < 0) { |
| 161 | + this.currentInterval = (this.initialInterval < this.maxInterval |
| 162 | + ? this.initialInterval : this.maxInterval); |
| 163 | + } |
| 164 | + else { |
| 165 | + this.currentInterval = multiplyInterval(); |
| 166 | + } |
| 167 | + return currentInterval; |
| 168 | + } |
| 169 | + |
| 170 | + private long multiplyInterval() { |
| 171 | + long i = this.currentInterval; |
| 172 | + i *= this.multiplier; |
| 173 | + return (i > this.maxInterval ? this.maxInterval :i); |
| 174 | + } |
| 175 | + |
| 176 | + private void checkMultiplier(double multiplier) { |
| 177 | + if (multiplier < 1) { |
| 178 | + throw new IllegalArgumentException("Invalid multiplier '" + multiplier + "'. Should be equal" + |
| 179 | + "or higher than 1. A multiplier of 1 is equivalent to a fixed interval"); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public String toString() { |
| 185 | + String i = (this.currentInterval < 0 ? "n/a" : this.currentInterval + "ms"); |
| 186 | + final StringBuilder sb = new StringBuilder("ExponentialBackOff{"); |
| 187 | + sb.append("currentInterval=").append(i); |
| 188 | + sb.append(", multiplier=").append(this.multiplier); |
| 189 | + sb.append('}'); |
| 190 | + return sb.toString(); |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments