Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow set connect retry to connect function #350

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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)

Expand All @@ -132,7 +135,10 @@ class LiveKit {
val protocolVersion = maxOf(options.protocolVersion, ProtocolVersion.v7)
val connectOptions = options.copy(protocolVersion = protocolVersion)

room.connect(url, token, connectOptions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be added to the room.connect function itself rather than this one. LiveKit.connect is deprecated and will be removed in the future.

executeSuspendWithRetry(maxConnectRetry){
room.connect(url, token, connectOptions)
}

return room
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.livekit.android.util

import kotlinx.coroutines.delay

suspend fun <T>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")
}