Always try to retry Http requests in case of 429 (#1300)

This commit is contained in:
Benoit Marty 2021-04-08 10:59:51 +02:00
parent 9b5bc60fa9
commit 8dead986a5
2 changed files with 12 additions and 3 deletions

View File

@ -18,6 +18,7 @@ Improvements 🙌:
- Room list improvements (paging)
- Fix quick click action (#3127)
- Get Event after a Push for a faster notification display in some conditions
- Always try to retry Http requests in case of 429 (#1300)
Bugfix 🐛:
- Fix bad theme change for the MainActivity

View File

@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.network
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.delay
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.failure.MatrixError
import org.matrix.android.sdk.api.failure.getRetryDelay
import org.matrix.android.sdk.api.failure.shouldBeRetried
import org.matrix.android.sdk.internal.network.ssl.CertUtil
@ -71,9 +72,16 @@ internal suspend inline fun <DATA> executeRequest(globalErrorReceiver: GlobalErr
// }
?.also { unrecognizedCertificateException -> throw unrecognizedCertificateException }
if (canRetry && currentRetryCount++ < maxRetriesCount && exception.shouldBeRetried()) {
// In case of 429, ensure we wait enough
delay(currentDelay.coerceAtLeast(exception.getRetryDelay(0)))
currentRetryCount++
if (exception is Failure.ServerError
&& exception.httpCode == 429
&& exception.error.code == MatrixError.M_LIMIT_EXCEEDED
&& currentRetryCount < maxRetriesCount) {
// 429, we can retry
delay(exception.getRetryDelay(1_000))
} else if (canRetry && currentRetryCount < maxRetriesCount && exception.shouldBeRetried()) {
delay(currentDelay)
currentDelay = currentDelay.times(2L).coerceAtMost(maxDelayBeforeRetry)
// Try again (loop)
} else {