Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions config/i18n/api-message-sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -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']",
Expand All @@ -77978,7 +77978,7 @@
},
{
"path": "server/lib/mediaServerAvailability.ts",
"line": 457,
"line": 462,
"kind": "message-helper-return",
"field": "message",
"expression": "false",
Expand All @@ -77987,7 +77987,7 @@
},
{
"path": "server/lib/mediaServerAvailability.ts",
"line": 465,
"line": 470,
"kind": "message-helper-return",
"field": "message",
"expression": "true",
Expand All @@ -77996,7 +77996,7 @@
},
{
"path": "server/lib/mediaServerAvailability.ts",
"line": 486,
"line": 491,
"kind": "message-helper-return",
"field": "message",
"expression": "hiddenAvailableSeason",
Expand All @@ -78005,7 +78005,7 @@
},
{
"path": "server/lib/mediaServerAvailability.ts",
"line": 554,
"line": 559,
"kind": "message-helper-return",
"field": "message",
"expression": "false",
Expand All @@ -78014,7 +78014,7 @@
},
{
"path": "server/lib/mediaServerAvailability.ts",
"line": 561,
"line": 566,
"kind": "message-helper-return",
"field": "message",
"expression": "false",
Expand All @@ -78023,7 +78023,7 @@
},
{
"path": "server/lib/mediaServerAvailability.ts",
"line": 564,
"line": 569,
"kind": "message-helper-return",
"field": "message",
"expression": "false",
Expand All @@ -78032,7 +78032,7 @@
},
{
"path": "server/lib/mediaServerAvailability.ts",
"line": 572,
"line": 577,
"kind": "message-helper-return",
"field": "message",
"expression": "true",
Expand All @@ -78041,7 +78041,7 @@
},
{
"path": "server/lib/mediaServerAvailability.ts",
"line": 594,
"line": 599,
"kind": "message-helper-return",
"field": "message",
"expression": "hiddenAvailableSeason",
Expand Down
5 changes: 3 additions & 2 deletions server/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
44 changes: 44 additions & 0 deletions server/lib/mediaServerAvailability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
applyUserMediaServerAvailability,
getMediaServerAccessContext,
getPresentationMediaServerAccessContext,
MEDIA_AVAILABILITY_CACHE_TTL_SECONDS,
MEDIA_AVAILABILITY_ERROR_TTL_SECONDS,
restrictEpisodeAvailabilityForUser,
} from './mediaServerAvailability';

Expand Down Expand Up @@ -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(
Expand Down
9 changes: 7 additions & 2 deletions server/lib/mediaServerAvailability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down