Hitomi: retry on stream reset error like the website (#6802)

This commit is contained in:
AwkwardPeak7 2024-12-27 11:03:33 +05:00 committed by Draff
parent 919a6490bb
commit c3c87863b2
No known key found for this signature in database
GPG Key ID: E8A89F3211677653
2 changed files with 23 additions and 3 deletions

View File

@ -1,7 +1,7 @@
ext { ext {
extName = 'Hitomi' extName = 'Hitomi'
extClass = '.HitomiFactory' extClass = '.HitomiFactory'
extVersionCode = 34 extVersionCode = 35
isNsfw = true isNsfw = true
} }

View File

@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.extension.all.hitomi
import android.app.Application import android.app.Application
import android.content.SharedPreferences import android.content.SharedPreferences
import android.util.Log
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
@ -29,6 +30,7 @@ import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody import okhttp3.ResponseBody.Companion.toResponseBody
import okhttp3.internal.http2.StreamResetException
import rx.Observable import rx.Observable
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -42,6 +44,7 @@ import java.util.LinkedList
import java.util.Locale import java.util.Locale
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
import kotlin.time.Duration.Companion.seconds
@OptIn(ExperimentalUnsignedTypes::class) @OptIn(ExperimentalUnsignedTypes::class)
class Hitomi( class Hitomi(
@ -62,7 +65,10 @@ class Hitomi(
private val json: Json by injectLazy() private val json: Json by injectLazy()
override val client = network.cloudflareClient.newBuilder() override val client = network.cloudflareClient.newBuilder()
.addInterceptor(::Intercept) .addInterceptor(::jxlContentTypeInterceptor)
.apply {
interceptors().add(0, ::streamResetRetry)
}
.build() .build()
private val preferences: SharedPreferences by lazy { private val preferences: SharedPreferences by lazy {
@ -708,7 +714,7 @@ class Hitomi(
return this.sliceArray(byteArray.indices).contentEquals(byteArray) 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()) val response = chain.proceed(chain.request())
if (response.headers["Content-Type"] != "application/octet-stream") { if (response.headers["Content-Type"] != "application/octet-stream") {
return response return response
@ -728,6 +734,20 @@ class Hitomi(
.build() .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 popularMangaParse(response: Response) = throw UnsupportedOperationException()
override fun popularMangaRequest(page: Int) = throw UnsupportedOperationException() override fun popularMangaRequest(page: Int) = throw UnsupportedOperationException()
override fun latestUpdatesRequest(page: Int) = throw UnsupportedOperationException() override fun latestUpdatesRequest(page: Int) = throw UnsupportedOperationException()