Skip to content

Commit 12ace1c

Browse files
authored
Merge pull request #3611 from raccoonback/dispose-connection-when-cancelled
Suggests to dispose connection when cancelled In NettyWriteResponseFilter
2 parents 4998631 + 02ab0bc commit 12ace1c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/NettyWriteResponseFilter.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.commons.logging.LogFactory;
2424
import reactor.core.publisher.Flux;
2525
import reactor.core.publisher.Mono;
26+
import reactor.core.publisher.SignalType;
2627
import reactor.netty.Connection;
2728

2829
import org.springframework.core.Ordered;
@@ -98,8 +99,12 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
9899
return (isStreamingMediaType(contentType)
99100
? response.writeAndFlushWith(body.map(Flux::just))
100101
: response.writeWith(body));
101-
})).doOnCancel(() -> cleanup(exchange))
102-
.doOnError(throwable -> cleanup(exchange));
102+
}))
103+
.doFinally(signalType -> {
104+
if (signalType == SignalType.CANCEL || signalType == SignalType.ON_ERROR) {
105+
cleanup(exchange);
106+
}
107+
});
103108
// @formatter:on
104109
}
105110

@@ -116,12 +121,12 @@ else if (bufferFactory instanceof DefaultDataBufferFactory) {
116121
byteBuf.release();
117122
return buffer;
118123
}
119-
throw new IllegalArgumentException("Unkown DataBufferFactory type " + bufferFactory.getClass());
124+
throw new IllegalArgumentException("Unknown DataBufferFactory type " + bufferFactory.getClass());
120125
}
121126

122127
private void cleanup(ServerWebExchange exchange) {
123128
Connection connection = exchange.getAttribute(CLIENT_RESPONSE_CONN_ATTR);
124-
if (connection != null && connection.channel().isActive()) {
129+
if (connection != null) {
125130
connection.dispose();
126131
}
127132
}

0 commit comments

Comments
 (0)