-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow set connect retry to connect function
- Loading branch information
1 parent
7a45b21
commit cf49991
Showing
2 changed files
with
31 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
livekit-android-sdk/src/main/java/io/livekit/android/util/ExcuteSuspendWithRetry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |