Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for configuring a custom HostsFileEntriesResolver #1831

Merged
merged 2 commits into from
Sep 29, 2021

Conversation

violetagg
Copy link
Member

Fixes #1822

@violetagg violetagg added this to the 1.0.12 milestone Sep 29, 2021
@violetagg violetagg requested a review from a team September 29, 2021 13:20
@violetagg
Copy link
Member Author

@KimJejun Once this PR is merged you will be able to do the following:

HttpClient.create()
          .resolver(spec ->
              spec.hostsFileEntriesResolver((inetHost, resolvedAddressTypes) -> {
                  HostsFileEntriesProvider provider = HostsFileEntriesProvider.parser().parseSilently();
                  List<InetAddress> addresses = provider.ipv4Entries().get(inetHost);
                  return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;
              }));

This example returns the first resolved address, but it is not limited to the first entry only.

@simonbasle
Copy link
Member

simonbasle commented Sep 29, 2021

I was confused for a bit by Netty's DefaultHostsFileEntriesResolver having an adresses method. The DnsNameResolver using HostsFileEntriesResolver does check if it is the default instance in which case it returns the whole list, while user-provided resolvers can only return a single address...

DefaultHostsFileEntriesResolver is final, and thus can't be extended by users.

That's a bummer, but a limitation of the Netty API.

Note that in the provided example in comment above, the resolvedAddressTypes is ignored.
An alternative code doing the same but honoring the type could easily be done using DefaultHostsFileEntriesResolver:

DefaultHostsFileEntriesResolver defaultResolver = new DefaultHostsFileEntriesResolver();
List<InetAddress> addresses = defaultResolver.addresses(inetHost, resolvedAddressTypes);
return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;

@simonbasle
Copy link
Member

So this actually fixes the issue because the DnsNameResolver won't see a DefaultHostsFileEntriesResolver and thus won't return multiple addresses for the purpose of resolveAll 👍

Copy link
Member

@simonbasle simonbasle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DnsNameResolver using HostsFileEntriesResolver does check if it is the default instance in which case it returns the whole list, while user-provided resolvers can only return a single address...

I wonder: should this and the snippet you provided (or my edit) be mentioned in the reference doc @violetagg ?

@violetagg
Copy link
Member Author

I wonder: should this and the snippet you provided (or my edit) be mentioned in the reference doc @violetagg ?

This is a rare case and for the moment I do not intend to provide an example in the ref documentation.

@violetagg violetagg merged commit 48dfda4 into main Sep 29, 2021
@violetagg violetagg deleted the name-resolver-custom-hosts-provider branch September 29, 2021 17:32
@KimJejun
Copy link

@violetagg Thanks. I will try this when released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Turn off netty client DNS connect retry.
3 participants