failsafe-lib / failsafe

Fault tolerance and resilience patterns for the JVM
https://failsafe.dev
Apache License 2.0
4.17k stars 296 forks source link

Add Ability to RetryPolicy to Ignore a Failure #332

Closed numeralnathan closed 2 years ago

numeralnathan commented 2 years ago

Please add to RetryPolicy a mechanism to essentially ignore a failure. For example, if an HTTP response code is 429 Too Many Requests, then I would like to delay and then try again but not have it count against the number of attempts.

Tembrel commented 2 years ago

Wouldn't that just be two nested RetryPolicy instances? The inner one would only retry on 429, treating anything else (actual success or non-429 error) as "success", and the outer one would be your original retry handler. The failure count on the outer would be the one you'd use. It wouldn't count the 429-triggered retries.

jhalterman commented 2 years ago

@numeralnathan What's the significance of not incrementing the number of attempts, for your use case?

numeralnathan commented 2 years ago

@Tembrel This is a great idea! The inner RetryPolicy could have withMaxRetries(Integer.MAX_VALUE).

numeralnathan commented 2 years ago

@jhalterman Are you saying that on failure, I could increment the policy's attempt count?

Tembrel commented 2 years ago

Sure, though wouldn't you want some practical limit on the number of retries? After the billionth 429, you might want to consider packing it in. 😀

jhalterman commented 2 years ago

@numeralnathan No, you won't be able to influence that, I was just curious about your use case, and why having the attempt count not increment sometimes is important.

numeralnathan commented 2 years ago

@Tembrel Yeah, typically one would give up. In this case, this is the only thing for the program to do. So, waiting and trying again is appropriate for an infinite number of calls. On a subsequent request, the server will delay processing the request and finally respond with the data.