When throttling E-Hentai, suspend the thread instead of sleeping it

This commit is contained in:
Jobobby04 2021-05-21 17:06:40 -04:00
parent fd65db51c1
commit 91ca176c28
3 changed files with 7 additions and 5 deletions

View File

@ -284,7 +284,7 @@ class EHentai(
override suspend fun getChapterList(manga: MangaInfo): List<ChapterInfo> = getChapterList(manga) {} override suspend fun getChapterList(manga: MangaInfo): List<ChapterInfo> = getChapterList(manga) {}
suspend fun getChapterList(manga: MangaInfo, throttleFunc: () -> Unit): List<ChapterInfo> { suspend fun getChapterList(manga: MangaInfo, throttleFunc: suspend () -> Unit): List<ChapterInfo> {
// Pull all the way to the root gallery // Pull all the way to the root gallery
// We can't do this with RxJava or we run into stack overflows on shit like this: // We can't do this with RxJava or we run into stack overflows on shit like this:
// https://exhentai.org/g/1073061/f9345f1c12/ // https://exhentai.org/g/1073061/f9345f1c12/
@ -357,7 +357,7 @@ class EHentai(
@Suppress("DeprecatedCallableAddReplaceWith") @Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Use getChapterList instead") @Deprecated("Use getChapterList instead")
fun fetchChapterList(manga: SManga, throttleFunc: () -> Unit) = runAsObservable({ fun fetchChapterList(manga: SManga, throttleFunc: suspend () -> Unit) = runAsObservable({
getChapterList(manga.toMangaInfo(), throttleFunc).map { it.toSChapter() } getChapterList(manga.toMangaInfo(), throttleFunc).map { it.toSChapter() }
}) })

View File

@ -53,7 +53,7 @@ class GalleryAdder {
url: String, url: String,
fav: Boolean = false, fav: Boolean = false,
forceSource: UrlImportableSource? = null, forceSource: UrlImportableSource? = null,
throttleFunc: () -> Unit = {} throttleFunc: suspend () -> Unit = {}
): GalleryAddEvent { ): GalleryAddEvent {
logger.d(context.getString(R.string.gallery_adder_importing_manga, url, fav.toString(), forceSource)) logger.d(context.getString(R.string.gallery_adder_importing_manga, url, fav.toString(), forceSource))
try { try {

View File

@ -1,5 +1,7 @@
package exh.eh package exh.eh
import kotlinx.coroutines.delay
class EHentaiThrottleManager( class EHentaiThrottleManager(
private val max: Int = THROTTLE_MAX, private val max: Int = THROTTLE_MAX,
private val inc: Int = THROTTLE_INC private val inc: Int = THROTTLE_INC
@ -8,12 +10,12 @@ class EHentaiThrottleManager(
var throttleTime: Long = 0 var throttleTime: Long = 0
private set private set
fun throttle() { suspend fun throttle() {
// Throttle requests if necessary // Throttle requests if necessary
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
val timeDiff = now - lastThrottleTime val timeDiff = now - lastThrottleTime
if (timeDiff < throttleTime) { if (timeDiff < throttleTime) {
Thread.sleep(throttleTime - timeDiff) delay(throttleTime - timeDiff)
} }
if (throttleTime < max) { if (throttleTime < max) {