Skip to content

Commit e40f795

Browse files
author
Noah Andrews
committed
Use nanoTime for Lost Connection Detection
1 parent 065e93e commit e40f795

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

src/main/java/org/java_websocket/AbstractWebSocket.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
7474
private ScheduledFuture connectionLostCheckerFuture;
7575

7676
/**
77-
* Attribute for the lost connection check interval
77+
* Attribute for the lost connection check interval in nanoseconds
7878
* @since 1.3.4
7979
*/
80-
private int connectionLostTimeout = 60;
80+
private long connectionLostTimeout = TimeUnit.SECONDS.toNanos(60);
8181

8282
/**
8383
* Attribute to keep track if the WebSocket Server/Client is running/connected
@@ -92,12 +92,12 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
9292
/**
9393
* Get the interval checking for lost connections
9494
* Default is 60 seconds
95-
* @return the interval
95+
* @return the interval in seconds
9696
* @since 1.3.4
9797
*/
9898
public int getConnectionLostTimeout() {
9999
synchronized (syncConnectionLost) {
100-
return connectionLostTimeout;
100+
return (int) TimeUnit.NANOSECONDS.toSeconds(connectionLostTimeout);
101101
}
102102
}
103103

@@ -110,7 +110,7 @@ public int getConnectionLostTimeout() {
110110
*/
111111
public void setConnectionLostTimeout( int connectionLostTimeout ) {
112112
synchronized (syncConnectionLost) {
113-
this.connectionLostTimeout = connectionLostTimeout;
113+
this.connectionLostTimeout = TimeUnit.SECONDS.toNanos(connectionLostTimeout);
114114
if (this.connectionLostTimeout <= 0) {
115115
log.trace("Connection lost timer stopped");
116116
cancelConnectionLostTimer();
@@ -183,9 +183,9 @@ public void run() {
183183
connections.clear();
184184
try {
185185
connections.addAll( getConnections() );
186-
long current = ( System.currentTimeMillis() - ( connectionLostTimeout * 1500 ) );
186+
long minimumPongTime = (long) (System.nanoTime() - ( connectionLostTimeout * 1.5 ));
187187
for( WebSocket conn : connections ) {
188-
executeConnectionLostDetection(conn, current);
188+
executeConnectionLostDetection(conn, minimumPongTime);
189189
}
190190
} catch ( Exception e ) {
191191
//Ignore this exception
@@ -194,20 +194,20 @@ public void run() {
194194
}
195195
};
196196

197-
connectionLostCheckerFuture = connectionLostCheckerService.scheduleAtFixedRate(connectionLostChecker, connectionLostTimeout, connectionLostTimeout, TimeUnit.SECONDS);
197+
connectionLostCheckerFuture = connectionLostCheckerService.scheduleAtFixedRate(connectionLostChecker, connectionLostTimeout, connectionLostTimeout, TimeUnit.NANOSECONDS);
198198
}
199199

200200
/**
201201
* Send a ping to the endpoint or close the connection since the other endpoint did not respond with a ping
202202
* @param webSocket the websocket instance
203-
* @param current the current time in milliseconds
203+
* @param minimumPongTime the lowest/oldest allowable last pong time (in nanoTime) before we consider the connection to be lost
204204
*/
205-
private void executeConnectionLostDetection(WebSocket webSocket, long current) {
205+
private void executeConnectionLostDetection(WebSocket webSocket, long minimumPongTime) {
206206
if (!(webSocket instanceof WebSocketImpl)) {
207207
return;
208208
}
209209
WebSocketImpl webSocketImpl = (WebSocketImpl) webSocket;
210-
if( webSocketImpl.getLastPong() < current ) {
210+
if( webSocketImpl.getLastPong() < minimumPongTime ) {
211211
log.trace("Closing connection due to no pong received: {}", webSocketImpl);
212212
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" );
213213
} else {

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public class WebSocketImpl implements WebSocket {
151151
/**
152152
* Attribute, when the last pong was recieved
153153
*/
154-
private long lastPong = System.currentTimeMillis();
154+
private long lastPong = System.nanoTime();
155155

156156
/**
157157
* Attribut to synchronize the write
@@ -802,7 +802,7 @@ long getLastPong() {
802802
* Update the timestamp when the last pong was received
803803
*/
804804
public void updateLastPong() {
805-
this.lastPong = System.currentTimeMillis();
805+
this.lastPong = System.nanoTime();
806806
}
807807

808808
/**

0 commit comments

Comments
 (0)