diff --git a/config/i18n/api-message-sources.json b/config/i18n/api-message-sources.json index 737b3cce4..e2363d961 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 a81045c0e..b40c0b0ca 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 ddb600ba6..f3c36bbb2 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 c1069d38c..699e783cf 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;