Skip to content

Commit bebe7c3

Browse files
authored
Merge pull request #502 from bonitoo-io/issue_480_1
imlement issue #480 : UDP target host address is cached
2 parents f6879f7 + 9434330 commit bebe7c3

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/main/java/org/influxdb/impl/InfluxDBImpl.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.squareup.moshi.JsonAdapter;
55
import com.squareup.moshi.Moshi;
66
import okhttp3.Headers;
7-
import okhttp3.HttpUrl;
87
import okhttp3.MediaType;
98
import okhttp3.OkHttpClient;
109
import okhttp3.Request;
@@ -42,7 +41,10 @@
4241
import java.net.DatagramPacket;
4342
import java.net.DatagramSocket;
4443
import java.net.InetAddress;
44+
import java.net.InetSocketAddress;
4545
import java.net.SocketException;
46+
import java.net.URI;
47+
import java.net.URISyntaxException;
4648
import java.net.UnknownHostException;
4749
import java.nio.charset.StandardCharsets;
4850
import java.util.ArrayList;
@@ -78,7 +80,7 @@ public class InfluxDBImpl implements InfluxDB {
7880
*/
7981
private static final LogLevel LOG_LEVEL = LogLevel.parseLogLevel(System.getProperty(LOG_LEVEL_PROPERTY));
8082

81-
private final InetAddress hostAddress;
83+
private final String hostName;
8284
private String version;
8385
private final Retrofit retrofit;
8486
private final InfluxDBService influxDBService;
@@ -116,7 +118,7 @@ public class InfluxDBImpl implements InfluxDB {
116118
public InfluxDBImpl(final String url, final String username, final String password, final OkHttpClient.Builder client,
117119
final ResponseFormat responseFormat) {
118120
this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat);
119-
this.hostAddress = parseHostAddress(url);
121+
this.hostName = parseHost(url);
120122

121123
this.loggingInterceptor = new HttpLoggingInterceptor();
122124
setLogLevel(LOG_LEVEL);
@@ -162,7 +164,7 @@ public InfluxDBImpl(final String url, final String username, final String passwo
162164
final InfluxDBService influxDBService, final JsonAdapter<QueryResult> adapter) {
163165
super();
164166
this.messagePack = false;
165-
this.hostAddress = parseHostAddress(url);
167+
this.hostName = parseHost(url);
166168

167169
this.loggingInterceptor = new HttpLoggingInterceptor();
168170
setLogLevel(LOG_LEVEL);
@@ -187,18 +189,25 @@ public InfluxDBImpl(final String url, final String username, final String passwo
187189
setRetentionPolicy(retentionPolicy);
188190
}
189191

190-
private InetAddress parseHostAddress(final String url) {
191-
HttpUrl httpUrl = HttpUrl.parse(url);
192+
private String parseHost(final String url) {
193+
String hostName;
194+
try {
195+
URI uri = new URI(url);
196+
hostName = uri.getHost();
197+
} catch (URISyntaxException e1) {
198+
throw new IllegalArgumentException("Unable to parse url: " + url, e1);
199+
}
192200

193-
if (httpUrl == null) {
194-
throw new IllegalArgumentException("Unable to parse url: " + url);
195-
}
201+
if (hostName == null) {
202+
throw new IllegalArgumentException("Unable to parse url: " + url);
203+
}
196204

197-
try {
198-
return InetAddress.getByName(httpUrl.host());
199-
} catch (UnknownHostException e) {
200-
throw new InfluxDBIOException(e);
201-
}
205+
try {
206+
InetAddress.getByName(hostName);
207+
} catch (UnknownHostException e) {
208+
throw new InfluxDBIOException(e);
209+
}
210+
return hostName;
202211
}
203212

204213
@Override
@@ -465,7 +474,7 @@ public void write(final int udpPort, final String records) {
465474
initialDatagramSocket();
466475
byte[] bytes = records.getBytes(StandardCharsets.UTF_8);
467476
try {
468-
datagramSocket.send(new DatagramPacket(bytes, bytes.length, hostAddress, udpPort));
477+
datagramSocket.send(new DatagramPacket(bytes, bytes.length, new InetSocketAddress(hostName, udpPort)));
469478
} catch (IOException e) {
470479
throw new InfluxDBIOException(e);
471480
}

src/test/java/org/influxdb/InfluxDBTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,24 @@ public void testWrongHostForInfluxdb(){
645645
Assertions.assertThrows(RuntimeException.class, () -> {
646646
InfluxDBFactory.connect("http://" + errorHost + ":" + TestUtils.getInfluxPORT(true));
647647
});
648+
649+
String unresolvableHost = "a.b.c";
650+
Assertions.assertThrows(InfluxDBIOException.class, () -> {
651+
InfluxDBFactory.connect("http://" + unresolvableHost + ":" + TestUtils.getInfluxPORT(true));
652+
});
648653
}
649654

655+
@Test
656+
public void testInvalidUrlHandling(){
657+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
658+
InfluxDBFactory.connect("@@@http://@@@");
659+
});
660+
661+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
662+
InfluxDBFactory.connect("http://@@@abc");
663+
});
664+
}
665+
650666
@Test
651667
public void testBatchEnabledTwice() {
652668
this.influxDB.enableBatch(1, 1, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)