Skip to content

Commit 9a09344

Browse files
committed
Add /pressure endpoint, bump maxTabs to 20, expose pool stats
- GET /pressure returns activeTabs, pendingTabs, capacity per browser - Default maxTabs bumped from 2 to 20 (configurable via MAX_TABS_PER_BROWSER) - ChromeInstanceStats now includes pendingTabs and maxTabs - ReaderClient.getPoolStats() exposes pool stats for daemon use
1 parent 95f11ad commit 9a09344

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/browser/playwright-pool.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export interface ChromeInstanceStats {
7373
proxyUrl: string | null;
7474
state: "launching" | "active" | "retired" | "closed";
7575
activeTabs: number;
76+
pendingTabs: number;
77+
maxTabs: number;
7678
totalPages: number;
7779
}
7880

@@ -125,7 +127,7 @@ export class ChromeInstance {
125127
logger?: Logger;
126128
}) {
127129
this.proxyUrl = options.proxyUrl;
128-
this.maxTabs = options.maxTabs ?? 2;
130+
this.maxTabs = options.maxTabs ?? 20;
129131
this.retireAfterPages = options.retireAfterPages ?? 100;
130132
this.showChrome = options.showChrome ?? false;
131133
this.userAgent = options.userAgent;
@@ -158,6 +160,8 @@ export class ChromeInstance {
158160
proxyUrl: this.proxyUrl,
159161
state: this.state,
160162
activeTabs: this.limit.activeCount,
163+
pendingTabs: this.limit.pendingCount,
164+
maxTabs: this.maxTabs,
161165
totalPages: this.totalPages,
162166
};
163167
}

src/client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export class ReaderClient {
247247

248248
this.pool = new PlaywrightPool({
249249
tiers: tierConfigs,
250-
maxTabsPerBrowser: 10,
250+
maxTabsPerBrowser: parseInt(process.env.MAX_TABS_PER_BROWSER || "20", 10),
251251
retireAfterPages: this.options.browserPool?.retireAfterPages ?? 100,
252252
showChrome: this.options.showChrome,
253253
userAgent: this.options.userAgent,
@@ -426,6 +426,10 @@ export class ReaderClient {
426426
return this.initialized && !this.closed;
427427
}
428428

429+
getPoolStats(): import("./browser/playwright-pool.js").PoolStatsSnapshot | null {
430+
return this.pool?.getStats() ?? null;
431+
}
432+
429433
/**
430434
* Close the client and release resources
431435
*

src/daemon/server.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ export class DaemonServer {
318318
return;
319319
}
320320

321+
// Pressure: per-browser load metrics for smart routing
322+
if (method === "GET" && url === "/pressure") {
323+
if (!this.checkAuth(req, res)) return;
324+
this.handlePressure(res);
325+
return;
326+
}
327+
321328
// --- POST / (existing action-based RPC) ---
322329

323330
if (method !== "POST" || url !== "/") {
@@ -486,6 +493,34 @@ export class DaemonServer {
486493
this.sendResponse<DaemonStatus>(res, 200, { success: true, data: status });
487494
}
488495

496+
private handlePressure(res: http.ServerResponse): void {
497+
const poolStats = this.client?.getPoolStats();
498+
let activeTabs = 0;
499+
let pendingTabs = 0;
500+
let capacity = 0;
501+
502+
const tiers =
503+
poolStats?.tiers.map((t) => {
504+
const browsers = t.browsers.map((b) => {
505+
activeTabs += b.activeTabs;
506+
pendingTabs += b.pendingTabs;
507+
if (b.state === "active") capacity += b.maxTabs;
508+
return {
509+
activeTabs: b.activeTabs,
510+
pendingTabs: b.pendingTabs,
511+
maxTabs: b.maxTabs,
512+
state: b.state,
513+
};
514+
});
515+
return { tier: t.tier, browsers };
516+
}) ?? [];
517+
518+
this.sendResponse(res, 200, {
519+
success: true,
520+
data: { activeTabs, pendingTabs, capacity, activeRequests: this.activeRequests, tiers },
521+
});
522+
}
523+
489524
/**
490525
* Handle shutdown request
491526
*/

0 commit comments

Comments
 (0)