com.github.robozonky.common.async.Backoff Maven / Gradle / Ivy
/*
* Copyright 2019 The RoboZonky Project
*
* 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.robozonky.common.async;
import java.time.Duration;
import java.time.Instant;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import com.github.robozonky.internal.test.DateUtil;
import io.vavr.control.Either;
import io.vavr.control.Try;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Backoff implements Supplier> {
private static final Logger LOGGER = LogManager.getLogger(Backoff.class);
private final Supplier operation;
private final BackoffTimeCalculator backoffTimeCalculator;
private final Duration cancelAfter;
private Backoff(final Supplier operation, final BackoffTimeCalculator backoffTimeCalculator,
final Duration cancelAfter) {
this.operation = operation;
this.backoffTimeCalculator = backoffTimeCalculator;
this.cancelAfter = cancelAfter;
}
static Duration calculateBackoffTime(final Duration original, final Duration initial) {
if (Objects.equals(original, Duration.ZERO)) {
return initial;
} else {
return Duration.ofNanos(original.toNanos() * 2);
}
}
/**
* Implements exponential backoff over a given operation.
* @param operation Operation to execute. Null is not a permitted return value of the {@link Supplier}.
* @param initialBackoffTime The minimal non-zero value of the backoff time to start the back-off with following up
* on failed execution of the operation.
* @param cancelAfter When the total time spent within the algorithm exceeds this, it will be terminated.
* @param Return type of the operation.
* @return Return value of the operation, or empty if not reached in time.
* @see Exponential backoff on Wikipedia
*/
public static Backoff exponential(final Supplier operation, final Duration initialBackoffTime,
final Duration cancelAfter) {
final BackoffTimeCalculator exponential = (originalBackoffTime) -> calculateBackoffTime(originalBackoffTime,
initialBackoffTime);
return new Backoff<>(operation, exponential, cancelAfter);
}
private static void wait(final Duration duration) {
LOGGER.debug("Will wait for {} milliseconds.", duration.toMillis());
try {
Thread.sleep(duration.toMillis());
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
LOGGER.debug("Wait interrupted.", ex);
}
}
private static Either execute(final Supplier operation) {
LOGGER.trace("Will execute {}.", operation);
return Try.ofSupplier(operation).map(Either::right)
.recover(t -> {
LOGGER.debug("Operation failed.", t);
return Either.left(t);
}).get();
}
@Override
public Either get() {
Duration backoffTime = Duration.ZERO;
final Instant startedOn = DateUtil.now();
do {
wait(backoffTime);
final Either result = execute(operation);
if (result.isRight()) {
LOGGER.trace("Success.");
return Either.right(result.get());
} else if (startedOn.plus(cancelAfter).isBefore(DateUtil.now())) {
LOGGER.trace("Expired.");
return Either.left(result.getLeft());
}
// need to try again
backoffTime = backoffTimeCalculator.apply(backoffTime);
} while (true);
}
@FunctionalInterface
private interface BackoffTimeCalculator extends UnaryOperator {
/**
* Calculate new backoff time based on the previous backoff time.
* @param duration The last back-off time. Will equal {@link Duration#ZERO} if this was the first run.
* @return New back off time.
*/
@Override
Duration apply(Duration duration);
}
}