perf(guide): Show a stale guide while it refreshes, and revalidate with ETag #33

Merged
namillis merged 2 commits from perf/guide-stale-etag into main 2026-11-09 14:43:51 +00:00
Owner

Merge #32 first. Both PRs change the same spots in HomeViewModel and GuideViewModel, so this branch has #32 merged in. Until #32 lands, this diff includes #32's commit too; afterwards it shows only this PR's changes.

What was wrong

Once the cached guide passed its 3-hour TTL (EpgCachePolicy.DEFAULT_TTL_MS), the Guide, Home's now-playing and Search all waited for a full xmltv.php download and parse: 3.4 MB gzipped, 22.5 MB of XML from the pool (our IPTV pool server). That happened even when the guide hadn't changed. The pool sends an ETag and answers a matching If-None-Match with a 304, but the app never sent one.

What changed

  • EpgCache.kt returns a stale entry straight away when it still covers the requested window and channels, and starts one background refresh under the same per-provider lock, so there is still one download at a time. It blocks only when no entry covers the request. A failed refresh keeps the stale guide, and the next open after a minute tries again.
  • EpgRepository.updates fires whenever a refresh downloads a guide (any 200); a 304 doesn't fire it. HomeViewModel, GuideViewModel and SearchViewModel then fetch again, which is served from memory. The Guide asks again for the archive (past) listings of rows that had them. Search keeps its current results if the new fetch returns nothing.
  • Each entry stores the guide response's ETag. A refresh sends it as If-None-Match; on a 304 the entry is marked fresh in memory and nothing is downloaded or parsed. The disk copy keeps its old time, so each later launch revalidates once, costing one 304, until a changed guide rewrites the disk copy. A download for a window the entry doesn't cover goes without the header, since an unchanged guide still wouldn't cover it.
  • forceRefresh still waits, and also sends the ETag. Nothing in the UI calls it today.
  • To work with #32's late channel list, the Guide's new-guide handler also stores the guide it applied, so new rows built from a later channel list use that guide.
  • The HTTP part moved into EpgRepository.downloadGuide so GuideDownloadTest.kt can drive it with a fake server.

Verified

  • testDebugUnitTest, lintDebug and assembleDebug pass on this branch with #32 merged in (591 tests), and with #34 merged on top as well (600 tests). The combined build ran on the emulator.
  • EpgCacheTest.kt replaces "a stale entry reloads" with 9 cases: stale served then refreshed, 304 keeps the entry, a changed guide tells the screens, one refresh at a time, failure backoff, forced refresh, an uncovered window skips the ETag, the ETag surviving a restart, and a refresh landing after clearProvider being dropped. GuideDownloadTest.kt adds 5 HTTP cases: the header sent or not, 304, a changed ETag, no ETag, and an error status.
  • Against the pool with the tv3 test login, using curl: the full guide took 1.28 s and 3.35 MB. The same request with its ETag got a 304 in 0.11 s.
  • LiveWireTV_API34 emulator with the tv3 login, with temporary logging (removed before commit). To make a stale guide visible I aged the saved guide's timestamp by 4 hours and put "STALE " in front of CBS 9 Washington's titles in the saved file:
    • With a stored ETag that doesn't match the server's: Home had the stale guide 0.5 s after asking and showed "STALE Entertainment Tonight". The background refresh got a 200 in 2.65 s, and about 0.3 s later Home showed "Entertainment Tonight".
    • With a matching ETag: the refresh got a 304 in 46 ms. The Guide's Local DC category kept showing the saved titles, which shows nothing was downloaded or parsed.
    • Guide open when a refresh landed: the log showed the Guide applying the new guide in place.
