Skip to content

Commit

Permalink
unify by doFinal
Browse files Browse the repository at this point in the history
  • Loading branch information
raccoonback committed Dec 2, 2024
1 parent 6a4a5c2 commit c813a7e
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SignalType;

import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpResponse;
Expand Down Expand Up @@ -49,18 +50,21 @@ public int getOrder() {
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added
// until the WebHandler is run
return chain.filter(exchange).doOnError(throwable -> cleanup(exchange)).then(Mono.defer(() -> {
return chain.filter(exchange).then(Mono.defer(() -> {
ClientResponse clientResponse = exchange.getAttribute(CLIENT_RESPONSE_ATTR);
if (clientResponse == null) {
return Mono.empty();
}
log.trace("WebClientWriteResponseFilter start");
ServerHttpResponse response = exchange.getResponse();

return response.writeWith(clientResponse.body(BodyExtractors.toDataBuffers()))
// .log("webClient response")
.doOnCancel(() -> cleanup(exchange));
}));
return response.writeWith(clientResponse.body(BodyExtractors.toDataBuffers()));
}))
.doFinally(signalType -> {
if(SignalType.CANCEL == signalType || SignalType.ON_ERROR == signalType) {
cleanup(exchange);
}
});
}

private void cleanup(ServerWebExchange exchange) {
Expand Down

0 comments on commit c813a7e

Please sign in to comment.