-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retry logic to SocketInstance (#373)
- Loading branch information
1 parent
f4d3d3d
commit 44818c0
Showing
5 changed files
with
101 additions
and
14 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
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
8 changes: 8 additions & 0 deletions
8
jellyfin-api/src/commonMain/kotlin/org/jellyfin/sdk/api/sockets/SocketInstanceConnection.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
62 changes: 57 additions & 5 deletions
62
jellyfin-api/src/commonMain/kotlin/org/jellyfin/sdk/api/sockets/helper/ReconnectHelper.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 |
---|---|---|
@@ -1,9 +1,61 @@ | ||
package org.jellyfin.sdk.api.sockets.helper | ||
|
||
internal class ReconnectHelper { | ||
fun reportConnect() {} | ||
fun reportDisconnect(){} | ||
fun reset() {} | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import mu.KotlinLogging | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
fun shouldReconnectNow() {} | ||
private val logger = KotlinLogging.logger {} | ||
|
||
internal class ReconnectHelper( | ||
private val coroutineScope: CoroutineScope, | ||
private val reconnect: suspend () -> Unit, | ||
) { | ||
private var reconnectJob: Job? = null | ||
private var attempts = 0 | ||
|
||
fun reset() { | ||
logger.debug { "Resetting" } | ||
reconnectJob?.cancel() | ||
attempts = 0 | ||
} | ||
|
||
fun notifyReconnect() { | ||
reconnectJob?.cancel() | ||
attempts++ | ||
logger.debug { "Notified about reconnect, attempts=${attempts}" } | ||
} | ||
|
||
fun notifyConnected() { | ||
attempts = 0 | ||
logger.debug { "Notified about connect, attempts reset" } | ||
} | ||
|
||
fun scheduleReconnect(error: Boolean = false): Boolean { | ||
reconnectJob?.cancel() | ||
|
||
if (attempts > RETRY_ATTEMPTS) { | ||
logger.debug { "Reconnect schedule failed: exceeded maximum retry attempts" } | ||
return false | ||
} | ||
|
||
reconnectJob = coroutineScope.launch { | ||
val retryAfter = if (error) RETRY_ERROR else RETRY_NORMAL | ||
|
||
logger.info { "Reconnect scheduled in $retryAfter (error=$error)" } | ||
|
||
delay(retryAfter) | ||
reconnect() | ||
} | ||
|
||
return true | ||
} | ||
|
||
companion object { | ||
val RETRY_NORMAL = 5.seconds | ||
val RETRY_ERROR = 30.seconds | ||
const val RETRY_ATTEMPTS = 5 | ||
} | ||
} |
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