Lock concurrency for Normal previews as well

This commit is contained in:
Dude so hot authored and renovate[bot] committed 2024-10-21 20:54:37 +08:00
1 parent 31f97cb840
commit 0dfee513f4
2 files changed
+10 -4

No files matched your search

@@ -25,6 +25,7 @@ import com.hippo.ehviewer.client.data.NormalGalleryPreview
const val URL_PREFIX_THUMB_E = "https://ehgt.org/"
const val URL_PREFIX_THUMB_EX = "https://s.exhentai.org/t/"
const val URL_SIGNATURE_THUMB_NORMAL = ".hath.network"
private const val NORMAL_PREVIEW_PREFIX = "$"
private val NormalPreviewKeyRegex = Regex("/(\\d+-\\d+)\\.jpg$")
@@ -6,18 +6,23 @@ import coil3.network.NetworkResponse
import coil3.network.ktor3.asNetworkClient
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.URL_PREFIX_THUMB_EX
import com.hippo.ehviewer.client.URL_SIGNATURE_THUMB_NORMAL
import io.ktor.client.HttpClient
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import kotlinx.coroutines.withContext
import moe.tarsin.coroutines.NamedSemaphore
import moe.tarsin.coroutines.withPermit
class LimitConcurrencyNetworkClient(val impl: NetworkClient) : NetworkClient {
val semaphore = Semaphore(Settings.thumbConcurrency)
val semaphores = NamedSemaphore<String>(permits = Settings.thumbConcurrency)
override suspend fun <T> executeRequest(req: NetworkRequest, f: suspend (NetworkResponse) -> T): T {
val url = req.url
return when {
URL_PREFIX_THUMB_EX in url -> semaphore.withPermit { withContext(NonCancellable) { impl.executeRequest(req, f) } }
// Ex thumb server has no h2 multiplexing support
URL_PREFIX_THUMB_EX in url -> semaphores.withPermit(URL_PREFIX_THUMB_EX) { withContext(NonCancellable) { impl.executeRequest(req, f) } }
// H@H server has no h2 multiplexing support
URL_SIGNATURE_THUMB_NORMAL in url -> semaphores.withPermit(url.substringBefore(URL_SIGNATURE_THUMB_NORMAL)) { withContext(NonCancellable) { impl.executeRequest(req, f) } }
// H2 multiplexing enabled
else -> impl.executeRequest(req, f)
}
}