Server: - lib/cache.ts: tiny TTL memo for hot paths. - lib/rcon.ts: pooled connection with 30s idle close and one-shot retry; was opening a fresh TCP per RCON call. - /api/status: race protocol-ping + RCON with Promise.any (was sequential with 5s timeouts) and memo 3s; adds Cache-Control. - /api/players: memo 10s, invalidated on mutations. - /api/mods: getModDetails memo 10s (invalidated on install/remove) — eliminates per-request readdirSync/statSync/AdmZip parse. - /api/analytics: reverse-scan JSONL to window cutoff (O(window) vs O(file)) and memo 30s. - /api/logs: memo 2s to throttle journalctl spawns during Live mode. Client: - StatusCard + ServerControls: staleTime 5s, both poll every 10s, dedup via shared query key. - Analytics: staleTime 30s; memoize sparkline SVG paths. - Modrinth search icons + mc-heads avatars: width/height + loading=lazy + decoding=async. - next.config.ts: images.remotePatterns for future next/image migration, explicit compress=true. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { Rcon } from "rcon-client";
|
|
import { MC_SERVER_IP, RCON_PORT, RCON_PASSWORD } from "./constants";
|
|
|
|
let pooled: Rcon | null = null;
|
|
let connecting: Promise<Rcon> | null = null;
|
|
let idleTimer: NodeJS.Timeout | null = null;
|
|
const IDLE_MS = 30_000;
|
|
|
|
async function open(): Promise<Rcon> {
|
|
const rcon = await Rcon.connect({
|
|
host: MC_SERVER_IP,
|
|
port: RCON_PORT,
|
|
password: RCON_PASSWORD,
|
|
timeout: 5000,
|
|
});
|
|
rcon.on("end", () => {
|
|
if (pooled === rcon) pooled = null;
|
|
});
|
|
rcon.on("error", () => {
|
|
if (pooled === rcon) pooled = null;
|
|
});
|
|
return rcon;
|
|
}
|
|
|
|
function scheduleIdleClose() {
|
|
if (idleTimer) clearTimeout(idleTimer);
|
|
idleTimer = setTimeout(() => {
|
|
const r = pooled;
|
|
pooled = null;
|
|
idleTimer = null;
|
|
r?.end().catch(() => {});
|
|
}, IDLE_MS);
|
|
}
|
|
|
|
async function getConnection(): Promise<Rcon> {
|
|
if (pooled) return pooled;
|
|
if (connecting) return connecting;
|
|
connecting = open()
|
|
.then((r) => {
|
|
pooled = r;
|
|
return r;
|
|
})
|
|
.finally(() => {
|
|
connecting = null;
|
|
});
|
|
return connecting;
|
|
}
|
|
|
|
export async function sendCommand(command: string): Promise<string> {
|
|
try {
|
|
const rcon = await getConnection();
|
|
const response = await rcon.send(command);
|
|
scheduleIdleClose();
|
|
return response;
|
|
} catch (e) {
|
|
// Drop the pooled connection on any error, retry once with fresh
|
|
const bad = pooled;
|
|
pooled = null;
|
|
bad?.end().catch(() => {});
|
|
const rcon = await getConnection();
|
|
const response = await rcon.send(command);
|
|
scheduleIdleClose();
|
|
return response;
|
|
}
|
|
}
|