From e9f2d267f673b0db73ec2d5dff37beb6e12dda39 Mon Sep 17 00:00:00 2001 From: Pauljd Date: Thu, 12 Nov 2026 08:22:13 +0000 Subject: [PATCH] Quiet first week on a new server: background 100 an hour, no collections or evidence reads Co-Authored-By: Claude Opus 5.5 --- .../java/tv/orbit/classic/EmbyConnectionScreen.kt | 4 +++- .../main/java/tv/orbit/classic/ServerRequests.kt | 6 ++++-- .../app/src/main/java/tv/orbit/classic/Settings.kt | 3 ++- .../src/main/java/tv/orbit/classic/Workspaces.kt | 13 +++++++++++++ .../test/java/tv/orbit/classic/SafetyLimitsTest.kt | 13 +++++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) 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 ac6c96e4..cf4fb257 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyConnectionScreen.kt @@ -432,6 +432,8 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea while (true) { // Refreshed each round, so it's there once the catalogue finishes its first full read (0.129 audit). withContext(Dispatchers.IO) { active.libraryTitles = runCatching { catalogue.libraryTitles(active.accountId) }.getOrNull() } + val quiet = withContext(Dispatchers.IO) { workspace.quietStart() } + BackgroundPacing.budget = if (quiet) QUIET_START_BUDGET else BackgroundPacing.HOURLY_BUDGET for (programming in SmartChannels) { val loop = smartLoop(programming) ?: run { // A channel of your own that is switched on but whose first plan never finished (an update arrived mid-read, @@ -458,7 +460,7 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea catch (cancelled: CancellationException) { throw cancelled } catch (_: Exception) { /* Tried again next hour. */ } // Stage C's light daily library refresh, in the background. A failure changes nothing and is tried next hour. - try { withContext(Dispatchers.IO) { BackgroundPacing.run { scheduler.readEvidence(active, active.accountId, catalogue) + if (!quiet) try { withContext(Dispatchers.IO) { BackgroundPacing.run { scheduler.readEvidence(active, active.accountId, catalogue) runCatching { scheduler.eventIdeas(active, active.accountId, catalogue) }.onFailure { android.util.Log.w("OrbitSchedule", "event ideas failed=${it.javaClass.simpleName}") } directorReports(scheduleStore, catalogue, active.accountId) } } } catch (cancelled: CancellationException) { throw cancelled } 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 b92e3660..35606ac9 100644 --- a/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt +++ b/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt @@ -105,6 +105,8 @@ internal object BackgroundPacing { const val GAP_MS = 1_500L /** 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]). */ + @Volatile var budget = HOURLY_BUDGET /** * Every request this TV makes, playback included, in any 60 minutes (12 November 2026). Watching is never stopped, but * once the TV reaches this, background work stops until the hour has room again, and Settings says so. @@ -162,12 +164,12 @@ internal object BackgroundPacing { val wait = lastMs + GAP_MS - now if (wait > 0) { sleep(wait); now = clock() } while (sent.isNotEmpty() && sent.first() <= now - 60 * 60_000L) sent.removeFirst() - if (sent.size >= HOURLY_BUDGET) throw PacedOut() + if (sent.size >= budget) throw PacedOut() if (ServerRequests.inLastHour() >= TOTAL_CEILING) { ceilingHitMs = now; throw PacedOut() } lastMs = now; sent.addLast(now) } } - internal fun resetForTest() { lastMs = 0; lastSetupMs = 0; sent.clear(); ceilingHitMs = null; clock = System::currentTimeMillis; sleep = { Thread.sleep(it) } } + internal fun resetForTest() { lastMs = 0; lastSetupMs = 0; sent.clear(); ceilingHitMs = null; budget = HOURLY_BUDGET; 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 e15c50a4..3c932c2a 100644 --- a/android/app/src/main/java/tv/orbit/classic/Settings.kt +++ b/android/app/src/main/java/tv/orbit/classic/Settings.kt @@ -663,4 +663,5 @@ internal fun SettingsSubPage(first: FocusRequester?, onBack: (() -> Unit)?, modi } /** How the background budget reads in Settings. */ -internal val BACKGROUND_BUDGET_LINE = "at most ${BackgroundPacing.HOURLY_BUDGET} an hour, one at a time" +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 "" diff --git a/android/app/src/main/java/tv/orbit/classic/Workspaces.kt b/android/app/src/main/java/tv/orbit/classic/Workspaces.kt index 4a33ba30..144fc2b6 100644 --- a/android/app/src/main/java/tv/orbit/classic/Workspaces.kt +++ b/android/app/src/main/java/tv/orbit/classic/Workspaces.kt @@ -35,8 +35,21 @@ internal class ServerWorkspace(val origin: String, val root: File, val artworkDi fun close() { if (catalogueOnce.isInitialized()) runCatching { catalogue.close() } } val schedules: ScheduleStore by lazy { scheduleDir.let { migrateChannelNumbers(it); ScheduleStore(it) } } internal var appContext: Context? = null + /** + * Quiet start (12 November 2026, after a ban): for the first [QUIET_START_MS] on a server, Orbit only does what playing + * its channels needs. Background reads keep to [QUIET_START_BUDGET] an hour, and the collections and the Chair's evidence + * aren't read at all. The clock starts the first time this box uses the server. + */ + fun quietStart(nowMs: Long = System.currentTimeMillis()): Boolean { + val stamp = File(root, "first-used") + val first = runCatching { stamp.readText().trim().toLong() }.getOrNull() ?: nowMs.also { runCatching { root.mkdirs(); stamp.writeText(it.toString()) } } + return nowMs - first < QUIET_START_MS + } } +internal const val QUIET_START_MS = 7 * 24 * 60 * 60_000L +internal const val QUIET_START_BUDGET = 100 + internal object Workspaces { /** The original server's key alias, as it always was. Others get their own, so removing one never touches another. */ private const val ORIGINAL_ALIAS = "orbit-connection-key-v1" 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 f7e451ec..1dfa182a 100644 --- a/android/app/src/test/java/tv/orbit/classic/SafetyLimitsTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/SafetyLimitsTest.kt @@ -45,4 +45,17 @@ class SafetyLimitsTest { // The rest begins exactly ten minutes after the last press (plus the same small margin as a programme's end). assertEquals(GUIDE_PICTURE_REST_MS + 50, programmeEndDelayMs(1_000L + GUIDE_PICTURE_REST_MS, 1_000L)) } + + @Test fun aNewServerGetsAQuietFirstWeek() { + val root = kotlin.io.path.createTempDirectory("quiet").toFile() + val w = ServerWorkspace("http://example.test", root, root, "alias", original = false) + assertTrue(w.quietStart(now)) // first use starts the clock + assertTrue(w.quietStart(now + 6 * 24 * 60 * 60_000L)) + assertFalse(w.quietStart(now + QUIET_START_MS)) + // The budget in force is what the limiter keeps to. + BackgroundPacing.budget = QUIET_START_BUDGET + var made = 0 + while (background()) made++ + assertEquals(100, made) + } }