failsafe-lib / failsafe

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

[Question] Is it possible to override the "maxRetries" and "delay" of retry policy at runtime? #385

Closed mjalvarez closed 2 months ago

mjalvarez commented 2 months ago

If I already created a retry policy of, say, maxRetries=3 and delay=300, would it be possible to override that config based on the exception encountered at runtime?

For example, a CustomRuntimeException with properties maxRetries=2 and delay=500 could override the current retry policy when thrown, such that the next attempt will be after 500ms, and that when every other Exception is encountered, the original retry policy will take effect.

Tembrel commented 2 months ago

You can't currently override maxRetries, but you can use FailsafeExecutor.get(ContextualSupplier<>) to make decisions about when to stop retrying based on execution Context..

You can use RetryPolicyBuilder.withDelayFn(ContextualSupplier<>) to decisions about how long to delay based on execution context.

mjalvarez commented 2 months ago

Gotcha. I'll play around with these alternatives. Maybe these are enough. Thanks @Tembrel ! 🙏