Re-order list of resolvers according to their searchOrder (#11236)

Motivation:
Mac OS specific DNS resolver fails to take into account search order
of resolvers causing wrong resolver being used is some circumstances

Modifications:
Re-order array of resolvers using their sort order as an ordering key.
Final order is opposite of the search order to make sure that resolver
with the lower sort order goes last (so it overrides previous one
in the `resolverMap`).

Result:
Fixes issue https://github.com/netty/netty/issues/11225
This commit is contained in:
Ivan 2021-05-10 23:48:29 -07:00 committed by GitHub
parent 400858cd5e
commit 0d009033eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,7 +29,9 @@ import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@ -43,6 +45,16 @@ import java.util.concurrent.atomic.AtomicLong;
*/
public final class MacOSDnsServerAddressStreamProvider implements DnsServerAddressStreamProvider {
private static final Comparator<DnsResolver> RESOLVER_COMPARATOR =
new Comparator<DnsResolver>() {
@Override
public int compare(DnsResolver r1, DnsResolver r2) {
// Note: order is descending (from higher to lower) so entries with lower search order override
// entries with higher search order.
return r1.searchOrder() < r2.searchOrder() ? 1 : (r1.searchOrder() == r2.searchOrder() ? 0 : -1);
}
};
private static final Throwable UNAVAILABILITY_CAUSE;
private static final InternalLogger logger =
@ -120,6 +132,7 @@ public final class MacOSDnsServerAddressStreamProvider implements DnsServerAddre
if (resolvers == null || resolvers.length == 0) {
return Collections.emptyMap();
}
Arrays.sort(resolvers, RESOLVER_COMPARATOR);
Map<String, DnsServerAddresses> resolverMap = new HashMap<String, DnsServerAddresses>(resolvers.length);
for (DnsResolver resolver: resolvers) {
// Skip mdns