diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/TooManyRequestExceptions.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/TooManyRequestExceptions.kt index a2908547d..17bea32f1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/TooManyRequestExceptions.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/TooManyRequestExceptions.kt @@ -6,4 +6,8 @@ import java.util.Date class TooManyRequestExceptions( val url: String, val retryAt: Date?, -) : IOException() +) : IOException() { + + val retryAfter: Long + get() = if (retryAt == null) 0 else (retryAt.time - System.currentTimeMillis()).coerceAtLeast(0) +} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt index 131abe9a5..5fb8d4d85 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt @@ -37,6 +37,7 @@ import okio.IOException import okio.buffer import okio.sink import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.core.exceptions.TooManyRequestExceptions import org.koitharu.kotatsu.core.network.CommonHeaders import org.koitharu.kotatsu.core.network.MangaHttpClient import org.koitharu.kotatsu.core.parser.MangaDataRepository @@ -277,7 +278,12 @@ class DownloadWorker @AssistedInject constructor( publishState(currentState.copy(isPaused = false, error = null)) } else { countDown-- - delay(DOWNLOAD_ERROR_DELAY) + val retryDelay = if (e is TooManyRequestExceptions) { + e.retryAfter + DOWNLOAD_ERROR_DELAY + } else { + DOWNLOAD_ERROR_DELAY + } + delay(retryDelay) } } }