quarkusmutinyvert.x-webclientquarkus-reactive

How to record each throwable when using .onFailure().retry() with delay between retries


I need to record failure reason in metrics for each failed http call when using Vert.x WebClient. This compiles:

               .onFailure()
                   .retry()
                   .withBackOff(Duration.ofMillis(INITIAL_RETRY_DELAY_MS))
                   .until(retryTimeExpired(wrapper))

I'm recording metrics in retryTimeExpired method. But at runtime I get this:

Caused by: java.lang.IllegalArgumentException: Invalid retry configuration, `when` cannot be used with a back-off configuration
    at io.smallrye.mutiny.groups.UniRetry.when(UniRetry.java:156)
    at io.smallrye.mutiny.groups.UniRetry.until(UniRetry.java:137)

I could of course add sleep but this is reactive. It would be possible to block for a short time but I would hate to block the thread. Any ideas how to do this without sleep?


Solution

  • You could try using many sequential onFailures. As long as the first doesn't handle the exception (recoverWithItem, recoverWithNull, recoverWithUni) or throw its own the next should observe the same failure.

    service.apiMethod(key)
        .invoke(record -> logger.info("Succeeded to read record."))
        .onFailure()
            .invoke(exception -> logger.warn("Failed to read record."))
        .onFailure()
            .retry().withBackOff(delay).indefinitely();