Expire live caption state after inactivity and tune stream limits

This commit is contained in:
Adolfo Reyna
2026-02-28 21:51:28 -05:00
parent 8aa1f3addd
commit 5195317c0c
2 changed files with 34 additions and 3 deletions

View File

@@ -29,6 +29,7 @@ const limiter = rateLimit({
limit: 500, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
standardHeaders: 'draft-8', // draft-6: `RateLimit-*` headers; draft-7 & draft-8: combined `RateLimit` header
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
skip: (req) => req.path.startsWith("/live-captions"),
keyGenerator: (req) => {
const forwarded = req.headers["x-forwarded-for"]?.split(",")[0]; // Take the first IP in the list
const ip = forwarded || req.ip; // Fallback to req.ip

View File

@@ -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);