mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
Remove the reduntant 'afterFinalFailure' from Retrier
'afterFinalFailure' is called just before rethrowing a non-retrying error from the retrier. This can happen either because the exception shouldn't be retried, or because we exceeded the maximum number of retries. The same thing can be done by catching that thrown error outside of the retrier: retrier.callWithRetry( callable, new FailureReporter() { @Override void afterFinalFailure(Throwable thrown, int failures) { // do something with thrown } }, RetriableException.class); is (almost) the same as: try { retrier.callWithRetry(callable, RetriableException.class); } catch (Throwable thrown) { // do something with thrown throw thrown; } ("almost" because the retrier might wrap the Throwable in a RuntimeException, so you might need to getCause or getRootCause. Also - there is the "beforeRetry" I ignored for the example) Removing "afterFinalFailure" also makes the FailureReporter in line with Java 8 functional interface - meaning we can more easily create it when we do need to override "beforeRetry". ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=189972101
This commit is contained in:
parent
63785e5149
commit
552940a816
6 changed files with 135 additions and 177 deletions
|
@ -49,16 +49,6 @@ public class Retrier implements Serializable {
|
|||
* <p>Not called at all if the retrier succeeded on its first attempt.
|
||||
*/
|
||||
void beforeRetry(Throwable thrown, int failures, int maxAttempts);
|
||||
|
||||
/**
|
||||
* Called after a a non-retriable error.
|
||||
*
|
||||
* <p>Called either after the final failure, or if the Throwable thrown isn't "a retriable
|
||||
* error". The retrier throws right after calling this function.
|
||||
*
|
||||
* <p>Not called at all if the retrier succeeds.
|
||||
*/
|
||||
void afterFinalFailure(Throwable thrown, int failures);
|
||||
}
|
||||
|
||||
@Inject
|
||||
|
@ -89,7 +79,6 @@ public class Retrier implements Serializable {
|
|||
return callable.call();
|
||||
} catch (Throwable e) {
|
||||
if (++failures == attempts || !isRetryable.test(e)) {
|
||||
failureReporter.afterFinalFailure(e, failures);
|
||||
throwIfUnchecked(e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
@ -180,13 +169,6 @@ public class Retrier implements Serializable {
|
|||
}
|
||||
|
||||
private static final FailureReporter LOGGING_FAILURE_REPORTER =
|
||||
new FailureReporter() {
|
||||
@Override
|
||||
public void beforeRetry(Throwable thrown, int failures, int maxAttempts) {
|
||||
logger.infofmt(thrown, "Retrying transient error, attempt %d", failures);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterFinalFailure(Throwable thrown, int failures) {}
|
||||
};
|
||||
(thrown, failures, maxAttempts) ->
|
||||
logger.infofmt(thrown, "Retrying transient error, attempt %d/%d", failures, maxAttempts);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue