perf(startup): skip inactive background services

This commit is contained in:
hungtrinh
2026-07-16 12:09:48 +07:00
parent 7dfb346667
commit 27b37705b3
8 changed files with 107 additions and 16 deletions
+44 -10
View File
@@ -14,7 +14,6 @@ import {
WATCHDOG_INTERVAL_MS, NETWORK_CHECK_INTERVAL_MS, VIRTUAL_IFACE_REGEX,
} from "@/lib/tunnel";
import { getMitmStatus, startMitm, loadEncryptedPassword, initDbHooks, restoreToolDNS, removeAllDNSEntriesSync } from "@/mitm/manager";
import { startQuotaAutoPing } from "@/shared/services/quotaAutoPing";
import { syncToJson as syncMitmAliasCache } from "@/lib/mitmAliasCache";
import { killAllBridges } from "@/lib/mcp/stdioSseBridge";
@@ -98,22 +97,32 @@ async function runHeavyStartup() {
safeRestartTailscale("startup").catch((e) => console.log("[InitApp] Tailscale resume failed:", e.message));
}
ensureCloudflared().catch(() => {});
if (settings.tunnelEnabled) ensureCloudflared().catch(() => {});
// Sync mitmAlias DB → JSON cache so standalone MITM server can read it
syncMitmAliasCache().catch(() => {});
if (settings.mitmEnabled) {
// Sync mitmAlias DB → JSON cache so standalone MITM server can read it.
syncMitmAliasCache().catch(() => {});
autoStartMitm(settings);
}
startWatchdog();
startNetworkMonitor();
autoStartMitm();
startQuotaAutoPing();
configureTunnelMonitoring(settings);
if (hasQuotaAutoPingEnabled(settings)) {
import("@/shared/services/quotaAutoPing")
.then(({ startQuotaAutoPing }) => startQuotaAutoPing())
.catch((e) => console.log("[AutoPing] scheduler start failed:", e.message));
}
}
async function autoStartMitm() {
function hasQuotaAutoPingEnabled(settings) {
return [settings?.claudeAutoPing, settings?.codexAutoPing]
.some((config) => Object.values(config?.connections || {}).some(Boolean));
}
async function autoStartMitm(settings) {
if (g.mitmStartInProgress) return;
g.mitmStartInProgress = true;
try {
const settings = await getSettings();
if (!settings.mitmEnabled) return;
const mitmStatus = await getMitmStatus();
if (mitmStatus.running) return;
@@ -232,6 +241,12 @@ function startWatchdog() {
if (g.watchdogInterval.unref) g.watchdogInterval.unref();
}
function stopWatchdog() {
if (!g.watchdogInterval) return;
clearInterval(g.watchdogInterval);
g.watchdogInterval = null;
}
// ─── Network monitor: detect IPv4 fingerprint change + sleep/wake ────────────
function getNetworkFingerprint() {
@@ -293,4 +308,23 @@ function startNetworkMonitor() {
if (g.networkMonitorInterval.unref) g.networkMonitorInterval.unref();
}
function stopNetworkMonitor() {
if (!g.networkMonitorInterval) return;
clearInterval(g.networkMonitorInterval);
g.networkMonitorInterval = null;
g.lastNetworkFingerprint = null;
g.lastOnline = null;
}
export function configureTunnelMonitoring(settings) {
if (settings?.tunnelEnabled || settings?.tailscaleEnabled) {
startWatchdog();
startNetworkMonitor();
return;
}
stopWatchdog();
stopNetworkMonitor();
}
export default initializeApp;
+15
View File
@@ -296,3 +296,18 @@ export function startQuotaAutoPing() {
g.interval = setInterval(() => { runQuotaAutoPingTick().catch(() => {}); }, C.tickIntervalMs);
if (g.interval.unref) g.interval.unref();
}
export function stopQuotaAutoPing() {
if (!g.interval) return;
clearInterval(g.interval);
g.interval = null;
console.log("[AutoPing] scheduler stopped");
}
export function configureQuotaAutoPing(settings) {
const enabled = Object.values(C.providers).some((providerConfig) =>
Object.values(settings?.[providerConfig.settingsKey]?.connections || {}).some(Boolean)
);
if (enabled) startQuotaAutoPing();
else stopQuotaAutoPing();
}