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