mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
Tighten renderer output back-pressure (128KB/32KB watermarks), account coalescer and write-queue backlog, drain output on user keystrokes, and add main-process IPC ACK flow control so SSH floods cannot starve Ctrl+C and other keyboard input. Closes #1711
135 lines
3.3 KiB
TypeScript
135 lines
3.3 KiB
TypeScript
import type { Terminal as XTerm } from "@xterm/xterm";
|
|
|
|
export const MAX_WRITE_QUEUE_ITEMS = 32;
|
|
export const MAX_WRITE_QUEUE_BYTES = 512 * 1024;
|
|
|
|
type QueuedWrite = {
|
|
bytes: number;
|
|
run: () => void;
|
|
};
|
|
|
|
type TerminalWriteQueue = {
|
|
writing: boolean;
|
|
pending: QueuedWrite[];
|
|
pendingBytes: number;
|
|
floodMode: boolean;
|
|
onDropped?: (bytes: number) => void;
|
|
};
|
|
|
|
const terminalWriteQueues = new WeakMap<XTerm, TerminalWriteQueue>();
|
|
|
|
const getOrCreateQueue = (term: XTerm): TerminalWriteQueue => {
|
|
let queue = terminalWriteQueues.get(term);
|
|
if (!queue) {
|
|
queue = {
|
|
writing: false,
|
|
pending: [],
|
|
pendingBytes: 0,
|
|
floodMode: false,
|
|
};
|
|
terminalWriteQueues.set(term, queue);
|
|
}
|
|
return queue;
|
|
};
|
|
|
|
const scheduleNextTerminalWrite = (term: XTerm, queue: TerminalWriteQueue) => {
|
|
const next = queue.pending.shift();
|
|
if (!next) {
|
|
queue.writing = false;
|
|
queue.floodMode = false;
|
|
terminalWriteQueues.delete(term);
|
|
return;
|
|
}
|
|
|
|
queue.pendingBytes -= next.bytes;
|
|
if (queue.pendingBytes < 0) queue.pendingBytes = 0;
|
|
queue.writing = true;
|
|
next.run();
|
|
};
|
|
|
|
const collapseFloodQueue = (queue: TerminalWriteQueue) => {
|
|
if (queue.pending.length <= 1) {
|
|
return;
|
|
}
|
|
let droppedBytes = 0;
|
|
const latest = queue.pending[queue.pending.length - 1]!;
|
|
for (let index = 0; index < queue.pending.length - 1; index += 1) {
|
|
droppedBytes += queue.pending[index]!.bytes;
|
|
}
|
|
queue.pending = [latest];
|
|
queue.pendingBytes = latest.bytes;
|
|
queue.floodMode = true;
|
|
if (droppedBytes > 0) {
|
|
queue.onDropped?.(droppedBytes);
|
|
}
|
|
};
|
|
|
|
export const setTerminalWriteQueueDropHandler = (
|
|
term: XTerm,
|
|
onDropped?: (bytes: number) => void,
|
|
): void => {
|
|
const queue = terminalWriteQueues.get(term);
|
|
if (queue) {
|
|
queue.onDropped = onDropped;
|
|
}
|
|
};
|
|
|
|
export const getTerminalWriteQueueDepth = (term: XTerm): number =>
|
|
terminalWriteQueues.get(term)?.pending.length ?? 0;
|
|
|
|
export const enqueueTerminalWrite = (
|
|
term: XTerm,
|
|
bytes: number,
|
|
write: (done: () => void) => void,
|
|
options: { onDropped?: (bytes: number) => void } = {},
|
|
): void => {
|
|
const queue = getOrCreateQueue(term);
|
|
if (options.onDropped) {
|
|
queue.onDropped = options.onDropped;
|
|
}
|
|
|
|
const shouldCollapse = queue.floodMode
|
|
|| queue.pending.length >= MAX_WRITE_QUEUE_ITEMS
|
|
|| queue.pendingBytes + bytes > MAX_WRITE_QUEUE_BYTES;
|
|
|
|
queue.pending.push({
|
|
bytes,
|
|
run: () => {
|
|
write(() => scheduleNextTerminalWrite(term, queue));
|
|
},
|
|
});
|
|
queue.pendingBytes += bytes;
|
|
|
|
if (shouldCollapse) {
|
|
collapseFloodQueue(queue);
|
|
}
|
|
|
|
if (!queue.writing) {
|
|
scheduleNextTerminalWrite(term, queue);
|
|
}
|
|
};
|
|
|
|
/** Drop queued output frames without writing them to xterm. */
|
|
export const abortTerminalWriteQueue = (
|
|
term: XTerm,
|
|
onDropped?: (bytes: number) => void,
|
|
): void => {
|
|
const queue = terminalWriteQueues.get(term);
|
|
if (!queue) return;
|
|
|
|
let droppedBytes = queue.pendingBytes;
|
|
if (queue.writing) {
|
|
// The in-flight write is not counted in pendingBytes once dequeued.
|
|
droppedBytes = queue.pending.reduce((sum, item) => sum + item.bytes, 0);
|
|
}
|
|
|
|
queue.pending = [];
|
|
queue.pendingBytes = 0;
|
|
queue.writing = false;
|
|
queue.floodMode = false;
|
|
terminalWriteQueues.delete(term);
|
|
|
|
if (droppedBytes > 0) {
|
|
onDropped?.(droppedBytes);
|
|
}
|
|
}; |