1

I want to be able to set a timeout value for requests made with Spring 5 WebClient (Spring Boot version 2.0.0.RELEASE). My first attempt was to configure the WebClient as proposed on this answer: Spring 5 webflux how to set a timeout on Webclient. This correctly times out if the server does not respond in time. But this also has other implication: after a WebClient successful emission of a response (after .retrive() or .exchange()) the timer keeps going on, such that if the timeout value is reached then a timeout exception is raised.

It seems that this is the intended behavior as the ReadTimeoutHandler timeouts after the configured period of time has elapsed and no data was read by the netty channel (disregarding previously read data). Putting in other words: you call an external http service with WebClient (having configured the ReadTimeoutHandler) then the working handler does some job that possibly takes more time than the timeout value so.... io.netty.handler.timeout.ReadTimeoutException is thrown.

So... to the point of my question: how to correctly set up a per client request timeout to each http call made with WebClient? I have one possible approach: chaining timeout method of Mono on top of every webClient call. I am wondering if this can lead to some sort of resource leak in the eventual case of a service outage. Are there other alternatives?

Thanks in advance!!!

Felipe Moraes
  • 218
  • 3
  • 9

1 Answers1

1

This part of your question is not totally clear.

This correctly times out if the server does not respond in time. But this also has other implication: after a WebClient successful emission of a response (after .retrive() or .exchange()) the timer keeps going on, such that if the timeout value is reached then a timeout exception is raised.

At the Reactor Netty level, you can configure several things:

  • the connect timeout, i.e. max time the client can take to establish a connection with the server
  • the read timeout, max amount of time the client should wait while receiving no data from the server and the response is incomplete

Looking at your question, it seems you're pointing out that streaming scenarios, where the server can send events as they come, can trigger this timeout. I agree with that.

There are two solutions for that:

  1. you configure a separate connector instance for streaming scenarios so you don't run into this issue
  2. you can append the timeout operator to your reactive chain. In case of timeouts or errors, all Spring WebFlux operations dealing with pooled resources are cleaning those resources to avoid leaks. You're of course responsible of that if you manually deal with resources that can't be GCed without a manual call from your code
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176