From ee1a82d7b6fa512abebeab6c2696c462b93d7034 Mon Sep 17 00:00:00 2001 From: Ncbudd Date: Sat, 28 Nov 2026 00:20:00 -0500 Subject: [PATCH] perf(availability): keep allowed titles for 10 minutes, denials for seconds Every Requests and Discover answer checked up to 32 titles against the viewer's media server again once the 30 s cache ran out, which was most of those pages' load time from a distant connection. A title the viewer can open is now kept 10 minutes; a denial or failed check stays 10 s, so newly shared or recovered access still shows within seconds. The cache holds up to 20,000 answers (4 MB) for a household's libraries. --- config/i18n/api-message-sources.json | 18 ++++----- server/lib/cache.ts | 5 ++- server/lib/mediaServerAvailability.test.ts | 44 ++++++++++++++++++++++ server/lib/mediaServerAvailability.ts | 9 ++++- 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/config/i18n/api-message-sources.json b/config/i18n/api-message-sources.json index 737b3cce46..e2363d9614 100644 --- a/config/i18n/api-message-sources.json +++ b/config/i18n/api-message-sources.json @@ -77969,7 +77969,7 @@ }, { "path": "server/lib/mediaServerAvailability.ts", - "line": 209, + "line": 214, "kind": "message-helper-expression", "field": "message", "expression": "media[format === '4k' ? 'status4k' : 'status']", @@ -77978,7 +77978,7 @@ }, { "path": "server/lib/mediaServerAvailability.ts", - "line": 457, + "line": 462, "kind": "message-helper-return", "field": "message", "expression": "false", @@ -77987,7 +77987,7 @@ }, { "path": "server/lib/mediaServerAvailability.ts", - "line": 465, + "line": 470, "kind": "message-helper-return", "field": "message", "expression": "true", @@ -77996,7 +77996,7 @@ }, { "path": "server/lib/mediaServerAvailability.ts", - "line": 486, + "line": 491, "kind": "message-helper-return", "field": "message", "expression": "hiddenAvailableSeason", @@ -78005,7 +78005,7 @@ }, { "path": "server/lib/mediaServerAvailability.ts", - "line": 554, + "line": 559, "kind": "message-helper-return", "field": "message", "expression": "false", @@ -78014,7 +78014,7 @@ }, { "path": "server/lib/mediaServerAvailability.ts", - "line": 561, + "line": 566, "kind": "message-helper-return", "field": "message", "expression": "false", @@ -78023,7 +78023,7 @@ }, { "path": "server/lib/mediaServerAvailability.ts", - "line": 564, + "line": 569, "kind": "message-helper-return", "field": "message", "expression": "false", @@ -78032,7 +78032,7 @@ }, { "path": "server/lib/mediaServerAvailability.ts", - "line": 572, + "line": 577, "kind": "message-helper-return", "field": "message", "expression": "true", @@ -78041,7 +78041,7 @@ }, { "path": "server/lib/mediaServerAvailability.ts", - "line": 594, + "line": 599, "kind": "message-helper-return", "field": "message", "expression": "hiddenAvailableSeason", diff --git a/server/lib/cache.ts b/server/lib/cache.ts index a81045c0e9..b40c0b0ca0 100644 --- a/server/lib/cache.ts +++ b/server/lib/cache.ts @@ -363,8 +363,9 @@ class CacheManager { 'Per-user media availability', { stdTtl: 30, - maxKeys: 5000, - maxBytes: 2 * 1024 * 1024, + // positive answers live 10 minutes now: room for a household's libraries + maxKeys: 20_000, + maxBytes: 4 * 1024 * 1024, } ), mdblist: new Cache('mdblist', 'MDBList API', { diff --git a/server/lib/mediaServerAvailability.test.ts b/server/lib/mediaServerAvailability.test.ts index ddb600ba63..f3c36bbb2e 100644 --- a/server/lib/mediaServerAvailability.test.ts +++ b/server/lib/mediaServerAvailability.test.ts @@ -18,6 +18,8 @@ import { applyUserMediaServerAvailability, getMediaServerAccessContext, getPresentationMediaServerAccessContext, + MEDIA_AVAILABILITY_CACHE_TTL_SECONDS, + MEDIA_AVAILABILITY_ERROR_TTL_SECONDS, restrictEpisodeAvailabilityForUser, } from './mediaServerAvailability'; @@ -68,6 +70,48 @@ afterEach(() => { }); describe('viewer-scoped media-server availability', () => { + it('keeps an allowed title for minutes and a denial only for seconds', async () => { + let allowed = true; + const lookup = mock.method(PlexAPI.prototype, 'getMetadata', async () => { + if (!allowed) throw new Error('Fixture access denied'); + return metadata(); + }); + const store = cacheManager.getCache('mediaavailability').data; + const ttls = () => + store + .keys() + .map((key) => + Math.round(((store.getTtl(key) ?? 0) - Date.now()) / 1000) + ); + + const first = media(); + await applyUserMediaServerAvailability([first], await context()); + assert.equal(first.status, MediaStatus.AVAILABLE); + assert.ok(MEDIA_AVAILABILITY_CACHE_TTL_SECONDS >= 600); + for (const ttl of ttls()) + assert.ok( + ttl > MEDIA_AVAILABILITY_CACHE_TTL_SECONDS - 5, + `allowed kept ${ttl}s` + ); + // the next page asks the cache, not the media server + const again = media(); + await applyUserMediaServerAvailability([again], await context()); + assert.equal(again.status, MediaStatus.AVAILABLE); + assert.equal(lookup.mock.callCount(), 1); + + store.flushAll(); + allowed = false; + const denied = media(); + await applyUserMediaServerAvailability([denied], await context()); + assert.equal(denied.status, MediaStatus.UNKNOWN); + assert.ok(MEDIA_AVAILABILITY_ERROR_TTL_SECONDS <= 10); + for (const ttl of ttls()) + assert.ok( + ttl <= MEDIA_AVAILABILITY_ERROR_TTL_SECONDS, + `denial kept ${ttl}s` + ); + }); + it('keeps the scanned library state visible to an administrator', async () => { const admin = await getRepository(User).findOneByOrFail({ id: 1 }); assert.equal( diff --git a/server/lib/mediaServerAvailability.ts b/server/lib/mediaServerAvailability.ts index c1069d38c9..699e783cf0 100644 --- a/server/lib/mediaServerAvailability.ts +++ b/server/lib/mediaServerAvailability.ts @@ -18,8 +18,13 @@ import { normalizeJellyfinGuid } from '@server/utils/jellyfin'; import { parsePositiveRouteId } from '@server/utils/routeId'; import { createHash } from 'node:crypto'; -const MEDIA_AVAILABILITY_CACHE_TTL_SECONDS = 30; -const MEDIA_AVAILABILITY_ERROR_TTL_SECONDS = 10; +// A title the viewer can open stays known for 10 minutes: libraries change rarely, and every +// Requests or Discover page otherwise checks the same titles against the media server again +// (up to 32 per answer), which is most of their load time from a distant connection. A +// denial or a failed check is kept only briefly, so newly shared or recovered access shows +// within seconds. +export const MEDIA_AVAILABILITY_CACHE_TTL_SECONDS = 10 * 60; +export const MEDIA_AVAILABILITY_ERROR_TTL_SECONDS = 10; const MAX_MEDIA_AVAILABILITY_CHECKS_PER_RESPONSE = 32; const MEDIA_AVAILABILITY_CHECK_CONCURRENCY = 8; const MAX_MEDIA_AVAILABILITY_IN_FLIGHT = 128;