应用后台时间过长时,下载崩溃的问题

This commit is contained in:
HapeLee
2026-05-13 18:18:03 +08:00
parent accd3afbbb
commit 4beeb2d4f6
@@ -49,6 +49,7 @@ class CacheBookService : BaseService() {
companion object { companion object {
private const val MB = 1024L * 1024L private const val MB = 1024L * 1024L
private const val DIAGNOSTICS_LOG_INTERVAL_MILLIS = 5_000L private const val DIAGNOSTICS_LOG_INTERVAL_MILLIS = 5_000L
private const val MAX_IDLE_SPIN_MS = 60_000L
var isRun = false var isRun = false
private set private set
@@ -181,15 +182,8 @@ class CacheBookService : BaseService() {
admissionIdleWaiters.values.flatten().forEach { it.complete(Unit) } admissionIdleWaiters.values.flatten().forEach { it.complete(Unit) }
admissionIdleWaiters.clear() admissionIdleWaiters.clear()
} }
serviceCommandScope.launch { CacheBook.close(clearFailureState = false)
try { serviceCommandScope.cancel()
serviceCommandMutex.withLock {
CacheBook.close(clearFailureState = false)
}
} finally {
serviceCommandScope.cancel()
}
}
super.onDestroy() super.onDestroy()
} }
@@ -486,14 +480,22 @@ class CacheBookService : BaseService() {
private suspend fun runDownloadLoop() { private suspend fun runDownloadLoop() {
try { try {
var lastActiveTime = System.currentTimeMillis()
while (currentCoroutineContext().isActive) { while (currentCoroutineContext().isActive) {
drainPendingDownloadRequests() drainPendingDownloadRequests()
if (CacheBook.isGloballyPaused) { if (CacheBook.isGloballyPaused) {
delay(200) break
continue }
val isActiveNow = CacheBook.isRun
|| hasPendingDownloadRequests()
|| hasAdmittingRequests()
if (isActiveNow) {
lastActiveTime = System.currentTimeMillis()
} else {
break
} }
if (!CacheBook.isRun) { if (!CacheBook.isRun) {
if (!hasPendingDownloadRequests() && !hasAdmittingRequests()) break if (System.currentTimeMillis() - lastActiveTime > MAX_IDLE_SPIN_MS) break
delay(200) delay(200)
continue continue
} }