com.github.rholder.retry.WaitStrategies Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guava-retrying Show documentation
Show all versions of guava-retrying Show documentation
This is a small extension to Google's Guava library to allow for the creation of configurable retrying strategies for an arbitrary function call, such as something that talks to a remote service with flaky uptime.
/*
* Copyright 2012-2013 Ray Holder
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.rholder.retry;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import com.google.common.base.Preconditions;
/**
* Factory class for instances of {@link WaitStrategy}.
* @author JB
*/
public final class WaitStrategies {
private static final WaitStrategy NO_WAIT_STRATEGY = new FixedWaitStrategy(0L);
private WaitStrategies() {
}
/**
* Returns a strategy that doesn't sleep at all before retrying.
*/
public static WaitStrategy noWait() {
return NO_WAIT_STRATEGY;
}
/**
* Returns a strategy that sleeps a fixed amount of time before retrying.
*
* @param sleepTime the time to sleep
* @param timeUnit the unit of the time to sleep
* @throws IllegalStateException if the sleep time is < 0.
*/
public static WaitStrategy fixedWait(long sleepTime, @Nonnull TimeUnit timeUnit) throws IllegalStateException {
Preconditions.checkNotNull(timeUnit, "The time unit may not be null");
return new FixedWaitStrategy(timeUnit.toMillis(sleepTime));
}
/**
* Returns a strategy that sleeps a random amount of time before retrying.
*
* @param maximumTime the maximum time to sleep
* @param timeUnit the unit of the maximum time
* @throws IllegalStateException if the maximum sleep time is <= 0.
*/
public static WaitStrategy randomWait(long maximumTime, @Nonnull TimeUnit timeUnit) {
Preconditions.checkNotNull(timeUnit, "The time unit may not be null");
return new RandomWaitStrategy(0L, timeUnit.toMillis(maximumTime));
}
/**
* Returns a strategy that sleeps a random amount of time before retrying.
*
* @param minimumTime the minimum time to sleep
* @param minimumTimeUnit the unit of the minimum time
* @param maximumTime the maximum time to sleep
* @param maximumTimeUnit the unit of the maximum time
* @throws IllegalStateException if the minimum sleep time is < 0, or if the
* maximum sleep time is less than (or equals to) the minimum.
*/
public static WaitStrategy randomWait(long minimumTime,
@Nonnull TimeUnit minimumTimeUnit,
long maximumTime,
@Nonnull TimeUnit maximumTimeUnit) {
Preconditions.checkNotNull(minimumTimeUnit, "The minimum time unit may not be null");
Preconditions.checkNotNull(maximumTimeUnit, "The maximum time unit may not be null");
return new RandomWaitStrategy(minimumTimeUnit.toMillis(minimumTime),
maximumTimeUnit.toMillis(maximumTime));
}
/**
* Returns a strategy that sleeps a fixed amount of time after the first
* failed attempt, and in incrementing the amounts of time after each
* failed attempt.
*
* @param initialSleepTime the time to sleep before retrying the first time
* @param initialSleepTimeUnit the unit of the initial sleep time
* @param increment the increment added to the previous sleep time after each failed attempt
* @param incrementTimeUnit the unit of the increment
*/
public static WaitStrategy incrementingWait(long initialSleepTime,
@Nonnull TimeUnit initialSleepTimeUnit,
long increment,
@Nonnull TimeUnit incrementTimeUnit) {
Preconditions.checkNotNull(initialSleepTimeUnit, "The initial sleep time unit may not be null");
Preconditions.checkNotNull(incrementTimeUnit, "The increment time unit may not be null");
return new IncrementingWaitStrategy(initialSleepTimeUnit.toMillis(initialSleepTime),
incrementTimeUnit.toMillis(increment));
}
/**
* Returns a strategy which sleeps for an exponential amount of time after the first failed attempt,
* and in exponentially incrementing amounts after each failed attempt up to Long.MAX_VALUE.
*/
public static WaitStrategy exponentialWait() {
return new ExponentialWaitStrategy(1, Long.MAX_VALUE);
}
/**
* Returns a strategy which sleeps for an exponential amount of time after the first failed attempt,
* and in exponentially incrementing amounts after each failed attempt up to the maximumTime.
*
* @param maximumTime the maximum time to sleep
* @param maximumTimeUnit the unit of the maximum time
*/
public static WaitStrategy exponentialWait(long maximumTime,
@Nonnull TimeUnit maximumTimeUnit) {
Preconditions.checkNotNull(maximumTimeUnit, "The maximum time unit may not be null");
return new ExponentialWaitStrategy(1, maximumTimeUnit.toMillis(maximumTime));
}
/**
* Returns a strategy which sleeps for an exponential amount of time after the first failed attempt,
* and in exponentially incrementing amounts after each failed attempt up to the maximumTime.
*
* @param multiplier multiply the wait time calculated by this
* @param maximumTime the maximum time to sleep
* @param maximumTimeUnit the unit of the maximum time
*/
public static WaitStrategy exponentialWait(long multiplier,
long maximumTime,
@Nonnull TimeUnit maximumTimeUnit) {
Preconditions.checkNotNull(maximumTimeUnit, "The maximum time unit may not be null");
return new ExponentialWaitStrategy(multiplier, maximumTimeUnit.toMillis(maximumTime));
}
@Immutable
private static final class FixedWaitStrategy implements WaitStrategy {
private final long sleepTime;
public FixedWaitStrategy(long sleepTime) {
Preconditions.checkArgument(sleepTime >= 0L, "sleepTime must be >= 0 but is %d", sleepTime);
this.sleepTime = sleepTime;
}
@Override
public long computeSleepTime(int previousAttemptNumber, long delaySinceFirstAttemptInMillis) {
return sleepTime;
}
}
@Immutable
private static final class RandomWaitStrategy implements WaitStrategy {
private static final Random RANDOM = new Random();
private final long minimum;
private final long maximum;
public RandomWaitStrategy(long minimum, long maximum) {
Preconditions.checkArgument(minimum >= 0, "minimum must be >= 0 but is %d", minimum);
Preconditions.checkArgument(maximum > minimum, "maximum must be > minimum but maximum is %d and minimum is", maximum, minimum);
this.minimum = minimum;
this.maximum = maximum;
}
@Override
public long computeSleepTime(int previousAttemptNumber, long delaySinceFirstAttemptInMillis) {
long t = Math.abs(RANDOM.nextLong()) % (maximum - minimum);
return t + minimum;
}
}
@Immutable
private static final class IncrementingWaitStrategy implements WaitStrategy {
private final long initialSleepTime;
private final long increment;
public IncrementingWaitStrategy(long initialSleepTime,
long increment) {
Preconditions.checkArgument(initialSleepTime >= 0L, "initialSleepTime must be >= 0 but is %d", initialSleepTime);
this.initialSleepTime = initialSleepTime;
this.increment = increment;
}
@Override
public long computeSleepTime(int previousAttemptNumber, long delaySinceFirstAttemptInMillis) {
long result = initialSleepTime + (increment * (previousAttemptNumber - 1));
return result >= 0L ? result : 0L;
}
}
@Immutable
private static final class ExponentialWaitStrategy implements WaitStrategy {
private final long multiplier;
private final long maximumWait;
public ExponentialWaitStrategy(long multiplier,
long maximumWait) {
Preconditions.checkArgument(multiplier > 0L, "multiplier must be > 0 but is %d", multiplier);
Preconditions.checkArgument(maximumWait >= 0L, "maximumWait must be >= 0 but is %d", maximumWait);
Preconditions.checkArgument(multiplier < maximumWait, "multiplier must be < maximumWait but is %d", multiplier);
this.multiplier = multiplier;
this.maximumWait = maximumWait;
}
@Override
public long computeSleepTime(int previousAttemptNumber, long delaySinceFirstAttemptInMillis) {
double exp = Math.pow(2, previousAttemptNumber);
long result = Math.round(multiplier * exp);
if(result > maximumWait) {
result = maximumWait;
}
return result >= 0L ? result : 0L;
}
}
}