Fix image download queue not respecting FIFO order

This commit is contained in:
FooIbar committed 2024-09-25 22:58:36 +08:00
1 parent 24d2072586
commit 8447760cb5
1 file changed
+7 -9
@@ -504,7 +504,6 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
private val showKeyLock = Mutex()
private val mDownloadDelay = Settings.downloadDelay.milliseconds
private val downloadTimeout = Settings.downloadTimeout.seconds
private val delayLock = Mutex()
private var lastRequestTime = TimeSource.Monotonic.markNow()
var isDownloadMode = false
private set
@@ -541,13 +540,6 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
if (currentJob?.isActive != true) {
mFetcherJobMap[index] = launch {
runCatching {
if (!force && index in mSpiderDen) {
return@runCatching updatePageState(index, STATE_FINISHED)
}
delayLock.withLock {
delay(mDownloadDelay - lastRequestTime.elapsedNow())
lastRequestTime = TimeSource.Monotonic.markNow()
}
mSemaphore.withPermit {
doInJob(index, force, orgImg, skipHath)
}
@@ -584,7 +576,6 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
?: getPTokenFromInternet(index)
?: getPTokenFromMultiPageViewer(index)
}
updatePageState(index, STATE_DOWNLOADING)
val previousPToken: String?
val pToken: String
pTokenLock.withLock {
@@ -592,13 +583,20 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
if (mSpiderInfo.pTokenMap[index] == TOKEN_FAILED) {
mSpiderInfo.pTokenMap.remove(index)
}
} else if (index in mSpiderDen) {
return updatePageState(index, STATE_FINISHED)
}
pToken = getPToken(index)
?: return updatePageState(index, STATE_FAILED, PTOKEN_FAILED_MESSAGE).also {
mSpiderInfo.pTokenMap[index] = TOKEN_FAILED
}
previousPToken = getPToken(index - 1)
// The lock for delay should be acquired before anything else to maintain FIFO order
delay(mDownloadDelay - lastRequestTime.elapsedNow())
lastRequestTime = TimeSource.Monotonic.markNow()
}
updatePageState(index, STATE_DOWNLOADING)
var skipHathKey: String? = null
var originImageUrl: String? = null