fix(transmission): 🐛 optimize ssh connection warmup

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
Natalie 2026-06-08 09:03:36 -07:00
parent 8d28a8a988
commit 952072a265

View file

@ -4,14 +4,27 @@
import { spawnSync } from "node:child_process";
// Host + transmission RPC endpoint are configurable; defaults preserve the
// original hardcoded topology so nothing breaks when the env is unset.
const BLACK_HOST = process.env["BLACK_SSH_HOST"] ?? "lilith@10.9.0.4";
// Host + transmission RPC endpoint are configurable. Default to black's LAN
// address (10.0.0.11): the mesh/overlay (10.9.0.4) needs a ~5s handshake per
// connection and flaps, so a fresh-cold poll there routinely blew past the
// timeout. Override BLACK_SSH_HOST when off-LAN (the whole media stack is anyway).
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",
"-o", "ControlPath=/tmp/tva-cm-%r@%h:%p",
"-o", "ControlPersist=120",
];
function ssh(cmd: string): { ok: boolean; out: string } {
const r = spawnSync("ssh", [BLACK_HOST, cmd], { encoding: "utf8", timeout: 30_000 });
const r = spawnSync("ssh", [...SSH_OPTS, BLACK_HOST, cmd], { encoding: "utf8", timeout: 45_000 });
const out = ((r.stdout ?? "") + (r.stderr ?? "")).trim();
return { ok: r.status === 0, out };
}