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

Fix StorkClientRequestFilter exception handling #39731

Merged
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 @@ -15,9 +15,7 @@
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestFilter;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.smallrye.stork.Stork;
import io.smallrye.stork.api.ServiceInstance;

@Priority(Priorities.AUTHENTICATION)
@Provider
Expand All @@ -34,67 +32,64 @@ public void filter(ResteasyReactiveClientRequestContext requestContext) {
}

requestContext.suspend();
Uni<ServiceInstance> serviceInstance;
boolean measureTime = shouldMeasureTime(requestContext.getResponseType());
try {
serviceInstance = Stork.getInstance()
Stork.getInstance()
.getService(serviceName)
.selectInstanceAndRecordStart(measureTime);
} catch (Throwable e) {
log.error("Error selecting service instance for serviceName: " + serviceName, e);
requestContext.resume(e);
return;
}

serviceInstance.subscribe()
.with(instance -> {
boolean isHttps = instance.isSecure() || "storks".equals(uri.getScheme());
String scheme = isHttps ? "https" : "http";
try {
// In the case the service instance does not set the host and/or port
String host = instance.getHost() == null ? "localhost" : instance.getHost();
int port = instance.getPort();
if (instance.getPort() == 0) {
if (isHttps) {
port = 433;
} else {
port = 80;
}
}
// Service instance can also contain an optional path.
Optional<String> path = instance.getPath();
String actualPath = uri.getRawPath();
if (path.isPresent()) {
var p = path.get();
if (!p.startsWith("/")) {
p = "/" + p;
.selectInstanceAndRecordStart(measureTime)
.subscribe()
.with(instance -> {
boolean isHttps = instance.isSecure() || "storks".equals(uri.getScheme());
String scheme = isHttps ? "https" : "http";
try {
// In the case the service instance does not set the host and/or port
String host = instance.getHost() == null ? "localhost" : instance.getHost();
int port = instance.getPort();
if (instance.getPort() == 0) {
if (isHttps) {
port = 433;
} else {
port = 80;
}
}
if (actualPath == null) {
actualPath = p;
} else {
// Append both.
if (actualPath.startsWith("/") || p.endsWith("/")) {
actualPath = p + actualPath;
// Service instance can also contain an optional path.
Optional<String> path = instance.getPath();
String actualPath = uri.getRawPath();
if (path.isPresent()) {
var p = path.get();
if (!p.startsWith("/")) {
p = "/" + p;
}
if (actualPath == null) {
actualPath = p;
} else {
actualPath = p + "/" + actualPath;
// Append both.
if (actualPath.startsWith("/") || p.endsWith("/")) {
actualPath = p + actualPath;
} else {
actualPath = p + "/" + actualPath;
}
}
}
//To avoid the path double encoding we create uri with path=null and set the path after
URI newUri = new URI(scheme,
uri.getUserInfo(), host, port,
null, uri.getQuery(), uri.getFragment());
URI build = UriBuilder.fromUri(newUri).path(actualPath).build();
requestContext.setUri(build);
if (measureTime && instance.gatherStatistics()) {
requestContext.setCallStatsCollector(instance);
}
requestContext.resume();
} catch (URISyntaxException e) {
requestContext.resume(new IllegalArgumentException("Invalid URI", e));
}
//To avoid the path double encoding we create uri with path=null and set the path after
URI newUri = new URI(scheme,
uri.getUserInfo(), host, port,
null, uri.getQuery(), uri.getFragment());
URI build = UriBuilder.fromUri(newUri).path(actualPath).build();
requestContext.setUri(build);
if (measureTime && instance.gatherStatistics()) {
requestContext.setCallStatsCollector(instance);
}
requestContext.resume();
} catch (URISyntaxException e) {
requestContext.resume(new IllegalArgumentException("Invalid URI", e));
}
},
requestContext::resume);
},
requestContext::resume);
} catch (Throwable e) {
log.error("Error selecting service instance for serviceName: " + serviceName, e);
requestContext.resume(e);
}
}

}
Expand Down
Loading