Skip to content

Commit

Permalink
Re-order list of resolvers according to their searchOrder (netty#11236)
Browse files Browse the repository at this point in the history
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 netty#11225
  • Loading branch information
gwre-ivan authored May 11, 2021
1 parent 400858c commit 0d00903
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
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;
Expand All @@ -43,6 +45,16 @@
*/
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 =
Expand Down Expand Up @@ -120,6 +132,7 @@ private static Map<String, DnsServerAddresses> retrieveCurrentMappings() {
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
Expand Down

0 comments on commit 0d00903

Please sign in to comment.