Retry S3 GetObject on first-request failures, return 502 for stream errors
This commit is contained in:
@@ -382,8 +382,23 @@ export const videoRoutes = new Elysia({ prefix: "/api/videos" })
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error("Stream error:", err);
|
const msg = err?.message || String(err);
|
||||||
return status(404, { error: "Video not found" });
|
console.error(
|
||||||
|
"Stream error for video",
|
||||||
|
id,
|
||||||
|
"key",
|
||||||
|
video.transcodedKey ?? video.videoKey,
|
||||||
|
":",
|
||||||
|
msg,
|
||||||
|
);
|
||||||
|
// S3 errors are server errors, not "not found"
|
||||||
|
const statusCode = msg.includes("NoSuchKey") ? 404 : 502;
|
||||||
|
return status(statusCode, {
|
||||||
|
error:
|
||||||
|
statusCode === 404
|
||||||
|
? "Video not found"
|
||||||
|
: "Failed to stream video",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -175,7 +175,25 @@ export async function getVideoStream(
|
|||||||
...(rangeHeader ? { Range: rangeHeader } : {}),
|
...(rangeHeader ? { Range: rangeHeader } : {}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await s3.send(command);
|
// Retry once on transient failures (cold S3 connections can fail the first request)
|
||||||
|
let result;
|
||||||
|
try {
|
||||||
|
result = await s3.send(command);
|
||||||
|
} catch (firstError: any) {
|
||||||
|
console.warn(
|
||||||
|
`[stream] First attempt failed for key=${key}:`,
|
||||||
|
firstError?.message || firstError,
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
result = await s3.send(command);
|
||||||
|
} catch (secondError: any) {
|
||||||
|
console.error(
|
||||||
|
`[stream] Second attempt also failed for key=${key}:`,
|
||||||
|
secondError?.message || secondError,
|
||||||
|
);
|
||||||
|
throw secondError;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!result.Body) {
|
if (!result.Body) {
|
||||||
throw new Error("Empty response body from RustFS");
|
throw new Error("Empty response body from RustFS");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user