Skip to content

Commit

Permalink
[AND-165] Fix reconnect issue when network conditions change (#1246)
Browse files Browse the repository at this point in the history
* Change log tags

* Replace fastReconnect with rejoin (temp)

* Edit logs

---------

Co-authored-by: Aleksandar Apostolov <apostolov.alexandar@gmail.com>
  • Loading branch information
liviu-timar and aleksandar-apostolov authored Dec 6, 2024
1 parent 4ef5c01 commit 1427526
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,16 @@ public class Call(
private val listener = object : NetworkStateProvider.NetworkStateListener {
override suspend fun onConnected() {
leaveTimeoutAfterDisconnect?.cancel()
logger.d { "[onConnected] no args" }
logger.d { "[NetworkStateListener#onConnected] #network; no args" }
val elapsedTimeMils = System.currentTimeMillis() - lastDisconnect
if (lastDisconnect > 0 && elapsedTimeMils < reconnectDeadlineMils) {
logger.d {
"[onConnected] Reconnecting (fast) time since last disconnect is ${elapsedTimeMils / 1000} seconds. Deadline is ${reconnectDeadlineMils / 1000} seconds"
"[NetworkStateListener#onConnected] #network; Reconnecting (fast). Time since last disconnect is ${elapsedTimeMils / 1000} seconds. Deadline is ${reconnectDeadlineMils / 1000} seconds"
}
fastReconnect()
rejoin()
} else {
logger.d {
"[onConnected] Reconnecting (full) time since last disconnect is ${elapsedTimeMils / 1000} seconds. Deadline is ${reconnectDeadlineMils / 1000} seconds"
"[NetworkStateListener#onConnected] #network; Reconnecting (full). Time since last disconnect is ${elapsedTimeMils / 1000} seconds. Deadline is ${reconnectDeadlineMils / 1000} seconds"
}
rejoin()
}
Expand All @@ -241,11 +241,11 @@ public class Call(
leaveTimeoutAfterDisconnect = scope.launch {
delay(clientImpl.leaveAfterDisconnectSeconds * 1000)
logger.d {
"[onDisconnected] Leaving after being disconnected for ${clientImpl.leaveAfterDisconnectSeconds}"
"[NetworkStateListener#onDisconnected] #network; Leaving after being disconnected for ${clientImpl.leaveAfterDisconnectSeconds}"
}
leave()
}
logger.d { "[onDisconnected] at $lastDisconnect" }
logger.d { "[NetworkStateListener#onDisconnected] #network; at $lastDisconnect" }
}
}

Expand Down Expand Up @@ -531,7 +531,7 @@ public class Call(
session?.subscriber?.state?.collect {
when (it) {
PeerConnection.PeerConnectionState.FAILED, PeerConnection.PeerConnectionState.DISCONNECTED -> {
fastReconnect()
rejoin()
}

else -> {
Expand All @@ -546,7 +546,7 @@ public class Call(
session?.subscriber?.state?.collect {
when (it) {
PeerConnection.PeerConnectionState.FAILED, PeerConnection.PeerConnectionState.DISCONNECTED -> {
fastReconnect()
rejoin()
}

else -> {
Expand Down Expand Up @@ -1307,7 +1307,7 @@ public class Call(

fun fastReconnect() {
call.scope.launch {
call.fastReconnect()
call.rejoin()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public class CallHealthMonitor(
logger.d { "[reconnect] skipping reconnect - too often" }
} else {
lastReconnectAt = now
call.fastReconnect()
call.rejoin()
}

reconnectInProgress = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public class RtcSession internal constructor(
logger.d { "[RtcSession#error] reconnectStrategy: $reconnectStrategy" }
when (reconnectStrategy) {
WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_FAST -> {
call.fastReconnect()
call.rejoin()
}

WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_REJOIN -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ public class NetworkStateProvider(
private val lock: Any = Any()
private val callback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
logger.d { "[callback#onAvailable] #network; onAvailable." }
notifyListenersIfNetworkStateChanged()
}

override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
logger.d { "[callback#onCapabilitiesChanged] #network; onCapabilitiesChanged" }
notifyListenersIfNetworkStateChanged()
}

override fun onLost(network: Network) {
logger.d { "[callback#onLost] #network; onLost" }
isConnected = false
notifyListenersIfNetworkStateChanged()
}
}
Expand All @@ -65,11 +69,11 @@ public class NetworkStateProvider(
private fun notifyListenersIfNetworkStateChanged() {
val isNowConnected = isConnected()
if (!isConnected && isNowConnected) {
logger.i { "Network connected." }
logger.d { "[notifyListenersIfNetworkStateChanged] #network; Network connected." }
isConnected = true
listeners.onConnected()
} else if (isConnected && !isNowConnected) {
logger.i { "Network disconnected." }
logger.d { "[notifyListenersIfNetworkStateChanged] #network; Network disconnected." }
isConnected = false
listeners.onDisconnected()
}
Expand Down

0 comments on commit 1427526

Please sign in to comment.