Skip to content

Commit

Permalink
imlement issue #480 : UDP target host address is cached
Browse files Browse the repository at this point in the history
  • Loading branch information
lxhoan committed Aug 29, 2018
1 parent 24c5542 commit d84124a
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -78,7 +79,7 @@ public class InfluxDBImpl implements InfluxDB {
*/
private static final LogLevel LOG_LEVEL = LogLevel.parseLogLevel(System.getProperty(LOG_LEVEL_PROPERTY));

private final InetAddress hostAddress;
private final String hostName;
private String version;
private final Retrofit retrofit;
private final InfluxDBService influxDBService;
Expand Down Expand Up @@ -116,7 +117,7 @@ public class InfluxDBImpl implements InfluxDB {
public InfluxDBImpl(final String url, final String username, final String password, final OkHttpClient.Builder client,
final ResponseFormat responseFormat) {
this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat);
this.hostAddress = parseHostAddress(url);
this.hostName = parseHost(url);

this.loggingInterceptor = new HttpLoggingInterceptor();
setLogLevel(LOG_LEVEL);
Expand Down Expand Up @@ -162,7 +163,7 @@ public InfluxDBImpl(final String url, final String username, final String passwo
final InfluxDBService influxDBService, final JsonAdapter<QueryResult> adapter) {
super();
this.messagePack = false;
this.hostAddress = parseHostAddress(url);
this.hostName = parseHost(url);

this.loggingInterceptor = new HttpLoggingInterceptor();
setLogLevel(LOG_LEVEL);
Expand All @@ -187,18 +188,20 @@ public InfluxDBImpl(final String url, final String username, final String passwo
setRetentionPolicy(retentionPolicy);
}

private InetAddress parseHostAddress(final String url) {
HttpUrl httpUrl = HttpUrl.parse(url);
private String parseHost(final String url) {
HttpUrl httpUrl = HttpUrl.parse(url);

if (httpUrl == null) {
throw new IllegalArgumentException("Unable to parse url: " + url);
}
if (httpUrl == null) {
throw new IllegalArgumentException("Unable to parse url: " + url);
}

try {
return InetAddress.getByName(httpUrl.host());
} catch (UnknownHostException e) {
throw new InfluxDBIOException(e);
}
String hostName = httpUrl.host();
try {
InetAddress.getByName(hostName);
} catch (UnknownHostException e) {
throw new InfluxDBIOException(e);
}
return hostName;
}

@Override
Expand Down Expand Up @@ -465,7 +468,7 @@ 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));
datagramSocket.send(new DatagramPacket(bytes, bytes.length, new InetSocketAddress(hostName, udpPort)));
} catch (IOException e) {
throw new InfluxDBIOException(e);
}
Expand Down

0 comments on commit d84124a

Please sign in to comment.