Skip to content

Commit

Permalink
OF-2809: Guard against negative max idle time for websockets (#2434)
Browse files Browse the repository at this point in the history
* OF-2809: Guard against negative max idle time for websockets

When the max idle time is configured to be negative, then do not schedule the check to see if the connection is still active.

* OF-2809: Tweak task implementation to make null-references less likely

... and rename class to follow convention of starting with a capital letter.
  • Loading branch information
guusdk committed Feb 29, 2024
1 parent 914f1f4 commit abd2f93
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015 Tom Evans, 2023 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2015 Tom Evans, 2023-2024 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -111,8 +111,13 @@ public void onConnect(Session session)
final Duration taskInterval = KEEP_ALIVE_FRAME_PING_INTERVAL_PROPERTY.getValue().dividedBy(10);
TaskEngine.getInstance().schedule(websocketFramePingTask, taskInterval, taskInterval);
}
xmppSessionIdleTask = new xmppSessionIdleTask();
TaskEngine.getInstance().schedule(xmppSessionIdleTask, getMaxIdleTime().dividedBy(10), getMaxIdleTime().dividedBy(10));

final Duration maxIdleTime = getMaxIdleTime();
xmppSessionIdleTask = new XmppSessionIdleTask();
if (!maxIdleTime.isNegative() && !maxIdleTime.isZero()) {
TaskEngine.getInstance().schedule(xmppSessionIdleTask, maxIdleTime.dividedBy(10), maxIdleTime.dividedBy(10));
}

wsConnection.setStanzaHandler(new WebSocketClientStanzaHandler(XMPPServer.getInstance().getPacketRouter(), wsConnection));
}

Expand Down Expand Up @@ -275,13 +280,13 @@ public void run() {
/**
* Task that, on prolonged inactivity, sends an XMPP ping, to ensure that the remote entity is still responsive.
*/
private final class xmppSessionIdleTask extends TimerTask {
private final class XmppSessionIdleTask extends TimerTask {
private Instant pendingPingSentAt = null;

@Override
public void run()
{
if (!isWebSocketOpen()) {
if (!isWebSocketOpen() || getMaxIdleTime().isNegative() || getMaxIdleTime().isZero()) {
TaskEngine.getInstance().cancelScheduledTask(websocketFramePingTask);
TaskEngine.getInstance().cancelScheduledTask(xmppSessionIdleTask);
return;
Expand Down

0 comments on commit abd2f93

Please sign in to comment.