refactor: re-use Puppteer Mutex (#2444)

This commit is contained in:
Nikolay Vitkov
2026-07-30 20:07:39 +02:00
committed by GitHub
parent 1937888386
commit 775ea2d3a3
5 changed files with 5 additions and 46 deletions
+2 -2
View File
@@ -18,7 +18,7 @@ import {labels, OFF_BY_DEFAULT_CATEGORIES} from './tools/categories.js';
import type {DefinedPageTool, ToolDefinition} from './tools/ToolDefinition.js';
import {pageIdSchema} from './tools/ToolDefinition.js';
import {logger} from './utils/logger.js';
import type {Mutex} from './utils/Mutex.js';
import type {Mutex} from './third_party/index.js';
export function buildFlag(category: ToolCategory) {
return `category${category.charAt(0).toUpperCase() + category.slice(1)}`;
@@ -310,7 +310,7 @@ export class ToolHandler {
success,
latencyMs: bucketizeLatency(Date.now() - startTime),
});
guard.dispose();
guard[Symbol.dispose]();
}
}
}
+1 -1
View File
@@ -24,7 +24,7 @@ import {ToolHandler} from './ToolHandler.js';
import type {DefinedPageTool, ToolDefinition} from './tools/ToolDefinition.js';
import {createTools} from './tools/tools.js';
import {logger} from './utils/logger.js';
import {Mutex} from './utils/Mutex.js';
import {Mutex} from './third_party/index.js';
import {VERSION} from './version.js';
export {buildFlag} from './ToolHandler.js';
+1 -1
View File
@@ -49,7 +49,7 @@ export type {CdpPage} from 'puppeteer-core/internal/cdp/Page.js';
export type {CdpWebWorker} from 'puppeteer-core/internal/cdp/WebWorker.js';
export type {Realm} from 'puppeteer-core/internal/api/Realm.js';
export type {JSONSchema7, JSONSchema7Definition} from 'json-schema';
export {Mutex} from 'puppeteer-core/internal/util/Mutex.js';
export {
resolveDefaultUserDataDir,
detectBrowserPlatform,
-41
View File
@@ -1,41 +0,0 @@
/**
* @license
* Copyright 2025 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
export class Mutex {
static Guard = class Guard {
#mutex: Mutex;
constructor(mutex: Mutex) {
this.#mutex = mutex;
}
dispose(): void {
return this.#mutex.release();
}
};
#locked = false;
#acquirers: Array<() => void> = [];
// This is FIFO.
async acquire(): Promise<InstanceType<typeof Mutex.Guard>> {
if (!this.#locked) {
this.#locked = true;
return new Mutex.Guard(this);
}
const {resolve, promise} = Promise.withResolvers<void>();
this.#acquirers.push(resolve);
await promise;
return new Mutex.Guard(this);
}
release(): void {
const resolve = this.#acquirers.shift();
if (!resolve) {
this.#locked = false;
return;
}
resolve();
}
}
+1 -1
View File
@@ -19,7 +19,7 @@ import type {
DefinedPageTool,
ToolDefinition,
} from '../src/tools/ToolDefinition.js';
import {Mutex} from '../src/utils/Mutex.js';
import {Mutex} from '../src/third_party/index.js';
describe('ToolHandler', () => {
afterEach(() => {