Skip to content

Commit

Permalink
different rate limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrwt committed Aug 11, 2024
1 parent 0d9a0a9 commit de6858b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const index = async (input: string = process.argv[2], options: IndexOptio

for (const url of indexablePages) {
// wait 1 second between requests to avoid rate limiting
await new Promise((resolve) => setTimeout(resolve, 1000));
// await new Promise((resolve) => setTimeout(resolve, 1000));

console.log(`📄 Processing url: ${url}`);
const status = await getPublishMetadata(accessToken, url, {
Expand Down
9 changes: 9 additions & 0 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ export async function fetchRetry(url: string, options: RequestInit, retries: num
const body = await response.text();
throw new Error(`Server error code ${response.status}\n${body}`);
}

if (response.status === 429) {
console.log("Rate limited. Retrying in 5 seconds...");
// Retry after 5 seconds if rate limited
await new Promise((resolve) => setTimeout(resolve, 5000));

// Retry the request ignoring the retries limit
return fetchRetry(url, options, retries);
}
return response;
} catch (err) {
if (retries <= 0) {
Expand Down

0 comments on commit de6858b

Please sign in to comment.