Expire live caption state after inactivity and tune stream limits
This commit is contained in:
@@ -1,19 +1,36 @@
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
const { rateLimit } = require("express-rate-limit");
|
||||
|
||||
const sessionChecker = require("../middleware/sessionChecker.js");
|
||||
|
||||
const MAX_BUFFER_SIZE = 300;
|
||||
const DEFAULT_INITIAL_LIMIT = 40;
|
||||
const MAX_INITIAL_LIMIT = 120;
|
||||
const INACTIVITY_RESET_MS = 10 * 60 * 1000;
|
||||
const CAPTION_META_KEYS = new Set(["sequence", "createdAt", "original", "draft", "sourceLang", "lang", "isDraft", "status", "translations"]);
|
||||
|
||||
const liveCaptionState = {
|
||||
startedAt: Date.now(),
|
||||
lastIngestAt: 0,
|
||||
latestSequence: 0,
|
||||
captions: [],
|
||||
};
|
||||
|
||||
const liveCaptionsLimiter = rateLimit({
|
||||
windowMs: 10 * 60 * 1000,
|
||||
limit: 6000,
|
||||
standardHeaders: "draft-8",
|
||||
legacyHeaders: false,
|
||||
keyGenerator: (req) => {
|
||||
const forwarded = req.headers["x-forwarded-for"]?.split(",")[0];
|
||||
const ip = forwarded || req.ip || "";
|
||||
return ip.includes(":") ? ip.split(":")[0] : ip;
|
||||
},
|
||||
});
|
||||
|
||||
router.use(liveCaptionsLimiter);
|
||||
|
||||
const normalizeLang = (lang = "") => {
|
||||
const value = String(lang || "").trim().toLowerCase();
|
||||
if (!value) return "";
|
||||
@@ -82,8 +99,22 @@ const getAvailableLanguages = () => {
|
||||
return Array.from(langs).filter(Boolean).sort();
|
||||
};
|
||||
|
||||
const resetLiveCaptionState = () => {
|
||||
liveCaptionState.startedAt = Date.now();
|
||||
liveCaptionState.lastIngestAt = 0;
|
||||
liveCaptionState.latestSequence = 0;
|
||||
liveCaptionState.captions = [];
|
||||
};
|
||||
|
||||
const maybeResetForInactivity = () => {
|
||||
if (!liveCaptionState.lastIngestAt) return;
|
||||
if ((Date.now() - liveCaptionState.lastIngestAt) < INACTIVITY_RESET_MS) return;
|
||||
resetLiveCaptionState();
|
||||
};
|
||||
|
||||
router.get("/stream", sessionChecker, async (req, res) => {
|
||||
try {
|
||||
maybeResetForInactivity();
|
||||
const sinceSequence = Number.parseInt(req.query?.sinceSequence, 10);
|
||||
const requestedLimit = Number.parseInt(req.query?.limit, 10);
|
||||
const initialLimit = Number.isFinite(requestedLimit)
|
||||
@@ -150,6 +181,7 @@ router.post("/ingest", async (req, res) => {
|
||||
};
|
||||
|
||||
liveCaptionState.latestSequence = sequence;
|
||||
liveCaptionState.lastIngestAt = Date.now();
|
||||
liveCaptionState.captions.push(caption);
|
||||
if (liveCaptionState.captions.length > MAX_BUFFER_SIZE) {
|
||||
liveCaptionState.captions.splice(0, liveCaptionState.captions.length - MAX_BUFFER_SIZE);
|
||||
@@ -170,9 +202,7 @@ router.post("/ingest", async (req, res) => {
|
||||
router.post("/reset", async (_, res) => {
|
||||
try {
|
||||
// TODO: Add admin authorization before exposing this endpoint.
|
||||
liveCaptionState.startedAt = Date.now();
|
||||
liveCaptionState.latestSequence = 0;
|
||||
liveCaptionState.captions = [];
|
||||
resetLiveCaptionState();
|
||||
return res.json({ status: "ok" });
|
||||
} catch (error) {
|
||||
console.error("Error resetting live captions state", error);
|
||||
|
||||
Reference in New Issue
Block a user