Skip to content

Commit

Permalink
Refactor the DNS resolver used to resolve internet names to avoid con…
Browse files Browse the repository at this point in the history
…fusion with the new net address resolver mechanism
  • Loading branch information
vietj committed Sep 11, 2023
1 parent 81dc710 commit 280f4cf
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, Address
obj.setHostsPath((String)member.getValue());
}
break;
case "hostsRefreshPeriod":
if (member.getValue() instanceof Number) {
obj.setHostsRefreshPeriod(((Number)member.getValue()).intValue());
}
break;
case "hostsValue":
if (member.getValue() instanceof String) {
obj.setHostsValue(io.vertx.core.buffer.Buffer.buffer(BASE64_DECODER.decode((String)member.getValue())));
Expand Down Expand Up @@ -115,6 +120,7 @@ static void toJson(AddressResolverOptions obj, java.util.Map<String, Object> jso
if (obj.getHostsPath() != null) {
json.put("hostsPath", obj.getHostsPath());
}
json.put("hostsRefreshPeriod", obj.getHostsRefreshPeriod());
if (obj.getHostsValue() != null) {
json.put("hostsValue", BASE64_ENCODER.encodeToString(obj.getHostsValue().getBytes()));
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/vertx/core/Vertx.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

package io.vertx.core;

import io.netty.channel.EventLoopGroup;
import io.vertx.codegen.annotations.*;
import io.vertx.core.datagram.DatagramSocket;
import io.vertx.core.datagram.DatagramSocketOptions;
Expand All @@ -25,7 +24,7 @@
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.VertxBuilder;
import io.vertx.core.impl.resolver.DnsResolverProvider;
import io.vertx.core.dns.impl.DnsAddressResolverProvider;
import io.vertx.core.metrics.Measured;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetClientOptions;
Expand Down Expand Up @@ -229,7 +228,7 @@ default DatagramSocket createDatagramSocket() {
/**
* Create a DNS client to connect to the DNS server configured by {@link VertxOptions#getAddressResolverOptions()}
* <p>
* DNS client takes the first configured resolver address provided by {@link DnsResolverProvider#nameServerAddresses()}}
* DNS client takes the first configured resolver address provided by {@link DnsAddressResolverProvider#nameServerAddresses()}}
*
* @return the DNS client
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/

package io.vertx.core.impl.resolver;
package io.vertx.core.dns.impl;

import io.netty.resolver.AddressResolverGroup;
import io.netty.resolver.DefaultAddressResolverGroup;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.dns.AddressResolverOptions;
import io.vertx.core.spi.resolver.ResolverProvider;
import io.vertx.core.spi.dns.AddressResolverProvider;

import java.net.InetSocketAddress;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class DefaultResolverProvider implements ResolverProvider {
public class DefaultAddressResolverProvider implements AddressResolverProvider {

@Override
public AddressResolverGroup<InetSocketAddress> resolver(AddressResolverOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/

package io.vertx.core.impl.resolver;
package io.vertx.core.dns.impl;

import io.netty.channel.EventLoop;
import io.netty.channel.socket.SocketChannel;
Expand All @@ -22,14 +22,13 @@
import io.vertx.core.impl.AddressResolver;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.spi.resolver.ResolverProvider;
import io.vertx.core.spi.dns.AddressResolverProvider;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.net.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -38,10 +37,10 @@
/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class DnsResolverProvider implements ResolverProvider, HostsFileEntriesResolver {
public class DnsAddressResolverProvider implements AddressResolverProvider, HostsFileEntriesResolver {

public static DnsResolverProvider create(VertxInternal vertx, AddressResolverOptions options) {
DnsResolverProvider provider = new DnsResolverProvider(vertx, options);
public static DnsAddressResolverProvider create(VertxInternal vertx, AddressResolverOptions options) {
DnsAddressResolverProvider provider = new DnsAddressResolverProvider(vertx, options);
provider.refresh();
return provider;
}
Expand All @@ -56,7 +55,7 @@ public static DnsResolverProvider create(VertxInternal vertx, AddressResolverOpt
private final long hostsRefreshPeriodNanos;
private volatile HostsFileEntries parsedHostsFile = new HostsFileEntries(Collections.emptyMap(), Collections.emptyMap());

private DnsResolverProvider(VertxInternal vertx, AddressResolverOptions options) {
private DnsAddressResolverProvider(VertxInternal vertx, AddressResolverOptions options) {
List<String> dnsServers = options.getServers();
if (dnsServers != null && dnsServers.size() > 0) {
for (String dnsServer : dnsServers) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/io/vertx/core/impl/AddressResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import io.vertx.core.impl.future.PromiseInternal;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.impl.resolver.DnsResolverProvider;
import io.vertx.core.spi.resolver.ResolverProvider;
import io.vertx.core.spi.dns.AddressResolverProvider;

import java.io.File;
import java.net.InetAddress;
Expand Down Expand Up @@ -74,10 +73,10 @@ private static Pattern resolvOption(String regex) {

private final Vertx vertx;
private final AddressResolverGroup<InetSocketAddress> resolverGroup;
private final ResolverProvider provider;
private final AddressResolverProvider provider;

public AddressResolver(Vertx vertx, AddressResolverOptions options) {
this.provider = ResolverProvider.factory(vertx, options);
this.provider = AddressResolverProvider.factory(vertx, options);
this.resolverGroup = provider.resolver(options);
this.vertx = vertx;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/vertx/core/impl/VertxImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import io.vertx.core.impl.future.PromiseInternal;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.impl.resolver.DnsResolverProvider;
import io.vertx.core.dns.impl.DnsAddressResolverProvider;
import io.vertx.core.spi.transport.Transport;
import io.vertx.core.shareddata.SharedData;
import io.vertx.core.shareddata.impl.SharedDataImpl;
Expand Down Expand Up @@ -518,7 +518,7 @@ public DnsClient createDnsClient(DnsClientOptions options) {
String host = options.getHost();
int port = options.getPort();
if (host == null || port < 0) {
DnsResolverProvider provider = DnsResolverProvider.create(this, addressResolverOptions);
DnsAddressResolverProvider provider = DnsAddressResolverProvider.create(this, addressResolverOptions);
InetSocketAddress address = provider.nameServerAddresses().get(0);
// provide the host and port
options = new DnsClientOptions(options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/

package io.vertx.core.spi.resolver;
package io.vertx.core.spi.dns;

import io.netty.resolver.AddressResolverGroup;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxException;
import io.vertx.core.dns.AddressResolverOptions;
import io.vertx.core.impl.VertxImpl;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.impl.resolver.DnsResolverProvider;
import io.vertx.core.impl.resolver.DefaultResolverProvider;
import io.vertx.core.dns.impl.DnsAddressResolverProvider;
import io.vertx.core.dns.impl.DefaultAddressResolverProvider;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;

Expand All @@ -29,25 +27,25 @@
/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public interface ResolverProvider {
public interface AddressResolverProvider {

String DISABLE_DNS_RESOLVER_PROP_NAME = "vertx.disableDnsResolver";

static ResolverProvider factory(Vertx vertx, AddressResolverOptions options) {
static AddressResolverProvider factory(Vertx vertx, AddressResolverOptions options) {
// For now not really plugable, we just want to not fail when we can't load the async provider
// that use an unstable API and fallback on the default (blocking) provider
try {
if (!Boolean.getBoolean(DISABLE_DNS_RESOLVER_PROP_NAME)) {
return DnsResolverProvider.create((VertxInternal) vertx, options);
return DnsAddressResolverProvider.create((VertxInternal) vertx, options);
}
} catch (Throwable e) {
if (e instanceof VertxException) {
throw e;
}
Logger logger = LoggerFactory.getLogger(ResolverProvider.class);
Logger logger = LoggerFactory.getLogger(AddressResolverProvider.class);
logger.info("Using the default address resolver as the dns resolver could not be loaded");
}
return new DefaultResolverProvider();
return new DefaultAddressResolverProvider();
}

AddressResolverGroup<InetSocketAddress> resolver(AddressResolverOptions options);
Expand Down

0 comments on commit 280f4cf

Please sign in to comment.