feat(transmission): optimize ssh connection pooling for black host

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
Natalie 2026-06-08 10:07:12 -07:00
parent 952072a265
commit a5507a790d

View file

@ -12,19 +12,35 @@ const BLACK_HOST = process.env["BLACK_SSH_HOST"] ?? "lilith@10.0.0.11";
const RPC_ENDPOINT = process.env["TRANSMISSION_RPC"] ?? "localhost:9091";
const TR = `transmission-remote ${RPC_ENDPOINT}`;
// Reuse one SSH connection across the 3s transfer polls (and add/remove). Without
// it every call paid a full cold handshake — fatal when black is I/O-loaded and
// each connect raced the timeout. Warm, torrent-get drops from ~25s to ~1.5s.
const SSH_OPTS = [
"-o", "ConnectTimeout=8",
"-o", "BatchMode=yes",
"-o", "ControlMaster=auto",
// Reuse one SSH connection across polls (and add/remove). Without it every call
// paid a full cold handshake — fatal when black is I/O-loaded and each connect
// raced the timeout. `ControlMaster=auto` on the per-call ssh did NOT survive,
// because the daemonized master dies with the short-lived poll process. So we
// open an EXPLICIT background master (-MNf) once and reuse its socket; it
// self-reaps via ControlPersist, so it never becomes a permanent orphan.
const CONTROL = [
"-o", "ControlPath=/tmp/tva-cm-%r@%h:%p",
"-o", "ControlPersist=120",
"-o", "ConnectTimeout=12",
"-o", "BatchMode=yes",
];
function ensureMaster(): void {
// Local check against the control socket — no network, instant when up.
const up = spawnSync("ssh", ["-O", "check", ...CONTROL, BLACK_HOST],
{ encoding: "utf8", timeout: 5_000 });
if (up.status === 0) return;
// -f daemonizes after auth (detaches from this process), -N runs no command.
// ControlPersist=600 reaps the master 10min after the last use.
spawnSync("ssh", ["-MNf", "-o", "ControlPersist=600", "-o", "ServerAliveInterval=30",
...CONTROL, BLACK_HOST], { encoding: "utf8", timeout: 20_000 });
}
function ssh(cmd: string): { ok: boolean; out: string } {
const r = spawnSync("ssh", [...SSH_OPTS, BLACK_HOST, cmd], { encoding: "utf8", timeout: 45_000 });
ensureMaster();
// ControlMaster=no: reuse the explicit master's socket; if it's somehow absent
// this still connects directly (just without multiplexing) rather than failing.
const r = spawnSync("ssh", ["-o", "ControlMaster=no", ...CONTROL, BLACK_HOST, cmd],
{ encoding: "utf8", timeout: 45_000 });
const out = ((r.stdout ?? "") + (r.stderr ?? "")).trim();
return { ok: r.status === 0, out };
}