Skip to content

The agent that grows with you

fix(webapp): retry a reused clip URL once; bound the URL cache

Teknium74a0e1f462c7
diff --git a/apps/desktop/src/hooks/use-media-playback-src.remount.test.tsx b/apps/desktop/src/hooks/use-media-playback-src.remount.test.tsx
index 727d1eb9e57..c4e7fbf82d8 100644
--- a/apps/desktop/src/hooks/use-media-playback-src.remount.test.tsx
+++ b/apps/desktop/src/hooks/use-media-playback-src.remount.test.tsx
@@ -18,9 +18,14 @@ vi.mock('@/lib/media', async importOriginal => ({
const resolve = vi.mocked(resolveMediaPlaybackSrc)
function Player() {
- const { onError, src } = useMediaPlaybackSrc('/srv/clip.mp4')
+ const { failed, onError, src } = useMediaPlaybackSrc('/srv/clip.mp4')
- return src ? <video data-testid="video" onError={onError} preload="metadata" src={src} /> : null
+ return (
+ <>
+ {src && <video data-testid="video" onError={onError} preload="metadata" src={src} />}
+ {failed && <span>failed</span>}
+ </>
+ )
}
const src = () => screen.getByTestId('video').getAttribute('src')
@@ -34,18 +39,27 @@ afterEach(() => {
resolve.mockReset()
})
-it('a remounted clip keeps its stream URL until that URL fails', async () => {
+it('a remounted clip keeps its stream URL', async () => {
const first = render(<Player />)
await waitFor(() => expect(src()).toMatch(/ticket=t1$/))
first.unmount()
- const second = render(<Player />)
+ render(<Player />)
expect(src()).toMatch(/ticket=t1$/)
expect(resolve).toHaveBeenCalledTimes(1)
+})
+
+it('a reused URL that fails before loading (a dead ticket) gets one fresh URL on the same mount', async () => {
+ const first = render(<Player />)
+ await waitFor(() => expect(src()).toMatch(/ticket=t1$/))
+ first.unmount()
- // A source that fails before it ever loaded is a real failure: the next mount resolves afresh.
- fireEvent.error(screen.getByTestId('video'))
- second.unmount()
render(<Player />)
+ expect(src()).toMatch(/ticket=t1$/)
+ fireEvent.error(screen.getByTestId('video'))
await waitFor(() => expect(src()).toMatch(/ticket=t2$/))
+
+ // The fresh URL failing too is a real failure.
+ fireEvent.error(screen.getByTestId('video'))
+ await screen.findByText('failed')
})
diff --git a/apps/desktop/src/hooks/use-media-playback-src.ts b/apps/desktop/src/hooks/use-media-playback-src.ts
index 0dee78a7a1a..2ce13999c87 100644
--- a/apps/desktop/src/hooks/use-media-playback-src.ts
+++ b/apps/desktop/src/hooks/use-media-playback-src.ts
@@ -52,7 +52,14 @@ export function useMediaPlaybackSrc(path: string) {
sessionId ?? ''
])
- const [media, setMedia] = useState(() => ({ failed: false, path, src: cachedPlaybackSrc(cacheKey) ?? '' }))
+ // `reused`: the URL came from an earlier mount, so its ticket can have died with a gateway restart
+ // and its first error gets one fresh URL even before it loaded.
+ const [media, setMedia] = useState(() => {
+ const cached = cachedPlaybackSrc(cacheKey)
+
+ return { failed: false, path, reused: Boolean(cached), src: cached ?? '' }
+ })
+
const loadedSrc = useRef('')
// Minted for a source that failed before it ever loaded; it gets no retry of its own.
const retriedSrc = useRef('')
@@ -64,7 +71,9 @@ export function useMediaPlaybackSrc(path: string) {
const cached = kind === 'file' ? undefined : cachedPlaybackSrc(cacheKey)
setMedia(current =>
- cached && current.path === path && current.src === cached ? current : { failed: kind === 'file', path, src: cached ?? '' }
+ cached && current.path === path && current.src === cached
+ ? current
+ : { failed: kind === 'file', path, reused: Boolean(cached), src: cached ?? '' }
)
if (kind === 'file' || cached) {
@@ -80,14 +89,14 @@ export function useMediaPlaybackSrc(path: string) {
}
if (!cancelled) {
- setMedia({ failed: false, path, src: value })
+ setMedia({ failed: false, path, reused: false, src: value })
} else if (objectUrl) {
URL.revokeObjectURL(objectUrl)
}
})
.catch(() => {
if (!cancelled) {
- setMedia({ failed: true, path, src: '' })
+ setMedia({ failed: true, path, reused: false, src: '' })
}
})
@@ -133,7 +142,14 @@ export function useMediaPlaybackSrc(path: string) {
const neverLoaded = loadedSrc.current !== failing
// A source that never loaded can only have expired if its element waited to fetch it
// (preload="none" until played); it gets one fresh ticket. Blob sources never expire.
- const realFailure = neverLoaded && (element.preload !== 'none' || retriedSrc.current === failing)
+ const reused = media.reused && media.src === failing
+
+ if (reused) {
+ forgetPlaybackSrc(cacheKey)
+ }
+
+ const realFailure =
+ neverLoaded && !reused && (element.preload !== 'none' || retriedSrc.current === failing)
if (failing.startsWith('blob:') || realFailure) {
fail()
@@ -161,11 +177,11 @@ export function useMediaPlaybackSrc(path: string) {
rememberPlaybackSrc(cacheKey, fresh)
resume.current = { ...point, src: fresh }
- setMedia(current => (current.src === failing ? { ...current, src: fresh } : current))
+ setMedia(current => (current.src === failing ? { ...current, reused: false, src: fresh } : current))
})
.catch(fail)
},
- [cacheKey, owner, path, sessionId]
+ [cacheKey, media.reused, media.src, owner, path, sessionId]
)
// An element with preload="none" fetches nothing for a new source until asked, so one
diff --git a/apps/desktop/src/lib/playback-source-cache.ts b/apps/desktop/src/lib/playback-source-cache.ts
index 4f43ccc6a69..03667d726e1 100644
--- a/apps/desktop/src/lib/playback-source-cache.ts
+++ b/apps/desktop/src/lib/playback-source-cache.ts
@@ -9,9 +9,11 @@
// (the server's stream tickets last an hour). A URL that fails anyway is
// re-resolved by its player, which replaces the entry.
+import { LruCache } from '@/lib/lru-cache'
+
const PLAYBACK_TTL_MS = 20 * 60_000
-const playback = new Map<string, { at: number; src: string }>()
+const playback = new LruCache<string, { at: number; src: string }>(128)
export function cachedPlaybackSrc(key: string, now = Date.now()): string | undefined {
const entry = playback.get(key)
@@ -26,22 +28,14 @@ export function cachedPlaybackSrc(key: string, now = Date.now()): string | undef
}
export function rememberPlaybackSrc(key: string, src: string, now = Date.now()): void {
- playback.delete(key)
-
// A blob URL is revoked by the element that minted it; never hand one to another mount.
if (!src || src.startsWith('blob:')) {
+ playback.delete(key)
+
return
}
playback.set(key, { at: now, src })
-
- for (const [stale, entry] of playback) {
- if (now - entry.at < PLAYBACK_TTL_MS) {
- break
- }
-
- playback.delete(stale)
- }
}
export function forgetPlaybackSrc(key: string): void {
@@ -50,5 +44,7 @@ export function forgetPlaybackSrc(key: string): void {
/** Process-memory only; tests reset it between cases that reuse one path. */
export function clearPlaybackSourceCache(): void {
- playback.clear()
+ for (const key of [...playback.keys()]) {
+ playback.delete(key)
+ }
}