forked from I2P_Developers/i2p.i2p
Timestamper: Reduce NTP timeouts to shorten startup time
when NTP is blocked
This commit is contained in:
@@ -55,6 +55,7 @@ class NtpClient {
|
||||
/** difference between the unix epoch and jan 1 1900 (NTP uses that) */
|
||||
private final static double SECONDS_1900_TO_EPOCH = 2208988800.0;
|
||||
private final static int NTP_PORT = 123;
|
||||
private static final int DEFAULT_TIMEOUT = 10*1000;
|
||||
|
||||
/**
|
||||
* Query the ntp servers, returning the current time from first one we find
|
||||
@@ -84,7 +85,7 @@ class NtpClient {
|
||||
* @throws IllegalArgumentException if none of the servers are reachable
|
||||
* @since 0.7.12
|
||||
*/
|
||||
public static long[] currentTimeAndStratum(String serverNames[]) {
|
||||
public static long[] currentTimeAndStratum(String serverNames[], int perServerTimeout) {
|
||||
if (serverNames == null)
|
||||
throw new IllegalArgumentException("No NTP servers specified");
|
||||
ArrayList<String> names = new ArrayList<String>(serverNames.length);
|
||||
@@ -92,7 +93,7 @@ class NtpClient {
|
||||
names.add(serverNames[i]);
|
||||
Collections.shuffle(names);
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
long[] rv = currentTimeAndStratum(names.get(i));
|
||||
long[] rv = currentTimeAndStratum(names.get(i), perServerTimeout);
|
||||
if (rv != null && rv[0] > 0)
|
||||
return rv;
|
||||
}
|
||||
@@ -105,7 +106,7 @@ class NtpClient {
|
||||
* @return milliseconds since january 1, 1970 (UTC), or -1 on error
|
||||
*/
|
||||
public static long currentTime(String serverName) {
|
||||
long[] la = currentTimeAndStratum(serverName);
|
||||
long[] la = currentTimeAndStratum(serverName, DEFAULT_TIMEOUT);
|
||||
if (la != null)
|
||||
return la[0];
|
||||
return -1;
|
||||
@@ -116,7 +117,7 @@ class NtpClient {
|
||||
* @return time in rv[0] and stratum in rv[1], or null for error
|
||||
* @since 0.7.12
|
||||
*/
|
||||
private static long[] currentTimeAndStratum(String serverName) {
|
||||
private static long[] currentTimeAndStratum(String serverName, int timeout) {
|
||||
DatagramSocket socket = null;
|
||||
try {
|
||||
// Send request
|
||||
@@ -135,7 +136,7 @@ class NtpClient {
|
||||
|
||||
// Get response
|
||||
packet = new DatagramPacket(buf, buf.length);
|
||||
socket.setSoTimeout(10*1000);
|
||||
socket.setSoTimeout(timeout);
|
||||
socket.receive(packet);
|
||||
|
||||
// Immediately record the incoming timestamp
|
||||
|
@@ -43,6 +43,8 @@ public class RouterTimestamper extends Timestamper {
|
||||
/** how many times do we have to query if we are changing the clock? */
|
||||
private static final int DEFAULT_CONCURRING_SERVERS = 3;
|
||||
private static final int MAX_CONSECUTIVE_FAILS = 10;
|
||||
private static final int DEFAULT_TIMEOUT = 10*1000;
|
||||
private static final int SHORT_TIMEOUT = 5*1000;
|
||||
|
||||
public static final String PROP_QUERY_FREQUENCY = "time.queryFrequencyMs";
|
||||
public static final String PROP_SERVER_LIST = "time.sntpServerList";
|
||||
@@ -177,7 +179,7 @@ public class RouterTimestamper extends Timestamper {
|
||||
if (_log != null && _log.shouldDebug())
|
||||
_log.debug("Querying servers " + servers);
|
||||
try {
|
||||
lastFailed = !queryTime(servers.toArray(new String[servers.size()]));
|
||||
lastFailed = !queryTime(servers.toArray(new String[servers.size()]), SHORT_TIMEOUT);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
if (!lastFailed && _log != null && _log.shouldWarn())
|
||||
_log.warn("Unable to reach any regional NTP servers: " + servers);
|
||||
@@ -192,7 +194,7 @@ public class RouterTimestamper extends Timestamper {
|
||||
if (_log != null && _log.shouldDebug())
|
||||
_log.debug("Querying servers " + _servers);
|
||||
try {
|
||||
lastFailed = !queryTime(_servers.toArray(new String[_servers.size()]));
|
||||
lastFailed = !queryTime(_servers.toArray(new String[_servers.size()]), DEFAULT_TIMEOUT);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
lastFailed = true;
|
||||
}
|
||||
@@ -259,18 +261,18 @@ public class RouterTimestamper extends Timestamper {
|
||||
/**
|
||||
* True if the time was queried successfully, false if it couldn't be
|
||||
*/
|
||||
private boolean queryTime(String serverList[]) throws IllegalArgumentException {
|
||||
private boolean queryTime(String serverList[], int perServerTimeout) throws IllegalArgumentException {
|
||||
long found[] = new long[_concurringServers];
|
||||
long now = -1;
|
||||
int stratum = -1;
|
||||
long expectedDelta = 0;
|
||||
_wellSynced = false;
|
||||
for (int i = 0; i < _concurringServers; i++) {
|
||||
if (i > 0) {
|
||||
// this delays startup when net is disconnected or the timeserver list is bad, don't make it too long
|
||||
try { Thread.sleep(2*1000); } catch (InterruptedException ie) {}
|
||||
}
|
||||
long[] timeAndStratum = NtpClient.currentTimeAndStratum(serverList);
|
||||
//if (i > 0) {
|
||||
// // this delays startup when net is disconnected or the timeserver list is bad, don't make it too long
|
||||
// try { Thread.sleep(2*1000); } catch (InterruptedException ie) {}
|
||||
//}
|
||||
long[] timeAndStratum = NtpClient.currentTimeAndStratum(serverList, perServerTimeout);
|
||||
now = timeAndStratum[0];
|
||||
stratum = (int) timeAndStratum[1];
|
||||
long delta = now - _context.clock().now();
|
||||
|
Reference in New Issue
Block a user