From c3c87863b2888da15a0e0e6dc4dd6f8c0865c7d3 Mon Sep 17 00:00:00 2001 From: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com> Date: Fri, 27 Dec 2024 11:03:33 +0500 Subject: [PATCH] Hitomi: retry on stream reset error like the website (#6802) --- src/all/hitomi/build.gradle | 2 +- .../tachiyomi/extension/all/hitomi/Hitomi.kt | 24 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/all/hitomi/build.gradle b/src/all/hitomi/build.gradle index 54152f37b..aeb76e8df 100644 --- a/src/all/hitomi/build.gradle +++ b/src/all/hitomi/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Hitomi' extClass = '.HitomiFactory' - extVersionCode = 34 + extVersionCode = 35 isNsfw = true } diff --git a/src/all/hitomi/src/eu/kanade/tachiyomi/extension/all/hitomi/Hitomi.kt b/src/all/hitomi/src/eu/kanade/tachiyomi/extension/all/hitomi/Hitomi.kt index 4e3aedc67..3d838a28d 100644 --- a/src/all/hitomi/src/eu/kanade/tachiyomi/extension/all/hitomi/Hitomi.kt +++ b/src/all/hitomi/src/eu/kanade/tachiyomi/extension/all/hitomi/Hitomi.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.extension.all.hitomi import android.app.Application import android.content.SharedPreferences +import android.util.Log import androidx.preference.ListPreference import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.network.GET @@ -29,6 +30,7 @@ import okhttp3.MediaType.Companion.toMediaType import okhttp3.Request import okhttp3.Response import okhttp3.ResponseBody.Companion.toResponseBody +import okhttp3.internal.http2.StreamResetException import rx.Observable import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -42,6 +44,7 @@ import java.util.LinkedList import java.util.Locale import kotlin.math.max import kotlin.math.min +import kotlin.time.Duration.Companion.seconds @OptIn(ExperimentalUnsignedTypes::class) class Hitomi( @@ -62,7 +65,10 @@ class Hitomi( private val json: Json by injectLazy() override val client = network.cloudflareClient.newBuilder() - .addInterceptor(::Intercept) + .addInterceptor(::jxlContentTypeInterceptor) + .apply { + interceptors().add(0, ::streamResetRetry) + } .build() private val preferences: SharedPreferences by lazy { @@ -708,7 +714,7 @@ class Hitomi( return this.sliceArray(byteArray.indices).contentEquals(byteArray) } - private fun Intercept(chain: Interceptor.Chain): Response { + private fun jxlContentTypeInterceptor(chain: Interceptor.Chain): Response { val response = chain.proceed(chain.request()) if (response.headers["Content-Type"] != "application/octet-stream") { return response @@ -728,6 +734,20 @@ class Hitomi( .build() } + private fun streamResetRetry(chain: Interceptor.Chain): Response { + return try { + chain.proceed(chain.request()) + } catch (e: StreamResetException) { + Log.e(name, "reset", e) + if (e.message.orEmpty().contains("INTERNAL_ERROR")) { + Thread.sleep(2.seconds.inWholeMilliseconds) + chain.proceed(chain.request()) + } else { + throw e + } + } + } + override fun popularMangaParse(response: Response) = throw UnsupportedOperationException() override fun popularMangaRequest(page: Int) = throw UnsupportedOperationException() override fun latestUpdatesRequest(page: Int) = throw UnsupportedOperationException()