Share semaphore

This commit is contained in:
Dude so hot authored and renovate[bot] committed 2024-10-21 20:54:37 +08:00
1 parent aed3d08167
commit 93e2d36dbb
1 file changed
+2 -5
@@ -5,7 +5,6 @@ import coil3.network.NetworkRequest
import coil3.network.NetworkResponse
import coil3.network.ktor3.asNetworkClient
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.URL_PREFIX_THUMB_E
import com.hippo.ehviewer.client.URL_PREFIX_THUMB_EX
import io.ktor.client.HttpClient
import kotlinx.coroutines.NonCancellable
@@ -14,13 +13,11 @@ import kotlinx.coroutines.sync.withPermit
import kotlinx.coroutines.withContext
class LimitConcurrencyNetworkClient(val impl: NetworkClient) : NetworkClient {
val eSemaphore = Semaphore(Settings.thumbConcurrency)
val exSemaphore = Semaphore(Settings.thumbConcurrency)
val semaphore = Semaphore(Settings.thumbConcurrency)
override suspend fun <T> executeRequest(req: NetworkRequest, f: suspend (NetworkResponse) -> T): T {
val url = req.url
return when {
URL_PREFIX_THUMB_E in url -> eSemaphore.withPermit { impl.executeRequest(req, f) }
URL_PREFIX_THUMB_EX in url -> exSemaphore.withPermit { withContext(NonCancellable) { impl.executeRequest(req, f) } }
URL_PREFIX_THUMB_EX in url -> semaphore.withPermit { withContext(NonCancellable) { impl.executeRequest(req, f) } }
else -> impl.executeRequest(req, f)
}
}