Commit d062ce13 by Johannes Edmeier

Properly sort the detected endpoints before omitting the duplicats

parent 9216a50d
...@@ -26,7 +26,6 @@ import reactor.core.publisher.Mono; ...@@ -26,7 +26,6 @@ import reactor.core.publisher.Mono;
import java.net.URI; import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
...@@ -37,11 +36,12 @@ import org.springframework.util.Assert; ...@@ -37,11 +36,12 @@ import org.springframework.util.Assert;
import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.groupingBy;
public class ProbeEndpointsStrategy implements EndpointDetectionStrategy { public class ProbeEndpointsStrategy implements EndpointDetectionStrategy {
private static final Logger log = LoggerFactory.getLogger(ProbeEndpointsStrategy.class); private static final Logger log = LoggerFactory.getLogger(ProbeEndpointsStrategy.class);
private final Collection<EndpointDefinition> endpoints; private final List<EndpointDefinition> endpoints;
private final InstanceWebClient instanceWebClient; private final InstanceWebClient instanceWebClient;
public ProbeEndpointsStrategy(InstanceWebClient instanceWebClient, String[] endpoints) { public ProbeEndpointsStrategy(InstanceWebClient instanceWebClient, String[] endpoints) {
...@@ -59,7 +59,7 @@ public class ProbeEndpointsStrategy implements EndpointDetectionStrategy { ...@@ -59,7 +59,7 @@ public class ProbeEndpointsStrategy implements EndpointDetectionStrategy {
.flatMap(this::convert); .flatMap(this::convert);
} }
private Mono<Endpoint> detectEndpoint(Instance instance, EndpointDefinition endpoint) { private Mono<DetectedEndpoint> detectEndpoint(Instance instance, EndpointDefinition endpoint) {
URI uri = UriComponentsBuilder.fromUriString(instance.getRegistration().getManagementUrl()) URI uri = UriComponentsBuilder.fromUriString(instance.getRegistration().getManagementUrl())
.path("/") .path("/")
.path(endpoint.getPath()) .path(endpoint.getPath())
...@@ -68,34 +68,49 @@ public class ProbeEndpointsStrategy implements EndpointDetectionStrategy { ...@@ -68,34 +68,49 @@ public class ProbeEndpointsStrategy implements EndpointDetectionStrategy {
return instanceWebClient.instance(instance).options().uri(uri).exchange().flatMap(this.convert(endpoint, uri)); return instanceWebClient.instance(instance).options().uri(uri).exchange().flatMap(this.convert(endpoint, uri));
} }
private Function<ClientResponse, Mono<Endpoint>> convert(EndpointDefinition endpoint, URI uri) { private Function<ClientResponse, Mono<DetectedEndpoint>> convert(EndpointDefinition endpointDefinition, URI uri) {
return response -> response.bodyToMono(Void.class) return response -> {
.then(response.statusCode() Mono<DetectedEndpoint> endpoint = Mono.empty();
.is2xxSuccessful() ? Mono.just(Endpoint.of(endpoint.getId(), if (response.statusCode().is2xxSuccessful()) {
uri.toString() endpoint = Mono.just(DetectedEndpoint.of(endpointDefinition, uri.toString()));
)) : Mono.empty()); }
return response.bodyToMono(Void.class).then(endpoint);
};
} }
private Mono<Endpoints> convert(List<Endpoint> endpoints) { private Mono<Endpoints> convert(List<DetectedEndpoint> endpoints) {
if (endpoints.isEmpty()) { if (endpoints.isEmpty()) {
return Mono.empty(); return Mono.empty();
} }
Map<String, List<Endpoint>> endpointsById = endpoints.stream().collect(groupingBy(Endpoint::getId)); Map<String, List<DetectedEndpoint>> endpointsById = endpoints.stream()
.collect(groupingBy(e -> e.getDefinition()
.getId()));
List<Endpoint> result = endpointsById.values().stream().map(endpointList -> { List<Endpoint> result = endpointsById.values().stream().map(endpointList -> {
endpointList.sort(comparingInt(e -> this.endpoints.indexOf(e.getDefinition())));
if (endpointList.size() > 1) { if (endpointList.size() > 1) {
log.warn("Duplicate endpoints for id '{}' detected. Omitting: {}", log.warn("Duplicate endpoints for id '{}' detected. Omitting: {}",
endpointList.get(0).getId(), endpointList.get(0).getDefinition().getId(),
endpointList.subList(1, endpointList.size()) endpointList.subList(1, endpointList.size())
); );
} }
return endpointList.get(0); return endpointList.get(0).getEndpoint();
}).collect(Collectors.toList()); }).collect(Collectors.toList());
return Mono.just(Endpoints.of(result)); return Mono.just(Endpoints.of(result));
} }
@Data @Data
private static class DetectedEndpoint {
private final EndpointDefinition definition;
private final Endpoint endpoint;
private static DetectedEndpoint of(EndpointDefinition endpointDefinition, String url) {
return new DetectedEndpoint(endpointDefinition, Endpoint.of(endpointDefinition.getId(), url));
}
}
@Data
private static class EndpointDefinition { private static class EndpointDefinition {
private final String id; private final String id;
private final String path; private final String path;
......
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