-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failsafe 3 #1416
Failsafe 3 #1416
Changes from all commits
1716824
ba34428
ff22533
dfb5edb
209568c
599f8cf
25aff4e
6bf111c
3b87deb
706e31d
d9be48c
3d52cb7
ec22ef5
7d8bc84
a6fc49f
46a1299
dca2189
2a577f8
d533d1d
2da55e6
7ff6773
76aa240
2bb5fe3
0f506a2
d5f54e6
f9c8706
93bc51a
43102e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
package org.zalando.riptide.failsafe; | ||
|
||
import lombok.AllArgsConstructor; | ||
import dev.failsafe.Policy; | ||
import dev.failsafe.PolicyConfig; | ||
import dev.failsafe.spi.PolicyExecutor; | ||
import lombok.Getter; | ||
import net.jodah.failsafe.AbstractExecution; | ||
import net.jodah.failsafe.Policy; | ||
import net.jodah.failsafe.PolicyExecutor; | ||
import org.apiguardian.api.API; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.apiguardian.api.API.Status.EXPERIMENTAL; | ||
|
||
@API(status = EXPERIMENTAL) | ||
@AllArgsConstructor | ||
@Getter | ||
public final class BackupRequest<R> implements Policy<R> { | ||
|
||
private final long delay; | ||
private final TimeUnit unit; | ||
private final PolicyConfig<R> config = new PolicyConfig<R>() { | ||
}; | ||
|
||
public BackupRequest(long delay, TimeUnit unit) { | ||
this.delay = delay; | ||
this.unit = unit; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public PolicyExecutor<Policy<R>> toExecutor( | ||
final AbstractExecution execution) { | ||
return (PolicyExecutor<Policy<R>>) create(execution); | ||
public PolicyExecutor<R> toExecutor(int policyIndex) { | ||
return create(policyIndex); | ||
} | ||
|
||
private PolicyExecutor<? extends Policy<R>> create( | ||
final AbstractExecution execution) { | ||
return new BackupRequestExecutor<R>(this, execution); | ||
private PolicyExecutor<R> create(int policyIndex) { | ||
return new BackupRequestExecutor<>(this, policyIndex); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.zalando.riptide.failsafe; | ||
|
||
import dev.failsafe.function.CheckedPredicate; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class CheckedPredicateConverter { | ||
|
||
private CheckedPredicateConverter() { | ||
} | ||
|
||
public static <T> CheckedPredicate<T> toCheckedPredicate(Predicate<T> predicate) { | ||
return predicate::test; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package org.zalando.riptide.failsafe; | ||
|
||
import dev.failsafe.function.ContextualSupplier; | ||
import lombok.AllArgsConstructor; | ||
import net.jodah.failsafe.ExecutionContext; | ||
import net.jodah.failsafe.function.DelayFunction; | ||
import dev.failsafe.ExecutionContext; | ||
import org.apiguardian.api.API; | ||
|
||
import java.time.Duration; | ||
|
@@ -14,23 +14,28 @@ | |
|
||
@API(status = EXPERIMENTAL) | ||
@AllArgsConstructor | ||
public final class CompositeDelayFunction<R, X extends Throwable> | ||
implements DelayFunction<R, X> { | ||
public final class CompositeDelayFunction<R> implements ContextualSupplier<R, Duration> { | ||
|
||
private final Collection<DelayFunction<R, X>> functions; | ||
private final Collection<ContextualSupplier<R, Duration>> functions; | ||
|
||
@Override | ||
public Duration computeDelay(final R result, final X failure, final ExecutionContext context) { | ||
public Duration get(final ExecutionContext<R> context) throws Throwable { | ||
return functions.stream() | ||
.map(function -> function.computeDelay(result, failure, context)) | ||
.map(function -> { | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that the method signature changed to throw instances of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return function.get(context); | ||
} catch (Throwable e) { | ||
throw new RuntimeException(e); | ||
} | ||
}) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
@SafeVarargs | ||
public static <R, X extends Throwable> DelayFunction<R, X> composite( | ||
final DelayFunction<R, X>... functions) { | ||
public static <R, X extends Throwable> ContextualSupplier<R, Duration> composite( | ||
final ContextualSupplier<R, Duration>... functions) { | ||
return new CompositeDelayFunction<>(Arrays.asList(functions)); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added for
Policy<R>
interface compatibility