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!!!