feat: multi-browser pooling (2 Chromium instances × 8 pages)

- Launch BROWSER_COUNT separate Chromium instances (default: 2)
- Each with PAGES_PER_BROWSER pages (default: 8, 16 total)
- Round-robin distribution across browser instances
- Independent restart scheduling per browser
- Updated health endpoint to show per-browser stats
- docker-compose: added BROWSER_COUNT and PAGES_PER_BROWSER env vars
This commit is contained in:
OpenClaw 2026-02-14 21:55:29 +00:00
parent a177020186
commit efa39661cf
6 changed files with 231 additions and 47 deletions

View file

@ -11,34 +11,27 @@ function getMonthKey(): string {
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`;
}
// Load usage data from file on startup
async function loadUsageData(): Promise<void> {
try {
const data = await fs.readFile(USAGE_FILE, "utf8");
const usageObj = JSON.parse(data);
usage = new Map();
for (const [key, record] of Object.entries(usageObj)) {
usage.set(key, record as { count: number; monthKey: string });
}
console.log(`Loaded usage data for ${usage.size} keys`);
} catch (error) {
// File doesn't exist or invalid JSON - start fresh
console.log("No existing usage data found, starting fresh");
usage = new Map();
}
}
// Save usage data to file
async function saveUsageData(): Promise<void> {
try {
const usageObj: Record<string, { count: number; monthKey: string }> = {};
for (const [key, record] of usage) {
usageObj[key] = record;
}
// Ensure directory exists
await fs.mkdir(path.dirname(USAGE_FILE), { recursive: true });
await fs.writeFile(USAGE_FILE, JSON.stringify(usageObj, null, 2));
} catch (error) {
@ -46,21 +39,20 @@ async function saveUsageData(): Promise<void> {
}
}
// Initialize usage data loading
loadUsageData().catch(console.error);
export function usageMiddleware(req: any, res: any, next: any): void {
const key = req.headers.authorization?.slice(7) || "unknown";
// Use apiKeyInfo attached by auth middleware (works for both Bearer and X-API-Key)
const keyInfo = req.apiKeyInfo;
const key = keyInfo?.key || "unknown";
const monthKey = getMonthKey();
// Pro keys have no limit
if (isProKey(key)) {
trackUsage(key, monthKey);
next();
return;
}
// Free tier limit check
const record = usage.get(key);
if (record && record.monthKey === monthKey && record.count >= FREE_TIER_LIMIT) {
res.status(429).json({
@ -83,8 +75,6 @@ function trackUsage(key: string, monthKey: string): void {
} else {
record.count++;
}
// Save to file after each update (simple approach)
saveUsageData().catch(console.error);
}