From 939b6b1e46e793a9143128e2b9562e05641c8bb5 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 22 Aug 2024 09:12:34 +0300 Subject: [PATCH] Improve TooManyRequestExceptions --- .../exception/TooManyRequestExceptions.kt | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/TooManyRequestExceptions.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/TooManyRequestExceptions.kt index 6ec98577..1e576928 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/TooManyRequestExceptions.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/TooManyRequestExceptions.kt @@ -2,25 +2,30 @@ package org.koitharu.kotatsu.parsers.exception import okio.IOException import java.time.Instant +import java.time.temporal.ChronoUnit class TooManyRequestExceptions( val url: String, - val retryAfter: Long, -) : IOException( - buildString { - append("Too man requests") - if (retryAfter > 0) { - append(", retry after ") - append(retryAfter) - append("ms") + retryAfter: Long, +) : IOException("Too man requests") { + + val retryAt: Instant? = if (retryAfter > 0 && retryAfter < Long.MAX_VALUE) { + Instant.now().plusMillis(retryAfter) + } else { + null + } + + fun getRetryDelay(): Long { + if (retryAt == null) { + return -1L } - }, -) { + return Instant.now().until(retryAt, ChronoUnit.MILLIS).coerceAtLeast(0L) + } - val retryAt: Instant? - get() = if (retryAfter > 0 && retryAfter < Long.MAX_VALUE) { - Instant.now().plusMillis(retryAfter) + override val message: String? + get() = if (retryAt != null) { + "${super.message}, retry at $retryAt" } else { - null + super.message } }