28 lines
779 B
JavaScript
28 lines
779 B
JavaScript
export const SERVER_HEALTH_MAX_COUNT = 30;
|
|
export const SERVER_HEALTH_CONCURRENCY = 4;
|
|
|
|
export async function checkServerHealth(servers, ping, {
|
|
maxCount = SERVER_HEALTH_MAX_COUNT,
|
|
concurrency = SERVER_HEALTH_CONCURRENCY,
|
|
} = {}) {
|
|
const queue = servers.slice(0, maxCount);
|
|
const results = new Array(queue.length);
|
|
let nextIndex = 0;
|
|
|
|
async function worker() {
|
|
while (nextIndex < queue.length) {
|
|
const index = nextIndex++;
|
|
const server = queue[index];
|
|
results[index] = {
|
|
id: server.id,
|
|
tag: server.label,
|
|
...await ping(server.host, server.port),
|
|
checkedAt: new Date().toISOString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
await Promise.all(Array.from({ length: Math.min(concurrency, queue.length) }, worker));
|
|
return results;
|
|
}
|