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 Sep 5, 2018
1 parent 24c5542 commit b855e09
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -42,7 +41,10 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -78,7 +80,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 +118,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 +164,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 +189,25 @@ 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) {
String hostName;
try {
URI uri = new URI(url);
hostName = uri.getHost();
} catch (URISyntaxException e1) {
throw new IllegalArgumentException("Unable to parse url: " + url, e1);
}

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

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

@Override
Expand Down Expand Up @@ -465,7 +474,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
11 changes: 11 additions & 0 deletions src/test/java/org/influxdb/InfluxDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,17 @@ public void testWrongHostForInfluxdb(){
});
}

@Test
public void testInvalidUrlHandling(){
Assertions.assertThrows(IllegalArgumentException.class, () -> {
InfluxDBFactory.connect("@@@http://@@@");
});

Assertions.assertThrows(IllegalArgumentException.class, () -> {
InfluxDBFactory.connect("http://@@@abc");
});
}

@Test
public void testBatchEnabledTwice() {
this.influxDB.enableBatch(1, 1, TimeUnit.SECONDS);
Expand Down

0 comments on commit b855e09

Please sign in to comment.