Skip to content

Commit

Permalink
Use nanoTime for Lost Connection Detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Andrews committed Apr 10, 2019
1 parent 065e93e commit e40f795
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions src/main/java/org/java_websocket/AbstractWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
private ScheduledFuture connectionLostCheckerFuture;

/**
* Attribute for the lost connection check interval
* Attribute for the lost connection check interval in nanoseconds
* @since 1.3.4
*/
private int connectionLostTimeout = 60;
private long connectionLostTimeout = TimeUnit.SECONDS.toNanos(60);

/**
* Attribute to keep track if the WebSocket Server/Client is running/connected
Expand All @@ -92,12 +92,12 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
/**
* Get the interval checking for lost connections
* Default is 60 seconds
* @return the interval
* @return the interval in seconds
* @since 1.3.4
*/
public int getConnectionLostTimeout() {
synchronized (syncConnectionLost) {
return connectionLostTimeout;
return (int) TimeUnit.NANOSECONDS.toSeconds(connectionLostTimeout);
}
}

Expand All @@ -110,7 +110,7 @@ public int getConnectionLostTimeout() {
*/
public void setConnectionLostTimeout( int connectionLostTimeout ) {
synchronized (syncConnectionLost) {
this.connectionLostTimeout = connectionLostTimeout;
this.connectionLostTimeout = TimeUnit.SECONDS.toNanos(connectionLostTimeout);
if (this.connectionLostTimeout <= 0) {
log.trace("Connection lost timer stopped");
cancelConnectionLostTimer();
Expand Down Expand Up @@ -183,9 +183,9 @@ public void run() {
connections.clear();
try {
connections.addAll( getConnections() );
long current = ( System.currentTimeMillis() - ( connectionLostTimeout * 1500 ) );
long minimumPongTime = (long) (System.nanoTime() - ( connectionLostTimeout * 1.5 ));
for( WebSocket conn : connections ) {
executeConnectionLostDetection(conn, current);
executeConnectionLostDetection(conn, minimumPongTime);
}
} catch ( Exception e ) {
//Ignore this exception
Expand All @@ -194,20 +194,20 @@ public void run() {
}
};

connectionLostCheckerFuture = connectionLostCheckerService.scheduleAtFixedRate(connectionLostChecker, connectionLostTimeout, connectionLostTimeout, TimeUnit.SECONDS);
connectionLostCheckerFuture = connectionLostCheckerService.scheduleAtFixedRate(connectionLostChecker, connectionLostTimeout, connectionLostTimeout, TimeUnit.NANOSECONDS);
}

/**
* Send a ping to the endpoint or close the connection since the other endpoint did not respond with a ping
* @param webSocket the websocket instance
* @param current the current time in milliseconds
* @param minimumPongTime the lowest/oldest allowable last pong time (in nanoTime) before we consider the connection to be lost
*/
private void executeConnectionLostDetection(WebSocket webSocket, long current) {
private void executeConnectionLostDetection(WebSocket webSocket, long minimumPongTime) {
if (!(webSocket instanceof WebSocketImpl)) {
return;
}
WebSocketImpl webSocketImpl = (WebSocketImpl) webSocket;
if( webSocketImpl.getLastPong() < current ) {
if( webSocketImpl.getLastPong() < minimumPongTime ) {
log.trace("Closing connection due to no pong received: {}", webSocketImpl);
webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE, "The connection was closed because the other endpoint did not respond with a pong in time. For more information check: https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection" );
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public class WebSocketImpl implements WebSocket {
/**
* Attribute, when the last pong was recieved
*/
private long lastPong = System.currentTimeMillis();
private long lastPong = System.nanoTime();

/**
* Attribut to synchronize the write
Expand Down Expand Up @@ -802,7 +802,7 @@ long getLastPong() {
* Update the timestamp when the last pong was received
*/
public void updateLastPong() {
this.lastPong = System.currentTimeMillis();
this.lastPong = System.nanoTime();
}

/**
Expand Down

0 comments on commit e40f795

Please sign in to comment.