diff --git a/src/main/java/org/influxdb/InfluxDBFactory.java b/src/main/java/org/influxdb/InfluxDBFactory.java index aee28d73a..76d18676c 100644 --- a/src/main/java/org/influxdb/InfluxDBFactory.java +++ b/src/main/java/org/influxdb/InfluxDBFactory.java @@ -1,9 +1,8 @@ package org.influxdb; +import okhttp3.OkHttpClient; import org.influxdb.InfluxDB.ResponseFormat; import org.influxdb.impl.InfluxDBImpl; - -import okhttp3.OkHttpClient; import org.influxdb.impl.Preconditions; import java.util.Objects; diff --git a/src/main/java/org/influxdb/impl/InfluxDBImpl.java b/src/main/java/org/influxdb/impl/InfluxDBImpl.java index 07916cf98..9535eeb20 100644 --- a/src/main/java/org/influxdb/impl/InfluxDBImpl.java +++ b/src/main/java/org/influxdb/impl/InfluxDBImpl.java @@ -45,6 +45,8 @@ import java.net.SocketException; import java.net.UnknownHostException; import java.nio.charset.StandardCharsets; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executors; @@ -76,7 +78,14 @@ public class InfluxDBImpl implements InfluxDB { */ private static final LogLevel LOG_LEVEL = LogLevel.parseLogLevel(System.getProperty(LOG_LEVEL_PROPERTY)); - private final InetAddress hostAddress; + /** + * How often (in seconds) the address will be looked up when writing over UDP. + */ + private static final long ADDRESS_LOOKUP_INTERVAL = 300L; + + private InetAddress hostAddress; + private ZonedDateTime lastAddressLookup = ZonedDateTime.now(ZoneOffset.UTC); + private final String url; private final String username; private final String password; private String version; @@ -116,6 +125,7 @@ public InfluxDBImpl(final String url, final String username, final String passwo final ResponseFormat responseFormat) { this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat); this.hostAddress = parseHostAddress(url); + this.url = url; this.username = username; this.password = password; @@ -164,6 +174,7 @@ public InfluxDBImpl(final String url, final String username, final String passwo super(); this.messagePack = false; this.hostAddress = parseHostAddress(url); + this.url = url; this.username = username; this.password = password; @@ -471,7 +482,12 @@ public void write(final int udpPort, final String records) { initialDatagramSocket(); byte[] bytes = records.getBytes(StandardCharsets.UTF_8); try { - datagramSocket.send(new DatagramPacket(bytes, bytes.length, hostAddress, udpPort)); + // look up the url again + if (this.lastAddressLookup.plusSeconds(ADDRESS_LOOKUP_INTERVAL).isAfter(ZonedDateTime.now(ZoneOffset.UTC))) { + this.hostAddress = this.parseHostAddress(this.url); + } + + datagramSocket.send(new DatagramPacket(bytes, bytes.length, this.hostAddress, udpPort)); } catch (IOException e) { throw new InfluxDBIOException(e); }