Skip to content
Open
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 @@ -11,6 +11,7 @@
package org.zowe.apiml.gateway.config;

import io.netty.channel.ChannelOption;
import io.netty.channel.ConnectTimeoutException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -25,6 +26,7 @@
import reactor.netty.http.client.HttpClient;

import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -84,14 +86,26 @@ protected HttpClient getHttpClient(Route route, ServerWebExchange exchange) {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return super.filter(exchange, chain).onErrorResume(e -> {
if (e.getCause() instanceof ConnectException) {
if (isServiceUnavailable(e)) {
log.debug("Connection to {} was not established: {}", exchange.getRequest().getURI(), e.getMessage());
var uri = exchange.getRequest().getURI();
return Mono.error(new ServiceNotAccessibleException(String.format("Service is not available at %s://%s:%d", uri.getScheme(), uri.getHost(), uri.getPort()), e));
}
return Mono.error(e);
});
}

static boolean isServiceUnavailable(Throwable e) {
Throwable cause = e;
while (cause != null) {
if (cause instanceof ConnectException
|| cause instanceof ConnectTimeoutException
|| cause instanceof NoRouteToHostException) {
return true;
}
cause = cause.getCause();
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import io.netty.channel.ChannelOption;
import io.netty.channel.ConnectTimeoutException;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import org.hamcrest.Matchers;
Expand All @@ -28,6 +29,8 @@
import reactor.netty.http.client.HttpClient;

import javax.net.ssl.SSLException;
import java.net.ConnectException;
import java.nio.channels.ClosedChannelException;
import java.time.Duration;

import static io.restassured.RestAssured.given;
Expand Down Expand Up @@ -158,6 +161,39 @@ void givenTimeoutAndRequirementsForClientCert_whenGetHttpClient_thenCallWithoutC

}

@Nested
class IsServiceUnavailable {

@Test
void givenConnectTimeoutException_whenIsServiceUnavailable_thenReturnsTrue() {
assertTrue(NettyRoutingFilterApiml.isServiceUnavailable(new ConnectTimeoutException("connect timed out")));
}

@Test
void givenConnectException_whenIsServiceUnavailable_thenReturnsTrue() {
assertTrue(NettyRoutingFilterApiml.isServiceUnavailable(new ConnectException("connection refused")));
}

@Test
void givenConnectExceptionNestedInClosedChannelException_whenIsServiceUnavailable_thenReturnsTrue() {
ClosedChannelException outer = new ClosedChannelException();
ConnectException inner = new ConnectException("connection refused");
outer.initCause(inner);
assertTrue(NettyRoutingFilterApiml.isServiceUnavailable(outer));
}

@Test
void givenUnrelatedException_whenIsServiceUnavailable_thenReturnsFalse() {
assertFalse(NettyRoutingFilterApiml.isServiceUnavailable(new RuntimeException("some other error")));
}

@Test
void givenSslException_whenIsServiceUnavailable_thenReturnsFalse() {
assertFalse(NettyRoutingFilterApiml.isServiceUnavailable(new javax.net.ssl.SSLException("cert error")));
}

}

@Nested
class UnavailableService extends AcceptanceTestWithMockServices {

Expand Down
Loading