Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.NettyPipeline;
Expand Down Expand Up @@ -151,10 +152,9 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
});

if (properties.getResponseTimeout() != null) {
//TODO: figure out how to make this a 504
responseFlux = responseFlux.timeout(properties.getResponseTimeout(),
Mono.error(new TimeoutException("Response took longer than timeout: " +
properties.getResponseTimeout())));
properties.getResponseTimeout()))).onErrorMap(TimeoutException.class, th -> new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, null, th));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, here one may say "Hey, why don't we get rid of TimeoutException altogether then"?

The answer is: it is a very convenient "marker" exception, which I already make use of in #555. I like the fact that, by not reusing java.util.concurrent.TimeoutException, one can tell apart this type of timeout from one issued, for example, by a filter using java.util.concurrent.Future for whatever dastardly reasons.

}

return responseFlux.then(chain.filter(exchange));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;

Expand All @@ -44,12 +45,8 @@ public void responseTimeoutWorks() {
testClient.get()
.uri("/delay/5")
.exchange()
.expectStatus().is5xxServerError()
.expectBody(Map.class)
.consumeWith(result -> {
Map body = result.getResponseBody();
assertThat(body).containsEntry("message", "Response took longer than timeout: PT3S");
});
.expectStatus().isEqualTo(HttpStatus.GATEWAY_TIMEOUT)
.expectBody().jsonPath("$.status").isEqualTo(String.valueOf(HttpStatus.GATEWAY_TIMEOUT.value()));
}

@EnableAutoConfiguration
Expand Down