mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-08 03:36:37 +00:00
Batch and delay for large amounts of targets
This commit is contained in:
@@ -4,21 +4,48 @@ import { Alias, SubnetProxyTarget } from "@server/lib/ip";
|
|||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
|
|
||||||
|
const BATCH_SIZE = 50;
|
||||||
|
const BATCH_DELAY_MS = 50;
|
||||||
|
|
||||||
|
function sleep(ms: number): Promise<void> {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
function chunkArray<T>(array: T[], size: number): T[][] {
|
||||||
|
const chunks: T[][] = [];
|
||||||
|
for (let i = 0; i < array.length; i += size) {
|
||||||
|
chunks.push(array.slice(i, i + size));
|
||||||
|
}
|
||||||
|
return chunks;
|
||||||
|
}
|
||||||
|
|
||||||
export async function addTargets(newtId: string, targets: SubnetProxyTarget[]) {
|
export async function addTargets(newtId: string, targets: SubnetProxyTarget[]) {
|
||||||
await sendToClient(newtId, {
|
const batches = chunkArray(targets, BATCH_SIZE);
|
||||||
type: `newt/wg/targets/add`,
|
for (let i = 0; i < batches.length; i++) {
|
||||||
data: targets
|
if (i > 0) {
|
||||||
});
|
await sleep(BATCH_DELAY_MS);
|
||||||
|
}
|
||||||
|
await sendToClient(newtId, {
|
||||||
|
type: `newt/wg/targets/add`,
|
||||||
|
data: batches[i]
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeTargets(
|
export async function removeTargets(
|
||||||
newtId: string,
|
newtId: string,
|
||||||
targets: SubnetProxyTarget[]
|
targets: SubnetProxyTarget[]
|
||||||
) {
|
) {
|
||||||
await sendToClient(newtId, {
|
const batches = chunkArray(targets, BATCH_SIZE);
|
||||||
type: `newt/wg/targets/remove`,
|
for (let i = 0; i < batches.length; i++) {
|
||||||
data: targets
|
if (i > 0) {
|
||||||
});
|
await sleep(BATCH_DELAY_MS);
|
||||||
|
}
|
||||||
|
await sendToClient(newtId, {
|
||||||
|
type: `newt/wg/targets/remove`,
|
||||||
|
data: batches[i]
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateTargets(
|
export async function updateTargets(
|
||||||
@@ -28,12 +55,24 @@ export async function updateTargets(
|
|||||||
newTargets: SubnetProxyTarget[];
|
newTargets: SubnetProxyTarget[];
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
await sendToClient(newtId, {
|
const oldBatches = chunkArray(targets.oldTargets, BATCH_SIZE);
|
||||||
type: `newt/wg/targets/update`,
|
const newBatches = chunkArray(targets.newTargets, BATCH_SIZE);
|
||||||
data: targets
|
const maxBatches = Math.max(oldBatches.length, newBatches.length);
|
||||||
}).catch((error) => {
|
|
||||||
logger.warn(`Error sending message:`, error);
|
for (let i = 0; i < maxBatches; i++) {
|
||||||
});
|
if (i > 0) {
|
||||||
|
await sleep(BATCH_DELAY_MS);
|
||||||
|
}
|
||||||
|
await sendToClient(newtId, {
|
||||||
|
type: `newt/wg/targets/update`,
|
||||||
|
data: {
|
||||||
|
oldTargets: oldBatches[i] || [],
|
||||||
|
newTargets: newBatches[i] || []
|
||||||
|
}
|
||||||
|
}).catch((error) => {
|
||||||
|
logger.warn(`Error sending message:`, error);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function addPeerData(
|
export async function addPeerData(
|
||||||
|
|||||||
Reference in New Issue
Block a user