smarter max rate

This commit is contained in:
tresabhi committed 2026-08-13 10:29:11 -05:00
1 parent 1569de4475
commit a438eb704f
2 files changed
+58 -59

No files matched your search

+25 -57
View File
@@ -4,7 +4,7 @@ import { exit } from "node:process";
import { GitObjectStorage } from "../core/blitzkit/gitObjectStorage"; import { GitObjectStorage } from "../core/blitzkit/gitObjectStorage";
import { IdArray } from "../core/blitzkit/idArray"; import { IdArray } from "../core/blitzkit/idArray";
import { CHUNK_COUNT, MAX_DRY_STREAK, MAX_IDS_PER_CALL } from "./constants"; import { CHUNK_COUNT, MAX_DRY_STREAK, MAX_IDS_PER_CALL } from "./constants";
import { sleep, withinTimeLimit } from "./time"; import { atMaxRate } from "./time";
import { IdsManifest, QuickInfo, RegionDescriptor } from "./types"; import { IdsManifest, QuickInfo, RegionDescriptor } from "./types";
const storage = await new GitObjectStorage( const storage = await new GitObjectStorage(
@@ -30,18 +30,16 @@ for (let i = 0; i < manifest.chunks; i++) {
console.log(`Found ${manifest.chunks} pre-existing chunks`); console.log(`Found ${manifest.chunks} pre-existing chunks`);
manifest.chunks = CHUNK_COUNT;
let chunks: IdArray[] = []; let chunks: IdArray[] = [];
if (discoveredChunks.length === manifest.chunks) { if (manifest.chunks === CHUNK_COUNT) {
console.log("Pre-existing chunking is consistent, passing as-is..."); console.log("Pre-existing chunking is consistent, passing as-is...");
chunks = discoveredChunks as IdArray[]; chunks = discoveredChunks as IdArray[];
} else { } else {
console.log("Pre-existing chunking is not consistent, re-chunking..."); console.log("Pre-existing chunking is not consistent, re-chunking...");
chunks = Array.from({ length: manifest.chunks }).map(() => new IdArray()); chunks = Array.from({ length: CHUNK_COUNT }).map(() => new IdArray());
for (let i = 0; i < discoveredChunks.length; i++) { for (let i = 0; i < discoveredChunks.length; i++) {
const ids = discoveredChunks[i]; const ids = discoveredChunks[i];
@@ -49,7 +47,7 @@ if (discoveredChunks.length === manifest.chunks) {
for (let j = 0; j < size; j++) { for (let j = 0; j < size; j++) {
const id = ids!.get(j); const id = ids!.get(j);
const chunk = chunks[id % manifest.chunks]; const chunk = chunks[id % CHUNK_COUNT];
chunk.push(id); chunk.push(id);
} }
@@ -60,6 +58,9 @@ if (discoveredChunks.length === manifest.chunks) {
console.log("Sorting chunks..."); console.log("Sorting chunks...");
for (let i = 0; i < chunks.length; i++) chunks[i].sort(); for (let i = 0; i < chunks.length; i++) chunks[i].sort();
manifest.chunks = CHUNK_COUNT;
manifest.last_verified = manifest.last_verified % CHUNK_COUNT;
} }
const regions: RegionDescriptor[] = [ const regions: RegionDescriptor[] = [
@@ -116,7 +117,6 @@ async function save() {
manifest.total_count = totalFound; manifest.total_count = totalFound;
await storage.write("manifest.json", JSON.stringify(manifest, null, 2)); await storage.write("manifest.json", JSON.stringify(manifest, null, 2));
await storage.commit(); await storage.commit();
console.log( console.log(
@@ -129,15 +129,18 @@ async function save() {
process.on("SIGINT", save); process.on("SIGINT", save);
process.on("SIGTERM", save); process.on("SIGTERM", save);
const queue: { region: RegionDescriptor; url: string; size: number }[] = [];
const applicationId = import.meta.env.PUBLIC_WARGAMING_APPLICATION_ID; const applicationId = import.meta.env.PUBLIC_WARGAMING_APPLICATION_ID;
const fields = "account_id%2C-account_id"; const fields = "account_id%2C-account_id";
let regionI = 0; let regionI = 0;
function fillQueue() { console.log("Starting discovery loop...");
if (queue.length >= 1) return;
atMaxRate(async ({ _break: break1 }) => {
if (regions.length === 0) {
console.log("All regions exhausted, exiting discovery loop...");
return break1();
}
while (regions.length > 0) {
regionI = (regionI + 1) % regions.length; regionI = (regionI + 1) % regions.length;
const region = regions[regionI]; const region = regions[regionI];
@@ -146,7 +149,8 @@ function fillQueue() {
`Removing region ${region.domain} removed due to ${region.dry_streak} dry streak...`, `Removing region ${region.domain} removed due to ${region.dry_streak} dry streak...`,
); );
regions.splice(regionI, 1); regions.splice(regionI, 1);
continue;
return;
} }
const id0 = region.seed; const id0 = region.seed;
@@ -162,8 +166,7 @@ function fillQueue() {
fields fields
}&account_id=${accountIds}`; }&account_id=${accountIds}`;
queue.push({ region, url, size: idRange.length }); atMaxRate(async ({ _break: break2 }) => {
if (idRange.length < MAX_IDS_PER_CALL) { if (idRange.length < MAX_IDS_PER_CALL) {
console.log( console.log(
`Removing region ${region.domain} removed due to ID range exhaustion...`, `Removing region ${region.domain} removed due to ID range exhaustion...`,
@@ -171,31 +174,12 @@ function fillQueue() {
regions.splice(regionI, 1); regions.splice(regionI, 1);
} }
return; const response = await fetch(url);
}
}
console.log("Starting discovery loop...");
while (withinTimeLimit() && regions.length > 0) {
await sleep();
fillQueue();
if (queue.length === 0) {
console.log("Queue empty, ending discovery loop...");
break;
}
const request = queue.shift()!;
const response = await fetch(request.url);
const data = (await response.json()) as QuickInfo; const data = (await response.json()) as QuickInfo;
if (data.status !== "ok") { if (data.status !== "ok") {
console.warn("Failed API call; pushing back into queue..."); console.warn("Failed API call; trying again...");
queue.push(request); return;
continue;
} }
let foundIds = 0; let foundIds = 0;
@@ -215,30 +199,14 @@ while (withinTimeLimit() && regions.length > 0) {
} }
if (foundIds === 0) { if (foundIds === 0) {
request.region.dry_streak += request.size; region.dry_streak += idRange.length;
} else { } else {
request.region.dry_streak = 0; region.dry_streak = 0;
// console.log(`Found ${foundIds} ids in ${request.region.domain}`); // console.log(`Found ${foundIds} ids in ${request.region.domain}`);
} }
}
// console.log("Starting verification loop..."); break2();
});
// while (withinTimeLimit()) { });
// const verificationIndex = (manifest.last_verified + 1) % manifest.chunks;
// manifest.last_verified = verificationIndex;
// const chunk = chunks[verificationIndex];
// const size = chunk.size();
// let i0 = 0;
// while (withinTimeLimit() && i0 < size) {
// await sleep();
// const i1 = Math.min(i0 + MAX_IDS_PER_CALL - 1, size - 1);
// const indices = range(i0, i1 + 1);
// }
// console.log(`Verification complete for chunk ${verificationIndex}`);
// }
await save(); await save();
+33 -2
View File
@@ -9,6 +9,37 @@ export function withinTimeLimit() {
return performance.now() < t1; return performance.now() < t1;
} }
export function sleep() { export function sleep(t: number) {
return new Promise((resolve) => setTimeout(resolve, 1000 / API_RATE)); return new Promise((resolve) => setTimeout(resolve, t));
}
interface MethodContext {
_break(): void;
}
let lastT = 0;
const maxDelta = 1000 / API_RATE;
const DELTA_BUFFER = 0;
export async function atMaxRate(method: (context: MethodContext) => void) {
let isBroken = false;
const context: MethodContext = {
_break() {
isBroken = true;
},
};
while (!isBroken) {
if (!withinTimeLimit()) break;
const t = performance.now();
const delta = t - lastT;
if (delta < maxDelta) {
await sleep(maxDelta - delta + DELTA_BUFFER);
}
method(context);
lastT = t;
}
} }