mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Reconnect websocket with exponential backof if connection is lost
This commit is contained in:
parent
5648714641
commit
4a3b0e5124
1 changed files with 21 additions and 4 deletions
|
@ -887,6 +887,8 @@ public class ManagerImpl implements Manager {
|
|||
signalWebSocket.connect();
|
||||
|
||||
hasCaughtUpWithOldMessages = false;
|
||||
var backOffCounter = 0;
|
||||
final var MAX_BACKOFF_COUNTER = 9;
|
||||
|
||||
while (!Thread.interrupted()) {
|
||||
SignalServiceEnvelope envelope;
|
||||
|
@ -901,6 +903,8 @@ public class ManagerImpl implements Manager {
|
|||
// store message on disk, before acknowledging receipt to the server
|
||||
cachedMessage[0] = account.getMessageCache().cacheMessage(envelope1, recipientId);
|
||||
});
|
||||
backOffCounter = 0;
|
||||
|
||||
if (result.isPresent()) {
|
||||
envelope = result.get();
|
||||
logger.debug("New message received from server");
|
||||
|
@ -924,11 +928,24 @@ public class ManagerImpl implements Manager {
|
|||
} else {
|
||||
throw e;
|
||||
}
|
||||
} catch (WebSocketUnavailableException e) {
|
||||
logger.debug("Pipe unexpectedly unavailable, connecting");
|
||||
signalWebSocket.connect();
|
||||
continue;
|
||||
} catch (IOException e) {
|
||||
logger.debug("Pipe unexpectedly unavailable: {}", e.getMessage());
|
||||
if (e instanceof WebSocketUnavailableException || "Connection closed!".equals(e.getMessage())) {
|
||||
final var sleepMilliseconds = 100 * (long) Math.pow(2, backOffCounter);
|
||||
backOffCounter = Math.min(backOffCounter + 1, MAX_BACKOFF_COUNTER);
|
||||
logger.warn("Connection closed unexpectedly, reconnecting in {} ms", sleepMilliseconds);
|
||||
try {
|
||||
Thread.sleep(sleepMilliseconds);
|
||||
} catch (InterruptedException interruptedException) {
|
||||
return;
|
||||
}
|
||||
hasCaughtUpWithOldMessages = false;
|
||||
signalWebSocket.connect();
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
} catch (TimeoutException e) {
|
||||
backOffCounter = 0;
|
||||
if (returnOnTimeout) return;
|
||||
continue;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue