Search in sources :

Example 1 with TIMEOUT

use of com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT in project spring-cloud-gateway by spring-cloud.

the class HystrixGatewayFilterFactory method apply.

@Override
public GatewayFilter apply(Config config) {
    // TODO: if no name is supplied, generate one from command id (useful for default filter)
    if (config.setter == null) {
        HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey(getClass().getSimpleName());
        HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey(config.name);
        config.setter = Setter.withGroupKey(groupKey).andCommandKey(commandKey);
    }
    return (exchange, chain) -> {
        RouteHystrixCommand command = new RouteHystrixCommand(config.setter, config.fallbackUri, exchange, chain);
        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) {
                    setResponseStatus(exchange, HttpStatus.GATEWAY_TIMEOUT);
                    return exchange.getResponse().setComplete();
                }
            }
            return Mono.empty();
        }).then();
    };
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) Arrays(java.util.Arrays) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) ServerWebExchangeUtils.containsEncodedParts(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts) Mono(reactor.core.publisher.Mono) DispatcherHandler(org.springframework.web.reactive.DispatcherHandler) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) Setter(com.netflix.hystrix.HystrixObservableCommand.Setter) TIMEOUT(com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT) Function(java.util.function.Function) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Observable(rx.Observable) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) RxReactiveStreams(rx.RxReactiveStreams) ServerWebExchangeUtils.setResponseStatus(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus) GATEWAY_REQUEST_URL_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR) HystrixObservableCommand(com.netflix.hystrix.HystrixObservableCommand) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) URI(java.net.URI) Subscription(rx.Subscription) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) Mono(reactor.core.publisher.Mono) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) Subscription(rx.Subscription) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey)

Aggregations

HystrixCommandGroupKey (com.netflix.hystrix.HystrixCommandGroupKey)1 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)1 HystrixObservableCommand (com.netflix.hystrix.HystrixObservableCommand)1 Setter (com.netflix.hystrix.HystrixObservableCommand.Setter)1 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)1 TIMEOUT (com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Function (java.util.function.Function)1 GatewayFilter (org.springframework.cloud.gateway.filter.GatewayFilter)1 GatewayFilterChain (org.springframework.cloud.gateway.filter.GatewayFilterChain)1 GATEWAY_REQUEST_URL_ATTR (org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR)1 ServerWebExchangeUtils.containsEncodedParts (org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts)1 ServerWebExchangeUtils.setResponseStatus (org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus)1 HttpStatus (org.springframework.http.HttpStatus)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 DispatcherHandler (org.springframework.web.reactive.DispatcherHandler)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1