**Merge #32 first.** Both PRs change the same spots in `HomeViewModel` and `GuideViewModel`, so this branch has #32 merged in. Until #32 lands, this diff includes #32's commit too; afterwards it shows only this PR's changes. ## What was wrong Once the cached guide passed its 3-hour TTL (`EpgCachePolicy.DEFAULT_TTL_MS`), the Guide, Home's now-playing and Search all waited for a full `xmltv.php` download and parse: 3.4 MB gzipped, 22.5 MB of XML from the pool (our IPTV pool server). That happened even when the guide hadn't changed. The pool sends an `ETag` and answers a matching `If-None-Match` with a 304, but the app never sent one. ## What changed - `EpgCache.kt` returns a stale entry straight away when it still covers the requested window and channels, and starts one background refresh under the same per-provider lock, so there is still one download at a time. It blocks only when no entry covers the request. A failed refresh keeps the stale guide, and the next open after a minute tries again. - `EpgRepository.updates` fires whenever a refresh downloads a guide (any 200); a 304 doesn't fire it. `HomeViewModel`, `GuideViewModel` and `SearchViewModel` then fetch again, which is served from memory. The Guide asks again for the archive (past) listings of rows that had them. Search keeps its current results if the new fetch returns nothing. - Each entry stores the guide response's ETag. A refresh sends it as `If-None-Match`; on a 304 the entry is marked fresh in memory and nothing is downloaded or parsed. The disk copy keeps its old time, so each later launch revalidates once, costing one 304, until a changed guide rewrites the disk copy. A download for a window the entry doesn't cover goes without the header, since an unchanged guide still wouldn't cover it. - `forceRefresh` still waits, and also sends the ETag. Nothing in the UI calls it today. - To work with #32's late channel list, the Guide's new-guide handler also stores the guide it applied, so new rows built from a later channel list use that guide. - The HTTP part moved into `EpgRepository.downloadGuide` so `GuideDownloadTest.kt` can drive it with a fake server. ## Verified - `testDebugUnitTest`, `lintDebug` and `assembleDebug` pass on this branch with #32 merged in (591 tests), and with #34 merged on top as well (600 tests). The combined build ran on the emulator. - `EpgCacheTest.kt` replaces "a stale entry reloads" with 9 cases: stale served then refreshed, 304 keeps the entry, a changed guide tells the screens, one refresh at a time, failure backoff, forced refresh, an uncovered window skips the ETag, the ETag surviving a restart, and a refresh landing after `clearProvider` being dropped. `GuideDownloadTest.kt` adds 5 HTTP cases: the header sent or not, 304, a changed ETag, no ETag, and an error status. - Against the pool with the tv3 test login, using curl: the full guide took 1.28 s and 3.35 MB. The same request with its ETag got a 304 in 0.11 s. - LiveWireTV_API34 emulator with the tv3 login, with temporary logging (removed before commit). To make a stale guide visible I aged the saved guide's timestamp by 4 hours and put "STALE " in front of CBS 9 Washington's titles in the saved file: - With a stored ETag that doesn't match the server's: Home had the stale guide 0.5 s after asking and showed "STALE Entertainment Tonight". The background refresh got a 200 in 2.65 s, and about 0.3 s later Home showed "Entertainment Tonight". - With a matching ETag: the refresh got a 304 in 46 ms. The Guide's Local DC category kept showing the saved titles, which shows nothing was downloaded or parsed. - Guide open when a refresh landed: the log showed the Guide applying the new guide in place.
perf(guide): Show a stale guide while it refreshes, and revalidate with ETag
All checks were successful
CI / test (pull_request) Successful in 7m17s
4ce72d491e
Once the cached guide passed its 3-hour TTL, the Guide, Home's now-playing
and Search all waited for a full xmltv.php download and parse (3.4 MB
gzipped, 22.5 MB of XML from the pool), even when the guide hadn't
changed.

EpgCache now returns a stale entry straight away when it still covers the
requested window and channels, and starts one background refresh under
the same per-provider lock. It blocks only when no entry covers the
request. A failed refresh keeps the stale guide and isn't retried for a
minute. EpgRepository.updates fires when a refresh brings a changed
guide, and Home, the Guide and Search fetch again from memory.

Each entry now stores the guide response's ETag. A refresh of a covering
entry sends it as If-None-Match; on a 304 the entry is marked fresh in
memory and nothing is downloaded or parsed. A download for a window the
entry doesn't cover goes without the header, since an unchanged guide
still wouldn't cover it. forceRefresh still waits, and also sends the
ETag.

The HTTP part moves into EpgRepository.downloadGuide so it can be tested
with a fake server.
perf(channels): Save the channel list to disk and show it before refreshing
All checks were successful
CI / test (pull_request) Successful in 7m6s
8a5fa8e97a
Every cold start waited for the full get_live_streams list (about 7.8 MB of
JSON for the pool) before Home, the Guide, Search or Sports had it, because
LiveChannelCache only kept it in memory for 10 minutes.

LiveChannelCache now writes each Xtream provider's parsed list to
cache/channel_cache, named by a hash of the provider's id, URL and login.
On launch the disk copy is returned straight away and refreshed once in the
background. A copy older than the 10-minute TTL is also served at once
while one background refresh runs, retried a few times if it fails. Only a
provider with no list at all makes the caller wait.

The disk copy has no pool events, so Home and the Guide now follow
ProviderRepository.liveChannelUpdates and swap in the new list when it
lands. The Guide keeps rows whose channel didn't change, and the focus
stays where it was. Home's own stale refresh uses freshLiveChannels so it
compares against the new list.

M3U lists stay in memory only: their catch-up templates can hold
credentials, and the player loads the playlist anyway. Editing or deleting
a provider deletes its file. Empty lists are still never cached.
Merge remote-tracking branch 'origin/perf/channel-disk-cache' into perf/guide-stale-etag
All checks were successful
CI / test (pull_request) Successful in 6m31s
43ccdbbd44
# Conflicts:
#	app/src/main/kotlin/com/livewire/tv/feature/epg/GuideViewModel.kt
#	app/src/main/kotlin/com/livewire/tv/feature/home/HomeViewModel.kt
namillis deleted branch perf/guide-stale-etag 2026-11-09 14:43:51 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
namillis/LiveWireTV!33
No description provided.