Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a periodic check that triggers a disconnect if a half-open connection is detected. #160

Merged
merged 7 commits into from Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 49 additions & 2 deletions jicoco/src/main/java/org/jitsi/xmpp/mucclient/MucClient.java
Expand Up @@ -48,11 +48,13 @@
*/
public class MucClient
{
private static final int DEFAULT_PING_INTERVAL_SECONDS = 30;

static
{
XMPPTCPConnection.setUseStreamManagementDefault(false);
XMPPTCPConnection.setUseStreamManagementResumptionDefault(false);
PingManager.setDefaultPingInterval(30);
PingManager.setDefaultPingInterval(DEFAULT_PING_INTERVAL_SECONDS);
}

/**
Expand Down Expand Up @@ -795,7 +797,7 @@ private void removePresenceExtension(String elementName, String namespace)
{
return;
}

if (lastPresenceSent.removeExtension(elementName, namespace) != null)
{
updatedPresence = lastPresenceSent.build();
Expand Down Expand Up @@ -853,4 +855,49 @@ public void pingFailed()
}
}
}

private final PeriodicRunnable halfOpenConnectionPeriodicCheck = new HalfOpenConnectionPeriodicCheck();

/**
* @return the {@link PeriodicRunnable} that checks the XMPP connection state and triggers a disconnect if a
* half-open connection is detected.
*/
public PeriodicRunnable getHalfOpenConnectionPeriodicCheck()
{
return halfOpenConnectionPeriodicCheck;
}

/**
* Periodically checks the connection state and triggers a disconnect if a half-open connection is detected.
*/
class HalfOpenConnectionPeriodicCheck
extends PeriodicRunnable
{
public HalfOpenConnectionPeriodicCheck() {
super(2*1000*DEFAULT_PING_INTERVAL_SECONDS);
}

/**
* Triggers a disconnect if a half-open connection is detected.
*/
@Override
public void run() {
super.run();

AbstractXMPPConnection con = xmppConnection;
if (con != null && con.isConnected() && con.isAuthenticated())
{
long lastStanzaReceivedMs = con.getLastStanzaReceived();
if (lastStanzaReceivedMs > 0)
{
long nowMs = System.currentTimeMillis();
if (nowMs - lastStanzaReceivedMs > 2 * 1000 * DEFAULT_PING_INTERVAL_SECONDS)
{
logger.warn("Half-open XMPP connection detected, will trigger a disconnect.");
con.disconnect();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you trigger the disconnect, it reconnects again, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct 👍

}
}
}
}
}
}
Expand Up @@ -17,6 +17,7 @@
package org.jitsi.xmpp.mucclient;

import org.jitsi.service.configuration.*;
import org.jitsi.utils.concurrent.*;
import org.jitsi.utils.logging2.*;
import org.jitsi.utils.logging2.Logger;
import org.jivesoftware.smack.*;
Expand Down Expand Up @@ -79,6 +80,11 @@ public class MucClientManager
*/
private final Object syncRoot = new Object();

/**
* The {@link RecurringRunnableExecutor} to be utilized by the {@link MucClientManager} class and its instances.
*/
private final RecurringRunnableExecutor recurringRunnableExecutor = new RecurringRunnableExecutor();

/**
* Initializes a new {@link MucClientManager} instance.
*
Expand Down Expand Up @@ -126,6 +132,7 @@ public boolean addMucClient(MucClientConfiguration config)

mucClient = new MucClient(config, MucClientManager.this);
mucClients.put(config.getId(), mucClient);
recurringRunnableExecutor.registerRecurringRunnable(mucClient.getHalfOpenConnectionPeriodicCheck());
}

mucClient.start();
Expand Down Expand Up @@ -325,7 +332,8 @@ public boolean removeMucClient(String id)
logger.info("Can not find MucClient to remove.");
return false;
}
mucClient.stop();
recurringRunnableExecutor.deRegisterRecurringRunnable(mucClient.getHalfOpenConnectionPeriodicCheck());
mucClient.stop();
return true;
}

Expand Down