To hopefully save on system resources I want to run user requests through the same Chromium version in Puppeteer.
If a user submits a form on my site which calls Puppeteer, and Chromium is already running how can I use the same Chromium instance up to a maximum of 4 tabs.
If there are more than 4 tabs open in the Chromium instance I then want to launch a new Chromium instance.
How can I achieve this?? Would I need to store the browserWSEndpoint
of the Chromium instance to a file and then retrieve it every time a new user submits a request? (using browserWSEndpoint
with puppeteer.connect()
).
If I have to do it this way, lets say there's 2 chromium browsers active. The first most recent browser has the maximum four open tabs so I could not use this browser. I would then check the next browserWSEndpoint
and if there are less than 4 open tabs, create a new page. If not launch a new browser.
Does that sound OK??
答案 0 :(得分:0)
您可以使用Lambda来节省成本,如果要使用API网关,请确保避免30秒钟的Lambda超时。
https://github.com/alixaxel/chrome-aws-lambda
NodeJS
const chromium = require('chrome-aws-lambda');
exports.handler = async (event, context) => {
let result = null;
let browser = null;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
let page = await browser.newPage();
await page.goto(event.url || 'https://example.com');
result = await page.title();
} catch (error) {
return context.fail(error);
} finally {
if (browser !== null) {
await browser.close();
}
}
return context.succeed(result);
};