Skip to content

Commit

Permalink
Close unidentified websocket when the app is in background
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Dec 29, 2023
1 parent e17b07b commit ce18927
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
Expand Up @@ -172,6 +172,20 @@ class IncomingMessageObserver(private val context: Application) {
}
}

private fun mayDisconnectUnidentified() {
val timeIdle: Long
val appVisibleSnapshot: Boolean

lock.withLock {
appVisibleSnapshot = appVisible
timeIdle = if (appVisibleSnapshot) 0 else System.currentTimeMillis() - lastInteractionTime
}
if (!appVisibleSnapshot && timeIdle < maxBackgroundTime) {
Log.d(TAG, "Disconnecting unidentified websocket")
ApplicationDependencies.getSignalWebSocket().disconnectUnidentified()
}
}

private fun isConnectionNecessary(): Boolean {
val timeIdle: Long
val keepAliveEntries: Set<Pair<String, Long>>
Expand Down Expand Up @@ -384,6 +398,7 @@ class IncomingMessageObserver(private val context: Application) {
while (isConnectionNecessary()) {
try {
Log.d(TAG, "Reading message...")
mayDisconnectUnidentified()

val hasMore = signalWebSocket.readMessageBatch(websocketReadTimeout, 30) { batch ->
Log.i(TAG, "Retrieved ${batch.size} envelopes!")
Expand Down
Expand Up @@ -134,6 +134,13 @@ public void onMessageError(int status, boolean isIdentifiedWebSocket) {
});
}

@Override
public void needsKeepAlive(boolean need, boolean isIdentifiedWebSocket) {
if (!isIdentifiedWebSocket) {
unidentified.needsKeepAlive = need;
}
}

private boolean isKeepAliveNecessary() {
return identified.needsKeepAlive || unidentified.needsKeepAlive;
}
Expand Down Expand Up @@ -172,7 +179,8 @@ public void run() {
sleepUntil(responseRequiredTime);

if (shouldKeepRunning && isKeepAliveNecessary()) {
if (identified.lastKeepAliveReceived < keepAliveSendTime || unidentified.lastKeepAliveReceived < keepAliveSendTime) {
if ((identified.needsKeepAlive && identified.lastKeepAliveReceived < keepAliveSendTime) ||
(unidentified.needsKeepAlive && unidentified.lastKeepAliveReceived < keepAliveSendTime)) {
Log.w(TAG, "Missed keep alives, identified last: " + identified.lastKeepAliveReceived +
" unidentified last: " + unidentified.lastKeepAliveReceived +
" needed by: " + responseRequiredTime);
Expand Down
Expand Up @@ -122,7 +122,7 @@ private void disconnectIdentified() {
}
}

private void disconnectUnidentified() {
public void disconnectUnidentified() {
if (unidentifiedWebSocket != null) {
unidentifiedWebSocketStateDisposable.dispose();

Expand Down Expand Up @@ -183,7 +183,9 @@ public synchronized void sendKeepAlive() throws IOException {
if (canConnect) {
try {
getWebSocket().sendKeepAlive();
getUnidentifiedWebSocket().sendKeepAlive();
if (unidentifiedWebSocket != null && !unidentifiedWebSocket.isDead()) {
getUnidentifiedWebSocket().sendKeepAlive();
}
} catch (WebSocketUnavailableException e) {
throw new AssertionError(e);
}
Expand Down
Expand Up @@ -7,4 +7,6 @@ public interface HealthMonitor {
void onKeepAliveResponse(long sentTimestamp, boolean isIdentifiedWebSocket);

void onMessageError(int status, boolean isIdentifiedWebSocket);

void needsKeepAlive(boolean need, boolean isIdentifiedWebSocket);
}
Expand Up @@ -175,6 +175,7 @@ public synchronized Observable<WebSocketConnectionState> connect() {
}

webSocketState.onNext(WebSocketConnectionState.CONNECTING);
healthMonitor.needsKeepAlive(true, credentialsProvider.isPresent());

this.client = okHttpClient.newWebSocket(requestBuilder.build(), this);
}
Expand All @@ -192,6 +193,7 @@ public synchronized void disconnect() {
client.close(1000, "OK");
client = null;
webSocketState.onNext(WebSocketConnectionState.DISCONNECTING);
healthMonitor.needsKeepAlive(false, credentialsProvider.isPresent());
}

notifyAll();
Expand Down

0 comments on commit ce18927

Please sign in to comment.