Commit 791fe642 by Johannes Edmeier

Create error responses only when necessary and reuse the exchangeStrategies

..to gain more performance and reduce gc pressure fixes #713
parent e25e24e1
...@@ -42,8 +42,8 @@ import org.springframework.http.client.reactive.ClientHttpRequest; ...@@ -42,8 +42,8 @@ import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.util.AntPathMatcher; import org.springframework.util.AntPathMatcher;
import org.springframework.web.reactive.function.BodyInserter; import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ResponseStatusException;
import static java.util.stream.Collectors.toMap; import static java.util.stream.Collectors.toMap;
...@@ -56,6 +56,7 @@ public class AbstractInstancesProxyController { ...@@ -56,6 +56,7 @@ public class AbstractInstancesProxyController {
private final InstanceWebClient instanceWebClient; private final InstanceWebClient instanceWebClient;
private final Set<String> ignoredHeaders; private final Set<String> ignoredHeaders;
private final Duration readTimeout; private final Duration readTimeout;
private final ExchangeStrategies strategies = ExchangeStrategies.withDefaults();
public AbstractInstancesProxyController(String adminContextPath, public AbstractInstancesProxyController(String adminContextPath,
Set<String> ignoredHeaders, Set<String> ignoredHeaders,
...@@ -80,7 +81,8 @@ public class AbstractInstancesProxyController { ...@@ -80,7 +81,8 @@ public class AbstractInstancesProxyController {
return registry.getInstance(InstanceId.of(instanceId)) return registry.getInstance(InstanceId.of(instanceId))
.flatMap(instance -> forward(instance, uri, method, headers, bodyInserter)) .flatMap(instance -> forward(instance, uri, method, headers, bodyInserter))
.defaultIfEmpty(ClientResponse.create(HttpStatus.SERVICE_UNAVAILABLE).build()); .switchIfEmpty(Mono.fromSupplier(
() -> ClientResponse.create(HttpStatus.SERVICE_UNAVAILABLE, strategies).build()));
} }
private Mono<ClientResponse> forward(Instance instance, private Mono<ClientResponse> forward(Instance instance,
...@@ -104,14 +106,14 @@ public class AbstractInstancesProxyController { ...@@ -104,14 +106,14 @@ public class AbstractInstancesProxyController {
} }
return headersSpec.exchange() return headersSpec.exchange()
.timeout(this.readTimeout, .timeout(this.readTimeout, Mono.fromSupplier(
Mono.just(ClientResponse.create(HttpStatus.GATEWAY_TIMEOUT).build())) () -> ClientResponse.create(HttpStatus.GATEWAY_TIMEOUT, strategies).build()))
.onErrorMap(ResolveEndpointException.class, .onErrorResume(ResolveEndpointException.class, ex -> Mono.fromSupplier(
error -> new ResponseStatusException(HttpStatus.NOT_FOUND, null, error)) () -> ClientResponse.create(HttpStatus.NOT_FOUND, strategies).build()))
.onErrorResume(IOException.class, .onErrorResume(IOException.class, ex -> Mono.fromSupplier(
ex -> Mono.just(ClientResponse.create(HttpStatus.BAD_GATEWAY).build())) () -> ClientResponse.create(HttpStatus.BAD_GATEWAY, strategies).build()))
.onErrorResume(ConnectException.class, .onErrorResume(ConnectException.class, ex -> Mono.fromSupplier(
ex -> Mono.just(ClientResponse.create(HttpStatus.BAD_GATEWAY).build())); () -> ClientResponse.create(HttpStatus.BAD_GATEWAY, strategies).build()));
} }
protected String getEndpointLocalPath(String pathWithinApplication) { protected String getEndpointLocalPath(String pathWithinApplication) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment