failsafe icon indicating copy to clipboard operation
failsafe copied to clipboard

[Question]Can withBackoff and withDelay be used at the same time?

Open ripley opened this issue 2 years ago • 2 comments

Say if I want to apply retry delay by increasing the retry interval exponentially for most cases, but for some specific exceptions like client error 429 I would like to calculate the delay by myself, is that possible by setting delay withBackoff and withDelay at the same time?

It appears this is not supported since delay will be overwritten but it'll be good if this can be confirmed.

Thanks for your attention.

ripley avatar Oct 23 '23 09:10 ripley

You can't use both withDelay and withBackoff in the same RetryPolicy, since they use the same configuration fields internally.

But you can use a backoff delay in conjunction with a computed delay, e.g., (uncompiled, untested):

RetryPolicy retryPolicy = RetryPolicy.builder()
    // other stuff
    .withBackoff(...)
    .withDelayFn(context -> is429OrOtherSpecial(context) ? computeCustomDelay(context) : -1)
    .build();

I'm basing that idea on this Javadoc:

A negative return value will cause Failsafe to use a configured fixed or backoff delay

Tembrel avatar Oct 23 '23 14:10 Tembrel

Thanks @Tembrel 👍 This works as expected.

ripley avatar Oct 25 '23 08:10 ripley