diff --git a/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt b/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt index 29e0df05..4d7b6465 100644 --- a/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt +++ b/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt @@ -91,6 +91,19 @@ internal class ChannelScheduler(private val store: ScheduleStore, Log.i("OrbitSchedule", "arrivals pages=${run.pages} read=${found.size} new=${arrivals.size} baseline=$baseline") } + /** + * The library read overnight until it's whole (12 November 2026: a 115,000-title library at the daytime 1,000 a day would + * take four months). Between 01:00 and 06:00, five pages of 200 an hour, six minutes apart: 5,000 titles a night, and + * nothing while people are likely to be watching. The daytime refresh carries on as before; once the read is whole this + * does nothing. + */ + fun overnightCatalogue(session: EmbySampleSession, userId: String, catalogue: CatalogueStore, nowMs: Long = clock(), pause: () -> Unit = { Thread.sleep(OVERNIGHT_PAGE_GAP_MS) }) { + val hour = java.time.Instant.ofEpochMilli(nowMs).atZone(java.time.ZoneId.systemDefault()).hour + if (hour !in OVERNIGHT_HOURS || catalogue.complete()) return + val run = session.importCatalogue(catalogue, OVERNIGHT_PAGES_PER_HOUR, pause = pause) + Log.i("OrbitSchedule", "overnight catalogue pages=${run.pages} read=${run.records} finished=${run.finished}") + } + /** * Stage D evidence, when asked for from adb: every film and series on a channel's list and every library title premiered in * the last 60 days, read in batches and appended to the evidence file, skipping any already read. Read only. @@ -388,3 +401,7 @@ internal object SeriesInspection { @Volatile var title: String? = null fun take(): String? = title.also { title = null } } + +internal val OVERNIGHT_HOURS = 1..5 +internal const val OVERNIGHT_PAGES_PER_HOUR = 5 +internal const val OVERNIGHT_PAGE_GAP_MS = 6 * 60_000L diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt b/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt index 8d4f82fc..3fb55ff2 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt @@ -104,7 +104,9 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI BackgroundPacing.beforeRequest() val kind = RequestKind.of(request.method, request.path) var ok = false - try { return send(request).also { ok = it.status in 200..299 } } finally { ServerRequests.count(kind, ok) } + val began = System.currentTimeMillis() + try { return send(request).also { ok = it.status in 200..299 } } + finally { ServerRequests.count(kind, ok); BackgroundPacing.observe(System.currentTimeMillis() - began, ok) } } private fun send(request: EmbyRequest): EmbyReply { // HTTPS, or plain HTTP to a home server whose name still leads only to private addresses. diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt b/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt index 0e0e5b30..ff7c51ff 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt @@ -465,6 +465,9 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea directorReports(scheduleStore, catalogue, active.accountId) } } } catch (cancelled: CancellationException) { throw cancelled } catch (failure: Exception) { android.util.Log.w("OrbitSchedule", "evidence failed=${failure.javaClass.simpleName}") } + if (!quiet) try { withContext(Dispatchers.IO) { BackgroundPacing.run { scheduler.overnightCatalogue(active, active.accountId, catalogue) } } } + catch (cancelled: CancellationException) { throw cancelled } + catch (failure: Exception) { android.util.Log.w("OrbitSchedule", "overnight catalogue failed=${failure.javaClass.simpleName}") } try { withContext(Dispatchers.IO) { BackgroundPacing.run { scheduler.refreshArrivals(active, active.accountId, catalogue) } } } catch (cancelled: CancellationException) { throw cancelled } catch (failure: Exception) { android.util.Log.w("OrbitSchedule", "arrivals failed=${failure.javaClass.simpleName}") } diff --git a/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt b/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt index 35606ac9..5d5117b6 100644 --- a/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt +++ b/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt @@ -103,6 +103,27 @@ internal class PacedOut : EmbyCheckException("Orbit is pacing its background rea internal object BackgroundPacing { /** At most one background request this often: 1.5 s is at most 2,400 an hour, and the budget keeps it well under. */ const val GAP_MS = 1_500L + /** The longest gap the back-off goes to. */ + const val MAX_GAP_MS = 60_000L + /** + * The gap in force (12 November 2026, for a 3-core VPS): it doubles whenever a background request takes over + * [SLOW_MS] or the server answers with an error, up to [MAX_GAP_MS], and eases back towards [GAP_MS] as answers come + * quickly again. Watching is never slowed. + */ + @Volatile var gapMs = GAP_MS + private set + const val SLOW_MS = 2_000L + /** Orbit is going slower because the server seemed busy. */ + val easedOff get() = gapMs > GAP_MS + /** Called after each request with how long it took and whether it worked; only background requests move the gap. */ + @Synchronized fun observe(tookMs: Long, ok: Boolean) { + if (!active) return + gapMs = when { + !ok || tookMs > SLOW_MS -> minOf(MAX_GAP_MS, gapMs * 2) + tookMs < SLOW_MS / 4 -> maxOf(GAP_MS, gapMs * 4 / 5) + else -> gapMs + } + } /** Background requests per TV in any 60 minutes (Paul: never thousands an hour again; 200 since Orbit was banned from a share, 12 November 2026). */ const val HOURLY_BUDGET = 200 /** The budget in force: [HOURLY_BUDGET], or less during a new server's quiet start ([ServerWorkspace.quietStart]). */ @@ -161,7 +182,7 @@ internal object BackgroundPacing { // Wait first, then count the request when it is actually sent, against the 60 minutes before it (0.129 audit: a // clock-hour count let one round spend the end of one hour and all of the next, about 1,000 in 60 minutes). var now = clock() - val wait = lastMs + GAP_MS - now + val wait = lastMs + gapMs - now if (wait > 0) { sleep(wait); now = clock() } while (sent.isNotEmpty() && sent.first() <= now - 60 * 60_000L) sent.removeFirst() if (sent.size >= budget) throw PacedOut() @@ -169,7 +190,7 @@ internal object BackgroundPacing { lastMs = now; sent.addLast(now) } } - internal fun resetForTest() { lastMs = 0; lastSetupMs = 0; sent.clear(); ceilingHitMs = null; budget = HOURLY_BUDGET; clock = System::currentTimeMillis; sleep = { Thread.sleep(it) } } + internal fun resetForTest() { lastMs = 0; lastSetupMs = 0; sent.clear(); ceilingHitMs = null; budget = HOURLY_BUDGET; gapMs = GAP_MS; clock = System::currentTimeMillis; sleep = { Thread.sleep(it) } } @Synchronized internal fun usedThisHour(): Int { val now = clock(); return sent.count { it > now - 60 * 60_000L } } } diff --git a/android/app/src/main/java/tv/orbit/classic/Settings.kt b/android/app/src/main/java/tv/orbit/classic/Settings.kt index 3c932c2a..1a30de07 100644 --- a/android/app/src/main/java/tv/orbit/classic/Settings.kt +++ b/android/app/src/main/java/tv/orbit/classic/Settings.kt @@ -664,4 +664,5 @@ internal fun SettingsSubPage(first: FocusRequester?, onBack: (() -> Unit)?, modi /** How the background budget reads in Settings. */ internal val BACKGROUND_BUDGET_LINE get() = "at most ${BackgroundPacing.budget} an hour, one at a time" + - if (BackgroundPacing.budget < BackgroundPacing.HOURLY_BUDGET) " (a quiet first week on this server: collections aren't read yet)" else "" + (if (BackgroundPacing.budget < BackgroundPacing.HOURLY_BUDGET) " (a quiet first week on this server: collections aren't read yet)" else "") + + (if (BackgroundPacing.easedOff) ". The server seemed busy, so Orbit is going slower for now" else "") diff --git a/android/app/src/test/java/tv/orbit/classic/SafetyLimitsTest.kt b/android/app/src/test/java/tv/orbit/classic/SafetyLimitsTest.kt index 1dfa182a..f2406610 100644 --- a/android/app/src/test/java/tv/orbit/classic/SafetyLimitsTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/SafetyLimitsTest.kt @@ -58,4 +58,23 @@ class SafetyLimitsTest { while (background()) made++ assertEquals(100, made) } + + @Test fun backgroundEasesOffWhenTheServerIsSlowAndRecovers() { + BackgroundPacing.run { + repeat(3) { BackgroundPacing.observe(3_000, true) } // three slow answers + assertEquals(12_000L, BackgroundPacing.gapMs); assertTrue(BackgroundPacing.easedOff) + repeat(10) { BackgroundPacing.observe(100, false) } // errors: up to the ceiling, no further + assertEquals(BackgroundPacing.MAX_GAP_MS, BackgroundPacing.gapMs) + repeat(40) { BackgroundPacing.observe(100, true) } // quick again: back to normal, not below + assertEquals(BackgroundPacing.GAP_MS, BackgroundPacing.gapMs); assertFalse(BackgroundPacing.easedOff) + } + // Watching never moves it. + BackgroundPacing.observe(30_000, false) + assertEquals(BackgroundPacing.GAP_MS, BackgroundPacing.gapMs) + } + + @Test fun theOvernightReadOnlyRunsInTheSmallHours() { + assertEquals(1..5, OVERNIGHT_HOURS) + assertEquals(5_000, OVERNIGHT_HOURS.count() * OVERNIGHT_PAGES_PER_HOUR * CATALOGUE_PAGE_SIZE) + } }