diff --git a/dialogue-clients/src/main/java/com/palantir/dialogue/clients/DialogueClients.java b/dialogue-clients/src/main/java/com/palantir/dialogue/clients/DialogueClients.java index d6f8ec8cb..5c3cb9996 100644 --- a/dialogue-clients/src/main/java/com/palantir/dialogue/clients/DialogueClients.java +++ b/dialogue-clients/src/main/java/com/palantir/dialogue/clients/DialogueClients.java @@ -17,6 +17,7 @@ package com.palantir.dialogue.clients; import com.google.common.annotations.Beta; +import com.google.common.collect.ImmutableList; import com.google.errorprone.annotations.CheckReturnValue; import com.palantir.conjure.java.api.config.service.ServicesConfigBlock; import com.palantir.conjure.java.client.config.ClientConfiguration; @@ -159,7 +160,13 @@ public interface PerHostClientFactory { * Returns a list of channels where each channel will route requests to a single, unique host, even if that host * returns some 429s. */ - Refreshable> getPerHostChannels(); + default Refreshable> getPerHostChannels() { + return getNamedPerHostChannels().map(channels -> ImmutableList.copyOf(channels.values())); + } + + default Refreshable> getPerHost(Class clientInterface) { + return getNamedPerHost(clientInterface).map(channels -> ImmutableList.copyOf(channels.values())); + } /** * Returns a list of channels where each channel will route requests to a single, unique host, even if that host @@ -167,8 +174,6 @@ public interface PerHostClientFactory { */ Refreshable> getNamedPerHostChannels(); - Refreshable> getPerHost(Class clientInterface); - Refreshable> getNamedPerHost(Class clientInterface); } diff --git a/dialogue-clients/src/main/java/com/palantir/dialogue/clients/PerHostTarget.java b/dialogue-clients/src/main/java/com/palantir/dialogue/clients/PerHostTarget.java index 7ee79c967..bc5b99393 100644 --- a/dialogue-clients/src/main/java/com/palantir/dialogue/clients/PerHostTarget.java +++ b/dialogue-clients/src/main/java/com/palantir/dialogue/clients/PerHostTarget.java @@ -21,24 +21,18 @@ public final class PerHostTarget { private final TargetUri targetUri; - private final boolean isSelf; - PerHostTarget(TargetUri targetUri, boolean isSelf) { + PerHostTarget(TargetUri targetUri) { this.targetUri = targetUri; - this.isSelf = isSelf; } public TargetUri targetUri() { return targetUri; } - public boolean isSelf() { - return isSelf; - } - @Override public String toString() { - return "Target{targetUri='" + targetUri + "', isSelf=" + isSelf + '}'; + return "Target{targetUri='" + targetUri + '}'; } @Override @@ -50,13 +44,11 @@ public boolean equals(Object other) { return false; } PerHostTarget perHostTarget = (PerHostTarget) other; - return targetUri.equals(perHostTarget.targetUri) && isSelf == perHostTarget.isSelf; + return targetUri.equals(perHostTarget.targetUri); } @Override public int hashCode() { - int result = targetUri.hashCode(); - result = 31 * result + Boolean.hashCode(isSelf); - return result; + return targetUri.hashCode(); } } diff --git a/dialogue-clients/src/main/java/com/palantir/dialogue/clients/ReloadingClientFactory.java b/dialogue-clients/src/main/java/com/palantir/dialogue/clients/ReloadingClientFactory.java index ec4aede62..001f2941d 100644 --- a/dialogue-clients/src/main/java/com/palantir/dialogue/clients/ReloadingClientFactory.java +++ b/dialogue-clients/src/main/java/com/palantir/dialogue/clients/ReloadingClientFactory.java @@ -66,10 +66,8 @@ import com.palantir.refreshable.Refreshable; import com.palantir.tritium.metrics.registry.TaggedMetricRegistry; import java.net.InetAddress; -import java.net.NetworkInterface; import java.net.Proxy; import java.net.ProxySelector; -import java.net.SocketException; import java.net.URI; import java.net.URISyntaxException; import java.security.Provider; @@ -246,16 +244,6 @@ public PerHostClientFactory perHost(String serviceName) { return ImmutableMap.of(); } - Set selfAddresses; - try { - selfAddresses = NetworkInterface.networkInterfaces() - .flatMap(NetworkInterface::inetAddresses) - .collect(Collectors.toSet()); - } catch (SocketException e) { - log.warn("Failed to obtain local addresses from network interfaces", e); - selfAddresses = Set.of(); - } - ImmutableMap.Builder map = ImmutableMap.builder(); for (int i = 0; i < targetUris.size(); i++) { TargetUri targetUri = targetUris.get(i); @@ -263,15 +251,11 @@ public PerHostClientFactory perHost(String serviceName) { .from(serviceConfiguration) .uris(ImmutableList.of(targetUri.uri())) .build(); - boolean isSelf = targetUri - .resolvedAddress() - .map(selfAddresses::contains) - .orElse(false); // subtle gotcha here is that every single one of these has the same channelName, // which means metrics like the QueuedChannel counter will end up being the sum of all of them. map.put( - new PerHostTarget(targetUri, isSelf), + new PerHostTarget(targetUri), cache.getNonReloadingChannel( params, singleUriServiceConf, @@ -283,23 +267,11 @@ public PerHostClientFactory perHost(String serviceName) { }); return new PerHostClientFactory() { - @Override - public Refreshable> getPerHostChannels() { - return perHostChannels.map(channels -> ImmutableList.copyOf(channels.values())); - } - @Override public Refreshable> getNamedPerHostChannels() { return perHostChannels; } - @Override - public Refreshable> getPerHost(Class clientInterface) { - return perHostChannels.map(channels -> channels.values().stream() - .map(channel -> Reflection.callStaticFactoryMethod(clientInterface, channel, params.runtime())) - .collect(ImmutableList.toImmutableList())); - } - @Override public Refreshable> getNamedPerHost(Class clientInterface) { return perHostChannels.map(channels -> channels.entrySet().stream() @@ -569,9 +541,11 @@ private ImmutableList getTargetUris( log.warn( "Failed to parse all URIs, falling back to legacy DNS approach for service '{}'", SafeArg.of("service", serviceNameForLogging)); - return uris.stream().map(TargetUri::of).collect(ImmutableList.toImmutableList()); + for (String uri : uris) { + targetUris.add(TargetUri.of(uri)); + } } - return ImmutableList.copyOf(targetUris); + return ImmutableSet.copyOf(targetUris).asList(); } private static ProxySelector proxySelector(Optional proxyConfiguration) {