mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-06 11:24:00 +00:00
fix: bound CCTV media upstream connection time
Adapt the response-header timeout contributed in public PR #53 to the CCTV provider.
This commit is contained in:
1 parent
3b19af4076
commit
c78ea4194c
1 file changed
+14
-5
@@ -216,9 +216,17 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
|
|||||||
};
|
};
|
||||||
const requestRange = req.headers?.range;
|
const requestRange = req.headers?.range;
|
||||||
if (requestRange) upstreamHeaders.Range = requestRange;
|
if (requestRange) upstreamHeaders.Range = requestRange;
|
||||||
const upstream = await fetch(mediaUrl, {
|
const controller = new AbortController();
|
||||||
headers: upstreamHeaders,
|
const timeoutId = setTimeout(() => controller.abort(), 15_000);
|
||||||
});
|
let upstream;
|
||||||
|
try {
|
||||||
|
upstream = await fetch(mediaUrl, {
|
||||||
|
headers: upstreamHeaders,
|
||||||
|
signal: controller.signal,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
const contentType = upstream.headers.get('content-type') || '';
|
const contentType = upstream.headers.get('content-type') || '';
|
||||||
if (!upstream.ok) {
|
if (!upstream.ok) {
|
||||||
setHealth(cameraId, {
|
setHealth(cameraId, {
|
||||||
@@ -270,17 +278,18 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
const timedOut = error?.name === 'AbortError' || error?.name === 'TimeoutError';
|
||||||
setHealth(cameraId, {
|
setHealth(cameraId, {
|
||||||
status: 'degraded',
|
status: 'degraded',
|
||||||
sourceKind: 'upstream',
|
sourceKind: 'upstream',
|
||||||
label: source?.provider || 'Configured source',
|
label: source?.provider || 'Configured source',
|
||||||
message: error?.message || 'Media fetch failed',
|
message: error?.message || 'Media fetch failed',
|
||||||
});
|
});
|
||||||
res.writeHead(502, {
|
res.writeHead(timedOut ? 504 : 502, {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Cache-Control': 'no-store',
|
'Cache-Control': 'no-store',
|
||||||
});
|
});
|
||||||
res.end(JSON.stringify({ error: 'Media proxy failed' }));
|
res.end(JSON.stringify({ error: timedOut ? 'Upstream media timeout' : 'Media proxy failed' }));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user