Browser Rendering Error: 429

Hey, I have a simple browser-rendering API worker (code below). But I frequently get this error: Error: Unabled to create new browser: code: 429: message: Too many browsers already running It happens locally when testing with —remote but also in prod. I had to switch my backend to another service as my tool went viral this weekend. My code to get html for a url:
import puppeteer from "@cloudflare/puppeteer";

let browser;
export default {
async fetch(request, env, ctx) {
const { searchParams } = new URL(request.url);
let url = searchParams.get("url");
if (!url) {
return new Response("Missing url parameter", { status: 400 });
}
url = new URL(url).toString(); // normalize
console.log(`Fetching ${url}`);
const browser = await puppeteer.launch(env.MYBROWSER);
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle0' });
const data = await page.content();
await browser.close();
// send as txt
return new Response(data, {
headers: {
"content-type": "text/plain",
},
});

},
};
import puppeteer from "@cloudflare/puppeteer";

let browser;
export default {
async fetch(request, env, ctx) {
const { searchParams } = new URL(request.url);
let url = searchParams.get("url");
if (!url) {
return new Response("Missing url parameter", { status: 400 });
}
url = new URL(url).toString(); // normalize
console.log(`Fetching ${url}`);
const browser = await puppeteer.launch(env.MYBROWSER);
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle0' });
const data = await page.content();
await browser.close();
// send as txt
return new Response(data, {
headers: {
"content-type": "text/plain",
},
});

},
};
5 Replies
Chaika
Chaika8mo ago
You'll want to use Sessions https://developers.cloudflare.com/browser-rendering/get-started/reuse-sessions/ or Durable Objects directly: https://developers.cloudflare.com/browser-rendering/get-started/browser-rendering-with-do/ to reuse browser instances https://developers.cloudflare.com/browser-rendering/platform/limits/ You only get
Two new browsers per minute per account. Two concurrent browsers per account. A browser instance gets killed if it does not get any command for 60 seconds, freeing one instance. browser.close() or disconnecting from the API WebSocket releases the browser instance.
asad
asadOP8mo ago
thanks. is there a limit to how many pages i can open if i wrap this in a durable object.
Chaika
Chaika8mo ago
only in the sense that a DO can only handle so many connections/a browser can only do so much Work. You'll hit a memory limit if you don't dispose your resources as well: https://discord.com/channels/595317990191398933/1110268932788588624/1229381952801345576
asad
asadOP8mo ago
hmmm 🤔 that makes this a bit awkward to use. I guess my assumption was that I don’t need to worry about putting any load on the API 😅 In comparison, I fell back to using browserless.io where I don’t need to manage browser instance myself… i just remote connect to one.
Chaika
Chaika8mo ago
I agree, I think it really should be as simple as just calling an endpoint to get a page and not having to worry about the backing session/browser or anything like that. Sadly, it is how it is right now
Want results from more Discord servers?
Add your server