Skip to content

Commit 1c9e2cd

Browse files
sagaxuvietj
authored andcommitted
Introduce a time unit option that defaults to nanoseconds for hostsRefreshPeriod of AddressResolverOptions
See #5666
1 parent 65c2d64 commit 1c9e2cd

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

vertx-core/src/main/generated/io/vertx/core/dns/AddressResolverOptionsConverter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, Address
2222
obj.setHostsValue(io.vertx.core.buffer.Buffer.fromJson((String)member.getValue()));
2323
}
2424
break;
25+
case "hostsRefreshPeriodUnit":
26+
if (member.getValue() instanceof String) {
27+
obj.setHostsRefreshPeriodUnit(java.util.concurrent.TimeUnit.valueOf((String)member.getValue()));
28+
}
29+
break;
2530
case "hostsRefreshPeriod":
2631
if (member.getValue() instanceof Number) {
2732
obj.setHostsRefreshPeriod(((Number)member.getValue()).intValue());
@@ -112,6 +117,9 @@ static void toJson(AddressResolverOptions obj, java.util.Map<String, Object> jso
112117
if (obj.getHostsValue() != null) {
113118
json.put("hostsValue", obj.getHostsValue().toJson());
114119
}
120+
if (obj.getHostsRefreshPeriodUnit() != null) {
121+
json.put("hostsRefreshPeriodUnit", obj.getHostsRefreshPeriodUnit().name());
122+
}
115123
json.put("hostsRefreshPeriod", obj.getHostsRefreshPeriod());
116124
if (obj.getServers() != null) {
117125
JsonArray array = new JsonArray();

vertx-core/src/main/java/io/vertx/core/dns/AddressResolverOptions.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.File;
2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import java.util.concurrent.TimeUnit;
2324

2425
import static io.vertx.core.impl.Utils.isLinux;
2526
import static io.vertx.core.internal.resolver.NameResolver.parseLinux;
@@ -76,7 +77,12 @@ public class AddressResolverOptions {
7677
public static final int DEFAULT_QUERY_TIMEOUT = 5000;
7778

7879
/**
79-
* The default value for the hosts refresh value in nanos = 0 (disabled)
80+
* The default time unit for the hosts refresh value = NANOSECONDS
81+
*/
82+
public static final TimeUnit DEFAULT_HOSTS_REFRESH_PERIOD_UNIT = TimeUnit.NANOSECONDS;
83+
84+
/**
85+
* The default value for the hosts refresh value = 0 (disabled)
8086
*/
8187
public static final int DEFAULT_HOSTS_REFRESH_PERIOD = 0;
8288

@@ -112,6 +118,7 @@ public class AddressResolverOptions {
112118

113119
private String hostsPath;
114120
private Buffer hostsValue;
121+
private TimeUnit hostsRefreshPeriodUnit;
115122
private int hostsRefreshPeriod;
116123
private List<String> servers;
117124
private boolean optResourceEnabled;
@@ -139,12 +146,14 @@ public AddressResolverOptions() {
139146
ndots = DEFAULT_NDOTS;
140147
rotateServers = DEFAULT_ROTATE_SERVERS;
141148
roundRobinInetAddress = DEFAULT_ROUND_ROBIN_INET_ADDRESS;
149+
hostsRefreshPeriodUnit = DEFAULT_HOSTS_REFRESH_PERIOD_UNIT;
142150
hostsRefreshPeriod = DEFAULT_HOSTS_REFRESH_PERIOD;
143151
}
144152

145153
public AddressResolverOptions(AddressResolverOptions other) {
146154
this.hostsPath = other.hostsPath;
147155
this.hostsValue = other.hostsValue != null ? other.hostsValue.copy() : null;
156+
this.hostsRefreshPeriodUnit = other.getHostsRefreshPeriodUnit() != null ? other.getHostsRefreshPeriodUnit() : DEFAULT_HOSTS_REFRESH_PERIOD_UNIT;
148157
this.hostsRefreshPeriod = other.hostsRefreshPeriod;
149158
this.servers = other.servers != null ? new ArrayList<>(other.servers) : null;
150159
this.optResourceEnabled = other.optResourceEnabled;
@@ -207,6 +216,24 @@ public AddressResolverOptions setHostsValue(Buffer hostsValue) {
207216
return this;
208217
}
209218

219+
/**
220+
* @return the hosts configuration refresh period time unit
221+
*/
222+
public TimeUnit getHostsRefreshPeriodUnit() {
223+
return hostsRefreshPeriodUnit;
224+
}
225+
226+
/**
227+
* Set the hosts configuration refresh period time unit. If not specified, default is nanoseconds.
228+
*
229+
* @param hostsRefreshPeriodUnit specify time unit
230+
* @return a reference to this, so the API can be used fluently
231+
*/
232+
public AddressResolverOptions setHostsRefreshPeriodUnit(TimeUnit hostsRefreshPeriodUnit) {
233+
this.hostsRefreshPeriodUnit = hostsRefreshPeriodUnit;
234+
return this;
235+
}
236+
210237
/**
211238
* @return the hosts configuration refresh period in nanos
212239
*/

vertx-core/src/main/java/io/vertx/core/dns/impl/DnsAddressResolverProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private DnsAddressResolverProvider(VertxInternal vertx, AddressResolverOptions o
100100
this.vertx = vertx;
101101
this.hostsPath = options.getHostsPath();
102102
this.hostsValue = options.getHostsValue();
103-
this.hostsRefreshPeriodNanos = options.getHostsRefreshPeriod();
103+
this.hostsRefreshPeriodNanos = options.getHostsRefreshPeriodUnit().toNanos(options.getHostsRefreshPeriod());
104104

105105
DnsNameResolverBuilder builder = new DnsNameResolverBuilder();
106106
builder.hostsFileEntriesResolver(this);

vertx-core/src/test/java/io/vertx/tests/dns/NameResolverTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,25 +431,42 @@ public void testTrailingDotResolveFromHosts() {
431431
@Test
432432
public void testRefreshHosts1() throws Exception {
433433
Assume.assumeFalse(Utils.isWindows());
434-
InetAddress addr = testRefreshHosts((int) TimeUnit.SECONDS.toNanos(1));
434+
InetAddress addr = testRefreshHosts((int) TimeUnit.SECONDS.toNanos(1), null);
435435
assertEquals("192.168.0.16", addr.getHostAddress());
436436
assertEquals("server.net", addr.getHostName());
437437
}
438438

439439
@Test
440440
public void testRefreshHosts2() throws Exception {
441-
InetAddress addr = testRefreshHosts(0);
441+
InetAddress addr = testRefreshHosts(0, null);
442442
assertEquals("192.168.0.15", addr.getHostAddress());
443443
assertEquals("server.net", addr.getHostName());
444444
}
445445

446-
private InetAddress testRefreshHosts(int period) throws Exception {
446+
@Test
447+
public void testRefreshHosts3() throws Exception {
448+
InetAddress addr = testRefreshHosts(100, TimeUnit.MILLISECONDS);
449+
assertEquals("192.168.0.16", addr.getHostAddress());
450+
assertEquals("server.net", addr.getHostName());
451+
}
452+
453+
@Test
454+
public void testRefreshHosts4() throws Exception {
455+
InetAddress addr = testRefreshHosts(2, TimeUnit.SECONDS);
456+
assertEquals("192.168.0.15", addr.getHostAddress());
457+
assertEquals("server.net", addr.getHostName());
458+
}
459+
460+
private InetAddress testRefreshHosts(int period, TimeUnit timeUnit) throws Exception {
447461
File hosts = File.createTempFile("vertx", "hosts");
448462
hosts.deleteOnExit();
449463
Files.writeString(hosts.toPath(), "192.168.0.15 server.net");
450464
AddressResolverOptions options = new AddressResolverOptions()
451465
.setHostsPath(hosts.getAbsolutePath())
452466
.setHostsRefreshPeriod(period);
467+
if (timeUnit != null) {
468+
options.setHostsRefreshPeriodUnit(timeUnit);
469+
}
453470
VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(options));
454471
InetAddress addr = awaitFuture(resolve(vertx, "server.net"));
455472
assertEquals("192.168.0.15", addr.getHostAddress());

0 commit comments

Comments
 (0)