browser rendering

Trying to experiment with https://developers.cloudflare.com/browser-rendering/get-started/reuse-sessions/ have a paid plan and only 1 worker when across my account.. When i deploy locally i keep getting this error โœ˜ [ERROR] Uncaught (in response) Error: Unable to create new browser: code: 429: message: Too many browsers already running I understand what it means but dont understand why its is happening and hitting the browser limit...I am unable to even invoke the worker once it always keeps showing the same error
2 Replies
hob8yte
hob8yteโ€ข7mo ago
Cloudflare has some pretty strict limits about how many browsers you can spin up each minute, which is two. Fortunately you can reuse browser sessions. More on limits: https://developers.cloudflare.com/browser-rendering/platform/limits/ I'm working around this by attempting to connect to a random available session, or else spinning up a new browser.
import puppeteer, { type BrowserWorker } from "@cloudflare/puppeteer";

/**
* Cloudflare Browser Rendering has strict limits on the number of browsers that can be active, but does
* allow sharing sessions. Sessions are active for sixty seconds after the last command is made. New
* browsers can be created at any time, but only two are allowed a minute.
*
* This will try to connect to an available session if one is available, otherwise it will create a new
* browser. If no browser is available, it will throw an error.
*/
export async function getPuppeteerBrowser(browserWorker: BrowserWorker) {
const availableSession = await getAvailableSession(browserWorker);

if (availableSession) {
return await puppeteer.connect(browserWorker, availableSession.sessionId);
}

return await aquireBrowser(browserWorker);
}

async function getAvailableSession(browserWorker: BrowserWorker) {
const activeSessions = await puppeteer.sessions(browserWorker);
const availableSessions = activeSessions.filter(
(session) => !session.connectionId,
);

if (availableSessions.length === 0) {
return undefined;
}

return availableSessions[
Math.floor(Math.random() * availableSessions.length)
];
}

async function aquireBrowser(browserWorker: BrowserWorker) {
const activeLimits = await puppeteer.limits(browserWorker);

if (activeLimits.timeUntilNextAllowedBrowserAcquisition) {
throw new Error("No available browser aquisitions.");
}

return await puppeteer.launch(browserWorker);
}
import puppeteer, { type BrowserWorker } from "@cloudflare/puppeteer";

/**
* Cloudflare Browser Rendering has strict limits on the number of browsers that can be active, but does
* allow sharing sessions. Sessions are active for sixty seconds after the last command is made. New
* browsers can be created at any time, but only two are allowed a minute.
*
* This will try to connect to an available session if one is available, otherwise it will create a new
* browser. If no browser is available, it will throw an error.
*/
export async function getPuppeteerBrowser(browserWorker: BrowserWorker) {
const availableSession = await getAvailableSession(browserWorker);

if (availableSession) {
return await puppeteer.connect(browserWorker, availableSession.sessionId);
}

return await aquireBrowser(browserWorker);
}

async function getAvailableSession(browserWorker: BrowserWorker) {
const activeSessions = await puppeteer.sessions(browserWorker);
const availableSessions = activeSessions.filter(
(session) => !session.connectionId,
);

if (availableSessions.length === 0) {
return undefined;
}

return availableSessions[
Math.floor(Math.random() * availableSessions.length)
];
}

async function aquireBrowser(browserWorker: BrowserWorker) {
const activeLimits = await puppeteer.limits(browserWorker);

if (activeLimits.timeUntilNextAllowedBrowserAcquisition) {
throw new Error("No available browser aquisitions.");
}

return await puppeteer.launch(browserWorker);
}
Cloudflare Docs
Limits ยท Browser Rendering docs
Control and interact with a headless browser instance programmatically.
no1ne7576
no1ne7576OPโ€ข7mo ago
Right the example I am using seem to be using that https://developers.cloudflare.com/browser-rendering/get-started/reuse-sessions/ it's showing one active session in the dashboard and still getting that error too many browsers already running Tried your code, it worked thanks a lot ๐Ÿ™
Want results from more Discord servers?
Add your server