Skip to content

Commit e80b44e

Browse files
author
dostrelic
committed
Add workers binding example
1 parent 1eaab51 commit e80b44e

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/content/docs/browser-rendering/how-to/rate-limiting.mdx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ sidebar:
88
## Example: Handling 429 Rate Limits with Retry-After Header
99

1010
When you make too many requests in a short period of time, Browser Rendering will respond with HTTP status code `429 Too Many Requests`.
11-
The response includes a `Retry-After` header, which specifies how many seconds to wait before retrying. You can view your account's rate limits on the [Limits](/browser-rendering/platform/limits/) page.
11+
The response includes a `Retry-After` header, which specifies how many seconds to wait before retrying. You can view your account's rate limits on the [Limits](/browser-rendering/platform/limits/) page.
1212

1313
The example below demonstrates how to handle rate limiting gracefully by reading the `Retry-After` value and retrying the request after that delay.
1414

@@ -31,3 +31,30 @@ if (response.status === 429) {
3131
const retryResponse = await fetch(/* same request as above */);
3232
}
3333
```
34+
35+
## Workers Binding Example: Handling 429 Rate Limits with Retry-After Header
36+
37+
```js
38+
import puppeteer from "@cloudflare/puppeteer";
39+
40+
try {
41+
const browser = await puppeteer.launch(env.MYBROWSER);
42+
43+
const page = await browser.newPage();
44+
await page.goto("https://example.com");
45+
const content = await page.content();
46+
47+
await browser.close();
48+
} catch (error) {
49+
if (error.status === 429) {
50+
const retryAfter = error.headers.get("Retry-After");
51+
console.log(
52+
`Browser instance limit reached. Waiting ${retryAfter} seconds...`,
53+
);
54+
await new Promise((resolve) => setTimeout(resolve, retryAfter * 1000));
55+
56+
// Retry launching browser
57+
const browser = await puppeteer.launch(env.MYBROWSER);
58+
}
59+
}
60+
```

0 commit comments

Comments
 (0)