Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Apr 11, 2016
1 parent 8ce5303 commit e1a8ed3
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 150 deletions.
28 changes: 28 additions & 0 deletions src/main/asciidoc/dataobjects.adoc
Expand Up @@ -1550,11 +1550,39 @@ Add a certificate value
[frame="topbot"] [frame="topbot"]
|=== |===
^|Name | Type ^| Description ^|Name | Type ^| Description
|[[cacheMaxTimeToLive]]`cacheMaxTimeToLive`|`Number (int)`|
+++
Set the cache maximum TTL value in seconds. After successful resolution IP addresses are cached with their DNS response TTL,
use this to set a maximum value to all responses TTL.
+++
|[[cacheMinTimeToLive]]`cacheMinTimeToLive`|`Number (int)`|
+++
Set the cache minimum TTL value in seconds. After resolution successful IP addresses are cached with their DNS response TTL,
use this to set a minimum value to all responses TTL.
+++
|[[cacheNegativeTimeToLive]]`cacheNegativeTimeToLive`|`Number (int)`|
+++
Set the negative cache TTL value in seconds. After a failed hostname resolution, DNS queries won't be retried
for a period of time equals to the negative TTL. This allows to reduce the response time of negative replies
and reduce the amount of messages to DNS servers.
+++
|[[maxQueries]]`maxQueries`|`Number (int)`|
+++
Set the maximum number of queries when an hostname is resolved.
+++
|[[optResourceEnabled]]`optResourceEnabled`|`Boolean`| |[[optResourceEnabled]]`optResourceEnabled`|`Boolean`|
+++ +++
Set to true to enable the automatic inclusion in DNS queries of an optional record that hints Set to true to enable the automatic inclusion in DNS queries of an optional record that hints
the remote DNS server about how much data the resolver can read per response. the remote DNS server about how much data the resolver can read per response.
+++ +++
|[[queryTimeout]]`queryTimeout`|`Number (long)`|
+++
Set the query timeout in milliseconds, i.e the amount of time after a query is considered to be failed.
+++
|[[rdFlag]]`rdFlag`|`Boolean`|
+++
Set the DNS queries <i>Recursion Desired</i> flag value.
+++
|[[servers]]`servers`|`Array of String`| |[[servers]]`servers`|`Array of String`|
+++ +++
Set the list of DNS server addresses, an address is the IP of the dns server, followed by an optional Set the list of DNS server addresses, an address is the IP of the dns server, followed by an optional
Expand Down
Expand Up @@ -27,9 +27,27 @@
public class HostnameResolverOptionsConverter { public class HostnameResolverOptionsConverter {


public static void fromJson(JsonObject json, HostnameResolverOptions obj) { public static void fromJson(JsonObject json, HostnameResolverOptions obj) {
if (json.getValue("cacheMaxTimeToLive") instanceof Number) {
obj.setCacheMaxTimeToLive(((Number)json.getValue("cacheMaxTimeToLive")).intValue());
}
if (json.getValue("cacheMinTimeToLive") instanceof Number) {
obj.setCacheMinTimeToLive(((Number)json.getValue("cacheMinTimeToLive")).intValue());
}
if (json.getValue("cacheNegativeTimeToLive") instanceof Number) {
obj.setCacheNegativeTimeToLive(((Number)json.getValue("cacheNegativeTimeToLive")).intValue());
}
if (json.getValue("maxQueries") instanceof Number) {
obj.setMaxQueries(((Number)json.getValue("maxQueries")).intValue());
}
if (json.getValue("optResourceEnabled") instanceof Boolean) { if (json.getValue("optResourceEnabled") instanceof Boolean) {
obj.setOptResourceEnabled((Boolean)json.getValue("optResourceEnabled")); obj.setOptResourceEnabled((Boolean)json.getValue("optResourceEnabled"));
} }
if (json.getValue("queryTimeout") instanceof Number) {
obj.setQueryTimeout(((Number)json.getValue("queryTimeout")).longValue());
}
if (json.getValue("rdFlag") instanceof Boolean) {
obj.setRdFlag((Boolean)json.getValue("rdFlag"));
}
if (json.getValue("servers") instanceof JsonArray) { if (json.getValue("servers") instanceof JsonArray) {
json.getJsonArray("servers").forEach(item -> { json.getJsonArray("servers").forEach(item -> {
if (item instanceof String) if (item instanceof String)
Expand All @@ -39,7 +57,13 @@ public static void fromJson(JsonObject json, HostnameResolverOptions obj) {
} }


public static void toJson(HostnameResolverOptions obj, JsonObject json) { public static void toJson(HostnameResolverOptions obj, JsonObject json) {
json.put("cacheMaxTimeToLive", obj.getCacheMaxTimeToLive());
json.put("cacheMinTimeToLive", obj.getCacheMinTimeToLive());
json.put("cacheNegativeTimeToLive", obj.getCacheNegativeTimeToLive());
json.put("maxQueries", obj.getMaxQueries());
json.put("optResourceEnabled", obj.isOptResourceEnabled()); json.put("optResourceEnabled", obj.isOptResourceEnabled());
json.put("queryTimeout", obj.getQueryTimeout());
json.put("rdFlag", obj.getRdFlag());
if (obj.getServers() != null) { if (obj.getServers() != null) {
json.put("servers", new JsonArray( json.put("servers", new JsonArray(
obj.getServers(). obj.getServers().
Expand Down
158 changes: 155 additions & 3 deletions src/main/java/io/vertx/core/dns/HostnameResolverOptions.java
Expand Up @@ -33,25 +33,53 @@ public class HostnameResolverOptions {
/** /**
* The default list of DNS servers = null (uses system name server's list like resolve.conf otherwise Google Public DNS) * The default list of DNS servers = null (uses system name server's list like resolve.conf otherwise Google Public DNS)
*/ */
public static final List<String> DEFAULT_DNS_SERVERS = null; public static final List<String> DEFAULT_SERVERS = null;


/** /**
* The default value for {@link #setOptResourceEnabled} = true * The default value for {@link #setOptResourceEnabled} = true
*/ */
public static final boolean DEFAULT_OPT_RESOURCE_ENABLED = true; public static final boolean DEFAULT_OPT_RESOURCE_ENABLED = true;


private List<String> servers = DEFAULT_DNS_SERVERS; public static final int DEFAULT_CACHE_MIN_TIME_TO_LIVE = 0;
private boolean optResourceEnabled = DEFAULT_OPT_RESOURCE_ENABLED; public static final int DEFAULT_CACHE_MAX_TIME_TO_LIVE = Integer.MAX_VALUE;
public static final int DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE = 0;
public static final int DEFAULT_QUERY_TIMEOUT = 5000;
public static final int DEFAULT_MAX_QUERIES = 3;
public static final boolean DEFAULT_RD_FLAG = true;

private List<String> servers;
private boolean optResourceEnabled;
private int cacheMinTimeToLive;
private int cacheMaxTimeToLive;
private int cacheNegativeTimeToLive;
private long queryTimeout;
private int maxQueries;
private boolean rdFlag;


public HostnameResolverOptions() { public HostnameResolverOptions() {
servers = DEFAULT_SERVERS;
optResourceEnabled = DEFAULT_OPT_RESOURCE_ENABLED;
cacheMinTimeToLive = DEFAULT_CACHE_MIN_TIME_TO_LIVE;
cacheMaxTimeToLive = DEFAULT_CACHE_MAX_TIME_TO_LIVE;
cacheNegativeTimeToLive = DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE;
queryTimeout = DEFAULT_QUERY_TIMEOUT;
maxQueries = DEFAULT_MAX_QUERIES;
rdFlag = DEFAULT_RD_FLAG;
} }


public HostnameResolverOptions(HostnameResolverOptions other) { public HostnameResolverOptions(HostnameResolverOptions other) {
this.servers = other.servers != null ? new ArrayList<>(other.servers) : null; this.servers = other.servers != null ? new ArrayList<>(other.servers) : null;
this.optResourceEnabled = other.optResourceEnabled; this.optResourceEnabled = other.optResourceEnabled;
this.cacheMinTimeToLive = other.cacheMinTimeToLive;
this.cacheMaxTimeToLive = other.cacheMaxTimeToLive;
this.cacheNegativeTimeToLive = other.cacheNegativeTimeToLive;
this.queryTimeout = other.queryTimeout;
this.maxQueries = other.maxQueries;
this.rdFlag = other.rdFlag;
} }


public HostnameResolverOptions(JsonObject json) { public HostnameResolverOptions(JsonObject json) {
this();
HostnameResolverOptionsConverter.fromJson(json, this); HostnameResolverOptionsConverter.fromJson(json, this);
} }


Expand Down Expand Up @@ -109,19 +137,143 @@ public HostnameResolverOptions setOptResourceEnabled(boolean optResourceEnabled)
return this; return this;
} }


/**
* @return the cache min TTL in seconds
*/
public int getCacheMinTimeToLive() {
return cacheMinTimeToLive;
}

/**
* Set the cache minimum TTL value in seconds. After resolution successful IP addresses are cached with their DNS response TTL,
* use this to set a minimum value to all responses TTL.
*
* @param cacheMinTimeToLive the cache min TTL in seconds
* @return a reference to this, so the API can be used fluently
*/
public HostnameResolverOptions setCacheMinTimeToLive(int cacheMinTimeToLive) {
this.cacheMinTimeToLive = cacheMinTimeToLive;
return this;
}

/**
* @return the cache max TTL in seconds
*/
public int getCacheMaxTimeToLive() {
return cacheMaxTimeToLive;
}

/**
* Set the cache maximum TTL value in seconds. After successful resolution IP addresses are cached with their DNS response TTL,
* use this to set a maximum value to all responses TTL.
*
* @param cacheMaxTimeToLive the cache max TTL in seconds
* @return a reference to this, so the API can be used fluently
*/
public HostnameResolverOptions setCacheMaxTimeToLive(int cacheMaxTimeToLive) {
this.cacheMaxTimeToLive = cacheMaxTimeToLive;
return this;
}

/**
* @return the cache negative TTL in seconds
*/
public int getCacheNegativeTimeToLive() {
return cacheNegativeTimeToLive;
}

/**
* Set the negative cache TTL value in seconds. After a failed hostname resolution, DNS queries won't be retried
* for a period of time equals to the negative TTL. This allows to reduce the response time of negative replies
* and reduce the amount of messages to DNS servers.
*
* @param cacheNegativeTimeToLive the cache negative TTL in seconds
* @return a reference to this, so the API can be used fluently
*/
public HostnameResolverOptions setCacheNegativeTimeToLive(int cacheNegativeTimeToLive) {
this.cacheNegativeTimeToLive = cacheNegativeTimeToLive;
return this;
}

/**
* @return the query timeout in milliseconds
*/
public long getQueryTimeout() {
return queryTimeout;
}

/**
* Set the query timeout in milliseconds, i.e the amount of time after a query is considered to be failed.
*
* @param queryTimeout the query timeout in milliseconds
* @return a reference to this, so the API can be used fluently
*/
public HostnameResolverOptions setQueryTimeout(long queryTimeout) {
this.queryTimeout = queryTimeout;
return this;
}

/**
* @return the maximum number of queries to be sent during a resolution
*/
public int getMaxQueries() {
return maxQueries;
}

/**
* Set the maximum number of queries when an hostname is resolved.
*
* @param maxQueries the max number of queries to be sent
* @return a reference to this, so the API can be used fluently
*/
public HostnameResolverOptions setMaxQueries(int maxQueries) {
this.maxQueries = maxQueries;
return this;
}

/**
* @return the DNS queries <i>Recursion Desired</i> flag value
*/
public boolean getRdFlag() {
return rdFlag;
}

/**
* Set the DNS queries <i>Recursion Desired</i> flag value.
*
* @param rdFlag the flag value
* @return a reference to this, so the API can be used fluently
*/
public HostnameResolverOptions setRdFlag(boolean rdFlag) {
this.rdFlag = rdFlag;
return this;
}

@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
HostnameResolverOptions that = (HostnameResolverOptions) o; HostnameResolverOptions that = (HostnameResolverOptions) o;
if (optResourceEnabled != that.optResourceEnabled) return false; if (optResourceEnabled != that.optResourceEnabled) return false;
if (cacheMinTimeToLive != that.cacheMinTimeToLive) return false;
if (cacheMaxTimeToLive != that.cacheMaxTimeToLive) return false;
if (cacheNegativeTimeToLive != that.cacheNegativeTimeToLive) return false;
if (queryTimeout != that.queryTimeout) return false;
if (maxQueries != that.maxQueries) return false;
if (rdFlag != that.rdFlag) return false;
return servers != null ? servers.equals(that.servers) : that.servers == null; return servers != null ? servers.equals(that.servers) : that.servers == null;
} }


@Override @Override
public int hashCode() { public int hashCode() {
int result = optResourceEnabled ? 1 : 0; int result = optResourceEnabled ? 1 : 0;
result = 31 * result + (servers != null ? servers.hashCode() : 0); result = 31 * result + (servers != null ? servers.hashCode() : 0);
result = 31 * result + cacheMinTimeToLive;
result = 31 * result + cacheMaxTimeToLive;
result = 31 * result + cacheNegativeTimeToLive;
result = 31 * result + Long.hashCode(queryTimeout);
result = 31 * result + maxQueries;
result = 31 * result + Boolean.hashCode(rdFlag);
return result; return result;
} }


Expand Down
21 changes: 14 additions & 7 deletions src/main/java/io/vertx/core/impl/VertxImpl.java
Expand Up @@ -149,9 +149,10 @@ public class VertxImpl implements VertxInternal, MetricsProvider {


DnsNameResolverBuilder builder = new DnsNameResolverBuilder(createEventLoopContext(null, new JsonObject(), Thread.currentThread().getContextClassLoader()).nettyEventLoop()); DnsNameResolverBuilder builder = new DnsNameResolverBuilder(createEventLoopContext(null, new JsonObject(), Thread.currentThread().getContextClassLoader()).nettyEventLoop());
builder.channelFactory(NioDatagramChannel::new); builder.channelFactory(NioDatagramChannel::new);
HostnameResolverOptions dnsOptions = options.getHostnameResolverOptions(); HostnameResolverOptions hostnameResolver = options.getHostnameResolverOptions();
if (dnsOptions != null) { DnsServerAddresses nameServerAddresses = DnsServerAddresses.defaultAddresses();
List<String> dnsServers = dnsOptions.getServers(); if (hostnameResolver != null) {
List<String> dnsServers = hostnameResolver.getServers();
if (dnsServers != null && dnsServers.size() > 0) { if (dnsServers != null && dnsServers.size() > 0) {
List<InetSocketAddress> serverList = new ArrayList<>(); List<InetSocketAddress> serverList = new ArrayList<>();
for (String dnsServer : dnsServers) { for (String dnsServer : dnsServers) {
Expand All @@ -171,11 +172,17 @@ public class VertxImpl implements VertxInternal, MetricsProvider {
throw new VertxException(e); throw new VertxException(e);
} }
} }
builder.nameServerAddresses(DnsServerAddresses.sequential(serverList)); nameServerAddresses = DnsServerAddresses.sequential(serverList);
builder.nameServerAddresses(nameServerAddresses);
} }
builder.optResourceEnabled(dnsOptions.isOptResourceEnabled()); builder.optResourceEnabled(hostnameResolver.isOptResourceEnabled());
} builder.ttl(hostnameResolver.getCacheMinTimeToLive(), hostnameResolver.getCacheMaxTimeToLive());
resolver = builder.build(); builder.negativeTtl(hostnameResolver.getCacheNegativeTimeToLive());
builder.queryTimeoutMillis(hostnameResolver.getQueryTimeout());
builder.maxQueriesPerResolve(hostnameResolver.getMaxQueries());
builder.recursionDesired(hostnameResolver.getRdFlag());
}
this.resolver = builder.build();


this.fileResolver = new FileResolver(this); this.fileResolver = new FileResolver(this);
this.deploymentManager = new DeploymentManager(this); this.deploymentManager = new DeploymentManager(this);
Expand Down

0 comments on commit e1a8ed3

Please sign in to comment.