-
Notifications
You must be signed in to change notification settings - Fork 23
Combining timeout and retry of network request
Devrath edited this page Jun 15, 2021
·
1 revision
- Composes higher-level functions retry() and with timeout().
- Demonstrates how simple and readable code written with Coroutines can be.
- The mock API first responds after the timeout and then returns an unsuccessful response. The third attempt is then successful.
class TimeoutAndRetryViewModel(
private val api: MockApi = mockApi()
) : BaseViewModel<UiState>() {
fun performNetworkRequest() {
uiState.value = UiState.Loading
val numberOfRetries = 2
val timeout = 1000L
val oreoVersionsDeferred = viewModelScope.async {
retryWithTimeout(numberOfRetries, timeout) {
api.getAndroidVersionFeatures(27)
}
}
val pieVersionsDeferred = viewModelScope.async {
retryWithTimeout(numberOfRetries, timeout) {
api.getAndroidVersionFeatures(28)
}
}
viewModelScope.launch {
try {
val versionFeatures = listOf(
oreoVersionsDeferred,
pieVersionsDeferred
).awaitAll()
uiState.value = UiState.Success(versionFeatures)
} catch (e: Exception) {
Timber.e(e)
uiState.value = UiState.Error("Network Request failed")
}
}
}
private suspend fun <T> retryWithTimeout(
numberOfRetries: Int,
timeout: Long,
block: suspend () -> T
) = retry(numberOfRetries) {
withTimeout(timeout) {
block()
}
}
private suspend fun <T> retry(
numberOfRetries: Int,
delayBetweenRetries: Long = 100,
block: suspend () -> T
): T {
repeat(numberOfRetries) {
try {
return block()
} catch (exception: Exception) {
Timber.e(exception)
}
delay(delayBetweenRetries)
}
return block() // last attempt
}
}