Skip to content

Commit 4e5c574

Browse files
committed
Fixed NTP socket timeout
1 parent b933939 commit 4e5c574

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/core/TimeProvider.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
import java.io.IOException;
1414
import java.net.InetAddress;
15+
import java.net.SocketTimeoutException;
1516
import java.nio.ByteBuffer;
17+
import java.util.concurrent.TimeUnit;
1618
import java.util.concurrent.atomic.AtomicLong;
1719

1820
/**
@@ -61,14 +63,19 @@ public static long currentTimeMillis() {
6163
}
6264

6365
private static void updateWithNtp() throws IOException {
64-
synchronized (offset) {
65-
NTPUDPClient client = new NTPUDPClient();
66-
client.open();
67-
TimeInfo info = client.getTime(InetAddress.getByName("time.google.com"));
68-
info.computeDetails();
69-
Long offsetValue = info.getOffset();
70-
LOGGER.debug(String.format("Loaded time offset from NTP: %dms", offsetValue));
71-
offset.set(offsetValue == null ? 0 : offsetValue);
66+
try {
67+
synchronized (offset) {
68+
NTPUDPClient client = new NTPUDPClient();
69+
client.open();
70+
client.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10));
71+
TimeInfo info = client.getTime(InetAddress.getByName("time.google.com"));
72+
info.computeDetails();
73+
Long offsetValue = info.getOffset();
74+
LOGGER.debug(String.format("Loaded time offset from NTP: %dms", offsetValue));
75+
offset.set(offsetValue == null ? 0 : offsetValue);
76+
}
77+
} catch (SocketTimeoutException ex) {
78+
updateWithNtp();
7279
}
7380
}
7481

0 commit comments

Comments
 (0)