user4165421
user4165421

Reputation: 131

Running into StackOverflowError when using retryWhen with WebFlux WebClient

I've attempted to create a webflux web client with built-in retry function for specific exceptions as for my use case, there are instances when the endpoint can be unavailable. With single failing requests the code seems to be working just fine, however under heavy, simulated load during the stress tests it ran into presumably infinite loop and threw StackOverflowError. This is the implementation of my function

private ExchangeFilterFunction retryFilterFunction(final Builder clientBuilder) {
  return (request, next) -> next.exchange(request)
    .retryWhen(globalRetryFunction(clientBuilder))
    .doOnError(throwable -> log.debug("Maximum retries exhausted for {}", clientBuilder.url));
}

Retry globalRetryFunction(final Builder clientBuilder) {
  return Retry.fixedDelay(clientBuilder.maxRetries, Duration.ofMillis(clientBuilder.delayBetweenRetries))
    .filter(throwable -> {
      final var exceptionCanonicalName = throwable.getClass().getCanonicalName();
      final var causeCanonicalName = throwable.getCause() != null
        ? throwable.getCause().getClass().getCanonicalName()
        : "";
      return webClientProperties.getExceptionsToRetry().contains(exceptionCanonicalName)
        || webClientProperties.getExceptionsToRetry().contains(causeCanonicalName)
        || clientBuilder.exceptionsToRetry.contains(exceptionCanonicalName)
        || clientBuilder.exceptionsToRetry.contains(causeCanonicalName);
    })
    .doBeforeRetry(retrySignal -> log.debug("Retrying for {} time", (retrySignal.totalRetries() + 1)));
}
 

and this is the stack trace that I get enter image description here

Does anyone have a clue whether or not it's even possible to create a web client definition for webflux with built-in retry?

I've also managed to spot, that my retry keeps calling itself recursively with an ever increasing amount of recursive calls. Any clues on how to fix it? enter image description here

Upvotes: 0

Views: 37

Answers (0)

Related Questions