Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring WebFlux WebClient timeout() and exchange()

I have code that is similar to the following example:

Mono<ResponseEntity<String>> result = webClient
    .post()
    .body(Mono.just(command), MyCommand.class)
    .exchange()
    .timeout(calculateTimeout(command))
    .flatMap(clientResponse -> clientResponse.toEntity(String.class));

The spring documentation says:

When using exchange() you must always use any of the body or toEntity methods of ClientResponse to ensure resources are released and to avoid potential issues with HTTP connection pooling. You can use bodyToMono(Void.class) if no response content is expected. However keep in mind that if the response does have content, the connection will be closed and will not be placed back in the pool.

Question: In case of a TimeoutException triggered by timeout(...) as in the code above, do I have to do something explicitely to make sure that all resources are released properly or is the code above sufficient? I want to avoid having a memory leak here.

like image 703
mio Avatar asked Nov 07 '22 03:11

mio


1 Answers

I don't think this is an issue in this case.

When triggered, timeout will cancel() upstream, effectively closing the connection and not returning it to the connection pool. You don't need to do anything special here, and there won't be a memory leak (besides buffers already sitting in reactor internal queues, which is a problem Spring Framework will solve in SPR-17025).

like image 102
Brian Clozel Avatar answered Nov 15 '22 10:11

Brian Clozel