feat: lazy background title fetch with disk cache and backoff

- Persist display names in per-channel JSON cache files (Name +
  DisplayName fields on cachedChannel). GetAllTitles reads all ch_*.json
  files; PutTitle updates a channel file in-place without losing messages.

- Replace blocking FetchTitles calls in fetchMeta and refreshChannel with
  an instant disk read (GetAllTitles) applied before the SSE broadcast,
  so channels appear with cached titles immediately on every load.

- ensureTitlesFetched runs in a single background goroutine (titlesMu +
  titlesLoading guard prevents duplicates). On success it persists titles
  and pushes an SSE update. On error or empty response it backs off for
  5 minutes so an old server does not cause endless retries.

- Block 0 of TitlesChannel now carries a uint16 total-block-count prefix
  (added in rebuildTitlesBlocks). FetchTitles reads the count from block 0
  and fetches all remaining blocks in parallel instead of sequentially.

- FetchTitles timeout raised from 10 s to 1 minute.
This commit is contained in:
Sepehr
2026-04-21 21:19:29 -04:00
parent 1496b00a94
commit 2bd4835674
4 changed files with 209 additions and 34 deletions
+8 -1
View File
@@ -166,6 +166,8 @@ func (f *Feed) getTitlesBlock(block int) ([]byte, error) {
}
// rebuildTitlesBlocks re-serializes the display name map and splits it into blocks.
// Block 0 is prefixed with a uint16 total-block count so the client can fetch all
// remaining blocks in parallel after reading the first one.
// Must be called with f.mu held.
func (f *Feed) rebuildTitlesBlocks() {
titles := make(map[string]string, len(f.channels))
@@ -175,7 +177,12 @@ func (f *Feed) rebuildTitlesBlocks() {
titles[name] = dn
}
}
f.titlesBlocks = protocol.SplitIntoBlocks(protocol.EncodeTitlesData(titles))
blocks := protocol.SplitIntoBlocks(protocol.EncodeTitlesData(titles))
if len(blocks) > 0 {
prefix := []byte{byte(len(blocks) >> 8), byte(len(blocks))}
blocks[0] = append(prefix, blocks[0]...)
}
f.titlesBlocks = blocks
}
func (f *Feed) rebuildVersionBlocks() {