reduces wait time window from 2min to 1min

This commit is contained in:
nai-degen 2023-05-11 11:45:07 -05:00
parent 8ff43aef1a
commit 09184079af
2 changed files with 17 additions and 22 deletions

View File

@ -4,7 +4,7 @@ import showdown from "showdown";
import { config, listConfig } from "./config";
import { keyPool } from "./key-management";
import { getUniqueIps } from "./proxy/rate-limit";
import { getAverageWaitTime, getQueueLength } from "./proxy/queue";
import { getEstimatedWaitTime, getQueueLength } from "./proxy/queue";
const INFO_PAGE_TTL = 5000;
let infoPageHtml: string | undefined;
@ -142,17 +142,20 @@ ${customGreeting}`;
return converter.makeHtml(infoBody);
}
/** Returns queue time in seconds, or minutes + seconds if over 60 seconds. */
function getQueueInformation() {
if (config.queueMode === "none") {
return {};
}
const waitMs = getAverageWaitTime();
const waitMs = getEstimatedWaitTime();
const waitTime =
waitMs < 60000
? `${Math.round(waitMs / 1000)} seconds`
: `${Math.round(waitMs / 60000)} minutes`;
? `${Math.round(waitMs / 1000)}sec`
: `${Math.round(waitMs / 60000)}min, ${Math.round(
(waitMs % 60000) / 1000
)}sec`;
return {
proomptersWaiting: getQueueLength(),
estimatedWaitTime: waitMs > 3000 ? waitTime : "no wait",
estimatedWaitTime: waitMs > 1000 ? waitTime : "no wait",
};
}

View File

@ -71,7 +71,7 @@ export function enqueue(req: Request) {
req.res!.write(": queue heartbeat\n\n");
} else {
req.log.info(`Sending heartbeat to request in queue.`);
const avgWait = Math.round(getAverageWaitTime() / 1000);
const avgWait = Math.round(getEstimatedWaitTime() / 1000);
const currentDuration = Math.round((Date.now() - req.startTime) / 1000);
const debugMsg = `queue length: ${queue.length}; elapsed time: ${currentDuration}s; avg wait: ${avgWait}s`;
req.res!.write(buildFakeSseMessage("heartbeat", debugMsg));
@ -188,7 +188,7 @@ function cleanQueue() {
});
const index = waitTimes.findIndex(
(waitTime) => now - waitTime.end > 120 * 1000
(waitTime) => now - waitTime.end > 60 * 1000
);
const removed = waitTimes.splice(0, index + 1);
log.info(
@ -204,7 +204,7 @@ export function start() {
log.info(`Started request queue.`);
}
const waitTimes: { start: number; end: number }[] = [];
let waitTimes: { start: number; end: number }[] = [];
/** Adds a successful request to the list of wait times. */
export function trackWaitTime(req: Request) {
@ -215,24 +215,16 @@ export function trackWaitTime(req: Request) {
}
/** Returns average wait time in milliseconds. */
export function getAverageWaitTime() {
if (waitTimes.length === 0) {
export function getEstimatedWaitTime() {
const now = Date.now();
const lastMinute = waitTimes.filter((wt) => now - wt.end < 60 * 1000);
if (lastMinute.length === 0) {
return 0;
}
// Include requests that are still in the queue right now
const now = Date.now();
const waitTimesWithCurrent = [
...waitTimes,
...queue.map((req) => ({
start: req.startTime!,
end: now,
})),
];
return (
waitTimesWithCurrent.reduce((acc, curr) => acc + curr.end - curr.start, 0) /
waitTimesWithCurrent.length
lastMinute.reduce((sum, wt) => sum + wt.end - wt.start, 0) /
lastMinute.length
);
}