4

Context

I'm trying to find the best way to combine Spring 5 WebClient and Hystrix. Using Hystrix, I set different timeouts for different type of requests done by the WebClient.

When Hystrix reaches it's timeout, I also want to make sure that WebClient closes its connection. Previously when using AsyncHttpClient, this was done by setting a requestTimeout before performing the specific call. However, setting the request timeout on WebClient is much more complicated and needs to be done on the ClientHttpConnector according to this answer.

Brian Cozel mentions that it is optimal to share the same ClientHttpConnector throughout the application. However, because the request specific timeout needs to be set on the ClientHttpConnector, this does not seem possible.

Question

In Spring's Reactive WebClient, is there a proper way to set request specific timeouts, but still use a single ClientHttpConnector?

Wyko
  • 577
  • 5
  • 13
  • Could you simplify your question? This seems to be 3 questions in one - please focus on a single one: 1) does spring cloud netflix support webflux? 2) What are the different timeout options with WebClient and how do they differ? 3) Is there a way to set a response timeout on a per request basis with WebClient – Brian Clozel Jun 05 '18 at 17:05
  • Hi @BrianClozel, thanks for the quick response. I've tried to clarify my question in an edit. My question is 3), setting a response timeout on a per request basic, while still reusing the same `ClientHttpConnector` as you suggest doing for performance reasons. – Wyko Jun 05 '18 at 20:08

1 Answers1

3

The timeout operations that you can configure on the client connector are quite low level: they're about socket/connection timeouts. This configuration cannot be done at the request level, since connections might be shared and reused in a connection pool.

This question is about response timeouts, since you seem to care about the amount of time to get the response, on a per request basis.

In this case, you can use the timeout operator on a per request basis:

Mono<UserData> result = this.webClient.get()
                .uri("/user")
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToMono(UserData.class)
                .timeout(Duration.ofSeconds(10));

The timeout operator will throw a TimeoutException in the pipeline; you can use one of the onError* operators to define what should be done in those cases. Alternatively, you can directly use the timeout(Duration, Mono) variant that provides a fallback.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • Hystrix will also throw some kind of timeout exception, but reaching this timeout doesn't mean the HTTP client connection is closed immediately, right? Or is that the case? – Nils Breunese Jun 08 '18 at 12:08
  • It is the case as far as I know for reactor-netty. But it depends on the underlying client library, if it is configured for connection pooling, etc. – Brian Clozel Jun 08 '18 at 12:10