Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support independent response time route setting #3981

Draft
wants to merge 1 commit into
base: v3.x.x
Choose a base branch
from
Draft
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 @@ -30,6 +30,7 @@
import java.util.Optional;

import static org.springframework.cloud.gateway.support.RouteMetadataUtils.CONNECT_TIMEOUT_ATTR;
import static org.springframework.cloud.gateway.support.RouteMetadataUtils.RESPONSE_TIMEOUT_ATTR;
import static org.zowe.apiml.constants.ApimlConstants.HTTP_CLIENT_USE_CLIENT_CERTIFICATE;

@Slf4j
Expand Down Expand Up @@ -66,23 +67,29 @@ static Integer getInteger(Object connectTimeoutAttr) {
@Override
protected HttpClient getHttpClient(Route route, ServerWebExchange exchange) {
// select proper HttpClient instance by attribute apiml.useClientCert
boolean useClientCert = Optional.ofNullable((Boolean) exchange.getAttribute(HTTP_CLIENT_USE_CLIENT_CERTIFICATE)).orElse(Boolean.FALSE);
HttpClient httpClient = useClientCert ? httpClientClientCert : httpClientNoCert;
var useClientCert = Optional.ofNullable((Boolean) exchange.getAttribute(HTTP_CLIENT_USE_CLIENT_CERTIFICATE)).orElse(Boolean.FALSE);
var httpClient = useClientCert ? httpClientClientCert : httpClientNoCert;

log.debug("Using client with keystore {}", useClientCert);
Object connectTimeoutAttr = route.getMetadata().get(CONNECT_TIMEOUT_ATTR);
var connectTimeoutAttr = route.getMetadata().get(CONNECT_TIMEOUT_ATTR);
var responseTimeoutAttr = route.getMetadata().get(RESPONSE_TIMEOUT_ATTR);

httpClient = httpClient
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, requestTimeout)
.responseTimeout(Duration.ofMillis(requestTimeout));

if (responseTimeoutAttr != null) {
httpClient = httpClient.responseTimeout(Duration.parse(String.valueOf(responseTimeoutAttr)));
}

if (connectTimeoutAttr != null) {
// if there is configured timeout, respect it
Integer connectTimeout = getInteger(connectTimeoutAttr);
return httpClient
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout)
.responseTimeout(Duration.ofMillis(connectTimeout));
httpClient = httpClient
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
}

// otherwise just return selected HttpClient with the default configured timeouts
return httpClient
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, requestTimeout)
.responseTimeout(Duration.ofMillis(requestTimeout));
return httpClient;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ static void setup() {
})
void testRoutingWithBasePath(String basePath) throws URISyntaxException {
String scgUrl = String.format("%s://%s:%s%s", conf.getScheme(), conf.getHost(), conf.getPort(), basePath);
given().get(new URI(scgUrl)).then().statusCode(200);
given()
.get(new URI(scgUrl))
.then()
.statusCode(200);
}

@ParameterizedTest(name = "When header X-Forward-To is set to {0} should return 200")
Expand All @@ -60,8 +63,11 @@ void testRoutingWithBasePath(String basePath) throws URISyntaxException {
})
void testRoutingWithHeader(String forwardTo) throws URISyntaxException {
String scgUrl = String.format("%s://%s:%s%s", conf.getScheme(), conf.getHost(), conf.getPort(), DISCOVERABLE_GREET);
given().header(HEADER_X_FORWARD_TO, forwardTo)
.get(new URI(scgUrl)).then().statusCode(200);
given()
.header(HEADER_X_FORWARD_TO, forwardTo)
.get(new URI(scgUrl))
.then()
.statusCode(200);
}

@ParameterizedTest(name = "When base path is {0} should return 404")
Expand All @@ -71,7 +77,10 @@ void testRoutingWithHeader(String forwardTo) throws URISyntaxException {
})
void testRoutingWithIncorrectServiceInBasePath(String basePath) throws URISyntaxException {
String scgUrl = String.format("%s://%s:%s%s", conf.getScheme(), conf.getHost(), conf.getPort(), basePath);
given().get(new URI(scgUrl)).then().statusCode(404);
given()
.get(new URI(scgUrl))
.then()
.statusCode(404);
}

@ParameterizedTest(name = "When header X-Forward-To is set to {0} should return 404")
Expand All @@ -81,8 +90,11 @@ void testRoutingWithIncorrectServiceInBasePath(String basePath) throws URISyntax
})
void testRoutingWithIncorrectServiceInHeader(String forwardTo) throws URISyntaxException {
String scgUrl = String.format("%s://%s:%s%s", conf.getScheme(), conf.getHost(), conf.getPort(), NON_EXISTING_SERVICE_ENDPOINT);
given().header(HEADER_X_FORWARD_TO, forwardTo)
.get(new URI(scgUrl)).then().statusCode(404);
given()
.header(HEADER_X_FORWARD_TO, forwardTo)
.get(new URI(scgUrl))
.then()
.statusCode(404);
}

@ParameterizedTest(name = "When header X-Forward-To is set to {0} and base path is {1} should return 200 - loopback")
Expand All @@ -91,8 +103,11 @@ void testRoutingWithIncorrectServiceInHeader(String forwardTo) throws URISyntaxE
})
void testWrongRoutingWithHeader(String forwardTo, String endpoint) throws URISyntaxException {
String scgUrl = String.format("%s://%s:%s%s", conf.getScheme(), conf.getHost(), conf.getPort(), endpoint);
given().header(HEADER_X_FORWARD_TO, forwardTo)
.get(new URI(scgUrl)).then().statusCode(200);
given()
.header(HEADER_X_FORWARD_TO, forwardTo)
.get(new URI(scgUrl))
.then()
.statusCode(200);
}

@ParameterizedTest(name = "When base path is {0} should return 404")
Expand All @@ -102,12 +117,19 @@ void testWrongRoutingWithHeader(String forwardTo, String endpoint) throws URISyn
})
void testWrongRoutingWithBasePath(String basePath) throws URISyntaxException {
String scgUrl = String.format("%s://%s:%s%s", conf.getScheme(), conf.getHost(), conf.getPort(), basePath);
given().get(new URI(scgUrl)).then().statusCode(404);
given()
.get(new URI(scgUrl))
.then()
.statusCode(404);
}

@Test
void givenEndpointDoesNotExistOnRegisteredService() throws URISyntaxException {
String scgUrl = String.format("%s://%s:%s%s", conf.getScheme(), conf.getHost(), conf.getPort(), "/dcpassticket/api/v1/unknown");
given().get(new URI(scgUrl)).then().statusCode(404);
given()
.get(new URI(scgUrl))
.then()
.statusCode(404);
}

}
Loading