|
25 | 25 |
|
26 | 26 | import org.springframework.cloud.gateway.filter.GatewayFilter; |
27 | 27 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
| 28 | +import org.springframework.cloud.gateway.support.TimeoutException; |
28 | 29 | import org.springframework.http.HttpStatus; |
29 | 30 | import org.springframework.http.server.reactive.ServerHttpRequest; |
30 | 31 | import org.springframework.util.Assert; |
|
39 | 40 | import com.netflix.hystrix.HystrixObservableCommand.Setter; |
40 | 41 | import com.netflix.hystrix.exception.HystrixRuntimeException; |
41 | 42 |
|
| 43 | +import static com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.COMMAND_EXCEPTION; |
42 | 44 | import static com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT; |
43 | 45 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; |
44 | 46 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts; |
|
52 | 54 | /** |
53 | 55 | * Depends on `spring-cloud-starter-netflix-hystrix`, {@see http://cloud.spring.io/spring-cloud-netflix/} |
54 | 56 | * @author Spencer Gibb |
| 57 | + * @author Michele Mancioppi |
55 | 58 | */ |
56 | 59 | public class HystrixGatewayFilterFactory extends AbstractGatewayFilterFactory<HystrixGatewayFilterFactory.Config> { |
57 | 60 |
|
@@ -101,7 +104,15 @@ public GatewayFilter apply(Config config) { |
101 | 104 | }).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> { |
102 | 105 | if (throwable instanceof HystrixRuntimeException) { |
103 | 106 | HystrixRuntimeException e = (HystrixRuntimeException) throwable; |
104 | | - if (e.getFailureType() == TIMEOUT) { //TODO: optionally set status |
| 107 | + HystrixRuntimeException.FailureType failureType = e.getFailureType(); |
| 108 | + |
| 109 | + if (failureType == TIMEOUT || |
| 110 | + /* |
| 111 | + * In this case, the timeout originates from WebClient due to the settings |
| 112 | + * of `spring.cloud.gateway.httpclient.responseTimeout` |
| 113 | + */ |
| 114 | + (failureType == COMMAND_EXCEPTION && e.getCause() instanceof TimeoutException)) { |
| 115 | + |
105 | 116 | setResponseStatus(exchange, HttpStatus.GATEWAY_TIMEOUT); |
106 | 117 | return exchange.getResponse().setComplete(); |
107 | 118 | } |
|
0 commit comments