org.spf4j.failsafe.TimeoutRetryPredicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spf4j-core Show documentation
Show all versions of spf4j-core Show documentation
A continuously growing collection of utilities to measure performance, get better diagnostics,
improve performance, or do things more reliably, faster that other open source libraries...
/*
* Copyright 2017 SPF4J.
*
* 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 org.spf4j.failsafe;
import java.util.concurrent.Callable;
import org.spf4j.base.TimeSource;
import java.util.concurrent.TimeUnit;
import org.spf4j.base.Timing;
/**
* @author Zoltan Farkas
*/
final class TimeoutRetryPredicate> implements RetryPredicate {
private final RetryPredicate predicate;
private final long deadlineNanos;
TimeoutRetryPredicate(final RetryPredicate predicate, final long deadlineNanos) {
this.predicate = predicate;
this.deadlineNanos = deadlineNanos;
}
@Override
public RetryDecision getDecision(final T value, final C what) {
RetryDecision decision = predicate.getDecision(value, what);
if (decision.getDecisionType() == RetryDecision.Type.Retry) {
long timeToDeadlineNanos = TimeSource.getTimeToDeadline(deadlineNanos, TimeUnit.NANOSECONDS);
if (timeToDeadlineNanos < decision.getDelayNanos()) {
return (RetryDecision) RetryDecision.abortThrow(new NotEnoughTimeToRetry("Time to deadline not enough "
+ timeToDeadlineNanos + " ns, deadline = "
+ Timing.getCurrentTiming().fromNanoTimeToInstant(deadlineNanos) + ", last result = " + value));
}
}
return decision;
}
@Override
public RetryDecision getExceptionDecision(final Throwable value, final C what) {
RetryDecision decision = predicate.getExceptionDecision(value, what);
if (decision.getDecisionType() == RetryDecision.Type.Retry) {
long timeToDeadlineNanos = TimeSource.getTimeToDeadline(deadlineNanos, TimeUnit.NANOSECONDS);
if (timeToDeadlineNanos < decision.getDelayNanos()) {
return (RetryDecision) RetryDecision.abortThrow(new NotEnoughTimeToRetry("Time to deadline not enough "
+ timeToDeadlineNanos + " ns, deadline = "
+ Timing.getCurrentTiming().fromNanoTimeToInstant(deadlineNanos), value));
}
}
return decision;
}
@Override
public String toString() {
return "TimeoutRetryPredicate{" + "predicate=" + predicate + ", deadlineNanos=" + deadlineNanos + '}';
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy