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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With