diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/LiveKit.kt b/livekit-android-sdk/src/main/java/io/livekit/android/LiveKit.kt index 17396882..6885329e 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/LiveKit.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/LiveKit.kt @@ -26,6 +26,7 @@ import io.livekit.android.room.Room import io.livekit.android.room.RoomListener import io.livekit.android.util.LKLog import io.livekit.android.util.LoggingLevel +import io.livekit.android.util.executeSuspendWithRetry import timber.log.Timber class LiveKit { @@ -114,6 +115,7 @@ class LiveKit { * Connect to a LiveKit room * @param url URL to LiveKit server (i.e. ws://mylivekitdeploy.io) * @param listener Listener to Room events. LiveKit interactions take place with these callbacks + * @param maxConnectRetry Int to set max connect retry. */ @Deprecated("Use LiveKit.create and Room.connect instead. This is limited to max protocol 7.") suspend fun connect( @@ -124,6 +126,7 @@ class LiveKit { roomOptions: RoomOptions = RoomOptions(), listener: RoomListener? = null, overrides: LiveKitOverrides = LiveKitOverrides(), + maxConnectRetry: Int = 0 ): Room { val room = create(appContext, roomOptions, overrides) @@ -132,7 +135,10 @@ class LiveKit { val protocolVersion = maxOf(options.protocolVersion, ProtocolVersion.v7) val connectOptions = options.copy(protocolVersion = protocolVersion) - room.connect(url, token, connectOptions) + executeSuspendWithRetry(maxConnectRetry){ + room.connect(url, token, connectOptions) + } + return room } } diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/util/ExcuteSuspendWithRetry.kt b/livekit-android-sdk/src/main/java/io/livekit/android/util/ExcuteSuspendWithRetry.kt new file mode 100644 index 00000000..c8fe6781 --- /dev/null +++ b/livekit-android-sdk/src/main/java/io/livekit/android/util/ExcuteSuspendWithRetry.kt @@ -0,0 +1,24 @@ +package io.livekit.android.util + +import kotlinx.coroutines.delay + +suspend fun executeSuspendWithRetry(maxRetries: Int, suspendFunction: suspend () -> T): T { + var currentAttempt = 0 + var lastException: Exception? = null + + while (currentAttempt <= maxRetries) { + try { + return suspendFunction() + } catch (e: Exception) { + LKLog.i {"connection number $currentAttempt failed with $e"} + // Store the last exception for error logging + lastException = e + currentAttempt++ + + // Delay before retrying + delay(1_000L * currentAttempt) + } + } + + throw lastException ?: RuntimeException("Max retries exceeded") +}