spicydev commented on code in PR #3776:
URL: https://github.com/apache/shenyu/pull/3776#discussion_r933942292
##########
shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/WebClientPlugin.java:
##########
@@ -49,27 +50,29 @@ public WebClientPlugin(final WebClient webClient) {
}
@Override
- protected Mono<ClientResponse> doRequest(final ServerWebExchange exchange,
final String httpMethod, final URI uri,
+ protected Mono<ResponseEntity<Void>> doRequest(final ServerWebExchange
exchange, final String httpMethod, final URI uri,
final HttpHeaders httpHeaders,
final Flux<DataBuffer> body) {
- // springWebflux5.3 mark #exchange() deprecated. because #echange
maybe make memory leak.
- // https://github.com/spring-projects/spring-framework/issues/25751
- // exchange is deprecated, so change to {@link
WebClient.RequestHeadersSpec#exchangeToMono(Function)}
- // exchangeToMono has two important bug:
- // 1.exchangeToMono can cause NPE when response body is null
- // 2.download file with exchangeToMono can't open
+ // Upgraded to Spring WebClient Retrieve utilizing RequestSpec &
ResponseSpec standard.
+ // OnStatus - is2xxSuccessful captures success responses
+ // OnStatus - isError captures all 4xx/5xx responses
+ // toBodilessEntity - captures 204 responses.
return webClient.method(HttpMethod.valueOf(httpMethod)).uri(uri)
.headers(headers -> headers.addAll(httpHeaders))
.body(BodyInserters.fromDataBuffers(body))
- .exchange()
- .doOnSuccess(res -> {
- if (res.statusCode().is2xxSuccessful()) {
-
exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE,
ResultEnum.SUCCESS.getName());
- } else {
-
exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE,
ResultEnum.ERROR.getName());
- }
+ .retrieve()
Review Comment:
Hi @dragon-zhang , the performance of both exchange() & retrieve() is same,
but retrieve restricts us to consume payload with it's ResponseSpec interface.
`@Override
public ResponseSpec retrieve() {
return new DefaultResponseSpec(exchange(), this::createRequest);
}`
Perhaps the question here is why should WebClientMessageWriter has to handle
the responsibility to release client response body which is clearly causing the
memory leak in case of null pointers. That responsibility we can try to
delegate back to the WebClientPlugin.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]