Skip to content

Commit

Permalink
[ecovacs] Shorten XMPP ping intervals after failure
Browse files Browse the repository at this point in the history
If a ping failed, subsequent failures are very likely, so a shorter
interval will shorten the time until reconnection while still providing
robustness against single failures.
  • Loading branch information
maniac103 committed Feb 8, 2022
1 parent a53c34d commit 05e2e68
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ public void disconnect() {

private class PingHandler {
private static final long INTERVAL = 30; // seconds
// After a failure, use shorter intervals since subsequent further failure is likely
private static final long POST_FAILURE_INTERVAL = 5; // seconds
private static final int MAX_FAILURES = 4;

private final PingManager pingManager;
Expand Down Expand Up @@ -278,7 +280,7 @@ public void stop() {

private void sendPing() {
long timeSinceLastStanza = (System.currentTimeMillis() - connection.getLastStanzaReceived()) / 1000;
if (timeSinceLastStanza < INTERVAL) {
if (timeSinceLastStanza < currentPingInterval()) {
scheduleNextPing(timeSinceLastStanza);
return;
}
Expand Down Expand Up @@ -306,9 +308,13 @@ private synchronized void scheduleNextPing(long delta) {
oldFuture.cancel(true);
}
if (started) {
this.nextPing = scheduler.schedule(this::sendPing, INTERVAL - delta, TimeUnit.SECONDS);
this.nextPing = scheduler.schedule(this::sendPing, currentPingInterval() - delta, TimeUnit.SECONDS);
}
}

private long currentPingInterval() {
return failedPings > 0 ? POST_FAILURE_INTERVAL : INTERVAL;
}
}

private class IncomingMessageHandler extends AbstractIqRequestHandler {
Expand Down

0 comments on commit 05e2e68

Please sign in to comment.