Refactor server picker and limit ping requests
All checks were successful
Build and Deploy Gateway / build-and-push (push) Successful in 15s
Build and Deploy Gateway / deploy (push) Successful in 7s

This commit is contained in:
2026-07-12 13:42:22 +03:00
parent 24fda3e34e
commit 57330f1c78
10 changed files with 612 additions and 83 deletions

View File

@@ -16,6 +16,7 @@ import {
} from './gatewayPresence.js';
import { createSingboxRuntime } from './singboxRuntime.js';
import { tcpPing } from './ping.js';
import { checkServerHealth } from './serverHealth.js';
import { buildSharedProxyInfo } from './sharedProxy.js';
import {
buildGatewayConfig,
@@ -577,12 +578,12 @@ async function handleApi(req, res) {
if (req.method === 'POST' && req.url === '/api/servers/ping-all') {
const state = stateStore.read();
const results = await Promise.all((state.servers || []).map(async (server) => ({
id: server.id,
tag: server.label,
...await tcpPing(server.host, server.port),
checkedAt: new Date().toISOString(),
})));
const { serverIds = [] } = await readBody(req);
const requestedIds = new Set(Array.isArray(serverIds) ? serverIds.map(String) : []);
const servers = requestedIds.size
? (state.servers || []).filter((server) => requestedIds.has(server.id))
: state.servers || [];
const results = await checkServerHealth(servers, tcpPing);
return sendState(res, { results });
}