From 72ea89a8b49743ce82b577e06597e4dd42e97ee7 Mon Sep 17 00:00:00 2001 From: Pauljd Date: Mon, 30 Nov 2026 08:23:31 +0000 Subject: [PATCH] Channel setup may use the household's whole 600 an hour while the server is quick Paul, 30 November 2026: PUNCHLINE's 260 films took half an hour at a two-TV share. Once the server slows, the household share applies again. Background reading is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../main/java/tv/orbit/classic/ServerRequests.kt | 6 +++++- .../java/tv/orbit/classic/HouseholdBudgetTest.kt | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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 8a74fa43..9fed6c35 100644 --- a/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt +++ b/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt @@ -179,6 +179,8 @@ internal object BackgroundPacing { private val setupSent = ArrayDeque() /** Setup requests a household may make in any 60 minutes at full speed (see [beforeRequest]). */ const val SETUP_HOURLY = 600 + /** This TV's setup allowance for the hour: all of [SETUP_HOURLY] while the server is answering quickly, else its household share. */ + fun setupCap() = if (easedOff) share(SETUP_HOURLY) else SETUP_HOURLY /** Called before each request; waits its turn for background work, or refuses once the hour's budget is spent. */ fun beforeRequest() { @@ -190,7 +192,9 @@ internal object BackgroundPacing { // set up always finishes; it just takes longer. val now0 = clock() while (setupSent.isNotEmpty() && setupSent.first() <= now0 - 60 * 60_000L) setupSent.removeFirst() - val cap = share(SETUP_HOURLY) + // While the server answers quickly, one TV setting up may use the whole of SETUP_HOURLY (Paul, 30 November + // 2026: PUNCHLINE's 260 films took half an hour at a two-TV share). Once it has slowed, the household share applies. + val cap = setupCap() val gap = if (setupSent.size >= cap) 60 * 60_000L / cap else SETUP_GAP_MS val wait = lastSetupMs + gap - now0 if (wait > 0) sleep(wait) diff --git a/android/app/src/test/java/tv/orbit/classic/HouseholdBudgetTest.kt b/android/app/src/test/java/tv/orbit/classic/HouseholdBudgetTest.kt index 3c4c3a75..180f4db4 100644 --- a/android/app/src/test/java/tv/orbit/classic/HouseholdBudgetTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/HouseholdBudgetTest.kt @@ -30,6 +30,21 @@ class SetupBudgetTest { BackgroundPacing.clock = { now } BackgroundPacing.sleep = { ms -> waits += ms; now += ms } BackgroundPacing.tvs = 2 + // A quick server: one TV setting up may use the whole 600 an hour, then the 601st waits 6 s. + BackgroundPacing.setup { repeat(601) { BackgroundPacing.beforeRequest() } } + assertEquals(1_000L, waits[waits.size - 2]) + assertEquals(6_000L, waits.last()) + } + + @Test fun aBusyServerGetsOnlyTheHouseholdShare() { + var now = 0L + val waits = mutableListOf() + BackgroundPacing.resetForTest() + BackgroundPacing.clock = { now } + BackgroundPacing.sleep = { ms -> waits += ms; now += ms } + BackgroundPacing.tvs = 2 + BackgroundPacing.run { BackgroundPacing.observe(5_000, ok = true) } // a slow answer: Orbit eases off + assertTrue(BackgroundPacing.easedOff) BackgroundPacing.setup { repeat(301) { BackgroundPacing.beforeRequest() } } // 300 at a second apart, then the 301st waits 12 s (300 an hour for each of two TVs). assertEquals(1_000L, waits[waits.size - 2])