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 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 08:23:31 +00:00
co-authored by Claude Opus 5.5
parent 9a9574cccb
commit 72ea89a8b4
2 changed files with 20 additions and 1 deletions
@@ -179,6 +179,8 @@ internal object BackgroundPacing {
private val setupSent = ArrayDeque<Long>()
/** 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)
@@ -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<Long>()
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])