If hystrix timesout, return status 504 gateway timeout.

fixes gh-82
This commit is contained in:
Spencer Gibb
2017-10-10 13:02:04 -04:00
parent 4a92c3156a
commit 60a4f769cf
2 changed files with 20 additions and 5 deletions

View File

@@ -17,6 +17,11 @@
package org.springframework.cloud.gateway.filter.factory;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import org.springframework.http.HttpStatus;
import org.springframework.tuple.Tuple;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
@@ -25,15 +30,16 @@ import org.springframework.web.server.WebFilterChain;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixObservableCommand;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import static com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus;
import reactor.core.publisher.Mono;
import rx.Observable;
import rx.RxReactiveStreams;
import rx.Subscription;
import java.util.Arrays;
import java.util.List;
/**
* @author Spencer Gibb
*/
@@ -61,7 +67,16 @@ public class HystrixWebFilterFactory implements WebFilterFactory {
return Mono.create(s -> {
Subscription sub = command.toObservable().subscribe(s::success, s::error, s::success);
s.onCancel(sub::unsubscribe);
});
}).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> {
if (throwable instanceof HystrixRuntimeException) {
HystrixRuntimeException e = (HystrixRuntimeException) throwable;
if (e.getFailureType() == TIMEOUT) { //TODO: optionally set status
setResponseStatus(exchange, HttpStatus.GATEWAY_TIMEOUT);
return exchange.getResponse().setComplete();
}
}
return Mono.empty();
}).then();
};
}

View File

@@ -70,7 +70,7 @@ public class HystrixWebFilterFactoryTests extends BaseWebClientTests {
StepVerifier.create(result)
.consumeNextWith(
response -> assertStatus(response, HttpStatus.INTERNAL_SERVER_ERROR))
response -> assertStatus(response, HttpStatus.GATEWAY_TIMEOUT))
.expectComplete()
.verify(DURATION);
}