guava-retrying icon indicating copy to clipboard operation
guava-retrying copied to clipboard

Initial wait time in an exponential retry strategy.

Open jcranston opened this issue 6 years ago • 3 comments

Is there a way to initialize the wait strategy to be a fixed amount of time? As in, all successive exponential wait times are added onto a fixed wait time. For example, the computeSleepTime for the exponential wait strategy which is currently

@Override
public long computeSleepTime(Attempt failedAttempt) {
    double exp = Math.pow(2, failedAttempt.getAttemptNumber());
    long result = Math.round(multiplier * exp);
    if (result > maximumWait) {
        result = maximumWait;
    }
    return result >= 0L ? result : 0L;
}

can return something like initialWait + (result >= 0L ? result : 0L)?

Perhaps this can already be accomplished in another way?

jcranston avatar Jun 15 '18 17:06 jcranston

I think the closest option is this method in WaitStrategies:

    public static WaitStrategy exponentialWait(long multiplier,
                                               long maximumTime,
                                               @Nonnull TimeUnit maximumTimeUnit)

The exponential wait is normally in milliseconds. If you know your operation typically takes, say, 800 ms, then you would do something like this:

WaitStrategy = exponentialWait(1000, 5, TimeUnit.MINUTES);

That way your waits would be in seconds instead of milliseconds.

rhuffman avatar Jun 17 '18 16:06 rhuffman

Ah, interesting. Upon digging, I'm assuming it would also suffice to use the CompositeWaitStrategy as well, with a ExponentialWaitStrategy and FixedWaitStrategy.

jcranston avatar Jun 19 '18 15:06 jcranston

It is inconvenient. It is logical to use fixed start time and maximum time. For example, when I have a disconnection issue I want to retry after 5 seconds and exponentially increase every next attempt. Why do I need to calculate time to wait?

valera7979 avatar Dec 06 '19 10:12 valera7979