question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Idea] Retry - throw on max retries reached

See original GitHub issue

Resilience4j version: 1.6.1

Java version: openjdk 15 2020-09-15

Here’s a feature suggestion which I would gladly open a PR for: Whenever the Retry logic reaches it’s configured maxAttempts, it would be nice for a configurability option to throw an exception, instead of returning the last result back to the caller.

I found it unintuitive to configure a retryOnResult, only to find out that after maxAttempt was reached, the result was still being returned. In my mind, if you consider a certain result an error to be retried, it doesn’t suddenly become a non-error just cause you ran out of retries 😃

This probably depends on the use-case though, but having some kind of throwOnCompletion or throwOnMaxFailedRetries configuration would be awesome.

Here’s my use case:

final Retry retry = Retry.of("clientCall", RetryConfig
    .<HttpResponse<String>>custom()
    .retryOnException(e -> e instanceof IOException)
    .retryOnResult(response -> response.statusCode() == 503)
    .build()
);

final HttpResponse<String> resp = retry.executeCheckedSupplier(() -> client.send(
    request, HttpResponse.BodyHandlers.ofString()
));

return resp.body();

resp would still be the last attempts response, even though retryOnResult would have kicked in.

My suggestion would be to config it up like:

final Retry retry = Retry.of("clientCall", RetryConfig
    .<HttpResponse<String>>custom()
    .retryOnException(e -> e instanceof IOException)
    .retryOnResult(response -> response.statusCode() == 503)
->  .throwOnMaxFailedRetries(response -> new RuntimeException("Call failed, retries exhausted. status=" + response.statusCode()))
    .build()
);

Which would make retry.executeCheckedSupplier() throw the configured exception, if the result still matches the retryPredicate.

What do you think about this?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
RobWincommented, Jan 7, 2021

Yes, throwing a MaxRetriesExceeded exception would be a breaking change. But keep in mind that the event consumer might run in a different thread than your function, if you would use a CompletableFuture. It’s more of a workaround than a solution 😦 I would prefer your initial proposal.

1reaction
RobWincommented, Jan 4, 2021

Hi, sry for the late response. We were on vacation.

I like the idea. But If you would like to provide a PR, please take care that the reactive modules like Reactor, RxJava2 and RxJava3 are also modified and tested.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Throw exception after reaching max attempts in resilience4j-retry
In my case when Response has other code than 200 I would like to throw exception. Retry retry = RetryRegistry.of( RetryConfig.<Response> custom ...
Read more >
ThrowExceptionForRetry - webMethods
raj, The throwExceptionForRetry works in conjunction from messages submitted via the broker to an IS service. The settings for the specific IS ...
Read more >
RE Framework MaxRetryNumber and Orchestrator Queue ...
Hi, I'm trying to get a clarification of these 2 numbers. My understanding from reading the documentations and crawling on the forum is ......
Read more >
Azure Functions error handling and retry guidance
Learn to handle errors and retry events in Azure Functions with links to ... completion occurs or the maximum number of retries is...
Read more >
Building resilient azure functions with retry policies - Medium
Not 100% related to retries, but an additional thing that needs to be considered is concurrency. When code is busy retrying or waiting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found