Performance: RCON pooling, route caching, parallel status probe

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>
This commit is contained in:
hurkicorgi 2026-04-13 00:59:10 -06:00
parent b6b10159ad
commit b6cf8c7cdc
14 changed files with 220 additions and 82 deletions

View file

@ -3,48 +3,58 @@ import { status } from "minecraft-server-util";
import { execSync } from "child_process";
import { MC_SERVER_IP, MC_SERVER_PORT } from "@/lib/constants";
import { sendCommand } from "@/lib/rcon";
import { memoAsync } from "@/lib/cache";
export const dynamic = "force-dynamic";
export async function GET() {
// Tier 1: MC protocol ping (fastest, gives player/version info)
try {
const result = await status(MC_SERVER_IP, MC_SERVER_PORT, {
timeout: 5000,
});
return NextResponse.json({
online: true,
players: { online: result.players.online, max: result.players.max },
version: result.version.name,
motd: result.motd.clean,
});
} catch {}
type StatusResult = {
online: boolean;
starting?: boolean;
players: { online: number; max: number };
version?: string;
motd?: string;
};
// Tier 2: RCON (server is online but protocol ping failed)
try {
const response = await sendCommand("list");
async function probeStatus(): Promise<StatusResult> {
// Race protocol ping + RCON in parallel — whichever wins first signals "online"
const ping = status(MC_SERVER_IP, MC_SERVER_PORT, { timeout: 3000 }).then(
(r): StatusResult => ({
online: true,
players: { online: r.players.online, max: r.players.max },
version: r.version.name,
motd: r.motd.clean,
})
);
const rcon = sendCommand("list").then((response): StatusResult => {
const match = response.match(/There are (\d+) of a max of (\d+) players/);
return NextResponse.json({
return {
online: true,
players: {
online: match ? parseInt(match[1], 10) : 0,
max: match ? parseInt(match[2], 10) : 20,
},
});
} catch {}
};
});
// Tier 3: systemctl (process alive but not yet accepting connections)
let starting = false;
try {
const out = execSync("systemctl is-active minecraft.service", {
encoding: "utf8",
}).trim();
starting = out === "active" || out === "activating";
} catch {}
return await Promise.any([ping, rcon]);
} catch {
// Both failed — check if process is up
let starting = false;
try {
const out = execSync("systemctl is-active minecraft.service", {
encoding: "utf8",
}).trim();
starting = out === "active" || out === "activating";
} catch {}
return { online: false, starting, players: { online: 0, max: 0 } };
}
}
return NextResponse.json({
online: false,
starting,
players: { online: 0, max: 0 },
export async function GET() {
const result = await memoAsync("status", 3000, probeStatus);
return NextResponse.json(result, {
headers: { "Cache-Control": "public, max-age=3" },
});
}