mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 02:02:21 +00:00
fix(cctv): keep a camera's health when an HLS viewer is refused for capacity
A full lease or session budget fell into the general catch and marked the camera degraded, although its upstream was fine and other viewers were watching it. Capacity refusals now throw HlsCapacityError and still answer 503 with Retry-After, without touching health. Tests: fill all 16 lease slots from one shared address and pin the refusal of a ninth viewer and a third session, keep a second lease alive so the released-lease 404 is the lease check, and share recordingResponse between the two middleware test files.
This commit is contained in:
1 parent
0699876e4e
commit
4e1f004a83
6 files changed
+93
-94
No files matched your search
@@ -18,7 +18,7 @@ import {
|
||||
import { sanitizeCctvRangeHeader } from './cctv/range.js';
|
||||
import { admitSameSiteKeyless } from './common/same-site.js';
|
||||
import { PROXY_SIGNALS } from '../../src/localRequestGate.mjs';
|
||||
import { createHlsPuller } from './cctv/stream.js';
|
||||
import { createHlsPuller, HlsCapacityError } from './cctv/stream.js';
|
||||
import { isMlitRiverAdjustmentImage } from './cctv/worldCameras.js';
|
||||
import { ottawaAuthorizedFrameUrl } from './cctv/ottawa.js';
|
||||
import { googleServerApiKey } from './places/google-key.js';
|
||||
@@ -449,13 +449,16 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
|
||||
'X-CCTV-Session': entry.token,
|
||||
});
|
||||
res.end(playlist);
|
||||
} catch {
|
||||
setHealth(cameraId, {
|
||||
status: 'degraded',
|
||||
sourceKind: 'fallback',
|
||||
label: source?.provider || 'Configured source',
|
||||
message: 'Live HLS unavailable',
|
||||
});
|
||||
} catch (error) {
|
||||
// A full lease or session budget says nothing about the source:
|
||||
// leave the health its other viewers see untouched.
|
||||
if (!(error instanceof HlsCapacityError))
|
||||
setHealth(cameraId, {
|
||||
status: 'degraded',
|
||||
sourceKind: 'fallback',
|
||||
label: source?.provider || 'Configured source',
|
||||
message: 'Live HLS unavailable',
|
||||
});
|
||||
if (!downstream.closed) {
|
||||
res.writeHead(503, {
|
||||
'Content-Type': 'application/json',
|
||||
|
||||
@@ -16,6 +16,9 @@ export const HLS_LIMITS = Object.freeze({
|
||||
readyMs: 12000,
|
||||
});
|
||||
|
||||
/** Every lease or session slot is taken: the source itself has not failed. */
|
||||
export class HlsCapacityError extends Error {}
|
||||
|
||||
/** Refuse redirects; count actual streamed bytes, including chunked responses. */
|
||||
export async function fetchHlsBytes(
|
||||
url,
|
||||
@@ -152,7 +155,7 @@ export function createHlsPuller({
|
||||
let lease = entry.leases.get(leaseId);
|
||||
if (!lease) {
|
||||
if (!create || entry.leases.size >= limits.leasesPerSession)
|
||||
throw new Error('HLS lease capacity reached');
|
||||
throw new HlsCapacityError('HLS lease capacity reached');
|
||||
lease = {};
|
||||
entry.leases.set(leaseId, lease);
|
||||
}
|
||||
@@ -264,7 +267,7 @@ export function createHlsPuller({
|
||||
}
|
||||
// Reserve synchronously, before any await: concurrent creation is singleflight.
|
||||
if (active.size >= limits.sessions)
|
||||
throw new Error('HLS session capacity reached');
|
||||
throw new HlsCapacityError('HLS session capacity reached');
|
||||
entry = {
|
||||
cameraId,
|
||||
url,
|
||||
|
||||
Reference in new issue
Block a user