Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/main/java/org/influxdb/InfluxDBFactory.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down