Skip to content

Commit

Permalink
feat: handle 429
Browse files Browse the repository at this point in the history
  • Loading branch information
iZemil committed Nov 29, 2022
1 parent 495cb98 commit 1076db7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
37 changes: 31 additions & 6 deletions src/config/runConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosError, AxiosResponse } from 'axios';
import chalk from 'chalk';
import * as fs from 'fs/promises';

import { delay } from '../utils';
import { wait } from '../utils';

import { LOG_PATH } from './consts';
import { openConfig } from './openConfig';
Expand All @@ -17,7 +17,17 @@ export function isAxiosError(error: any): error is AxiosError {

const log = console.log;

const handleRequest = async (url: string, num: number, total: number): Promise<AxiosResponse | null> => {
interface IHandleRequest {
url: string;
num: number;
total: number;
sleepDelay?: number;
sleepCount?: number;
}
const ONE_MINUTE = 60 * 1000;
const handleRequest = async (options: IHandleRequest): Promise<AxiosResponse | null> => {
const { url, num, total, sleepDelay = ONE_MINUTE, sleepCount = 0 } = options;

let response: AxiosResponse | null = null;
let isErroredRequest = false;

Expand All @@ -37,14 +47,29 @@ const handleRequest = async (url: string, num: number, total: number): Promise<A
);
}

// Handle: Too Many Requests
if (sleepCount > 3) {
throw new Error(`Exceeded ${sleepCount} requests`);
}
if (response && response.status === 429) {
log(chalk.grey(`sleep...`));

await wait(sleepDelay);

return handleRequest({
...options,
sleepDelay: sleepDelay + ONE_MINUTE,
sleepCount: sleepCount + 1,
});
}

return response;
};

const writeToLog = async (...strs: string[]) => {
await fs.appendFile(LOG_PATH, '\n' + strs.join('\n'));
};

// TODO: 429 status - sleep and retry
// TODO: run from last log element
export const runConfig = async (): Promise<void> => {
try {
Expand All @@ -58,11 +83,11 @@ export const runConfig = async (): Promise<void> => {
let index = 0;
for (const item of items) {
const num = (index += 1);

const url = query(baseUrl, item);
const response = await handleRequest(url, num, total);

try {
const response = await handleRequest({ url, num, total });

await writeToLog(
`\nurl: ${url}`,
`status: ${response?.status}`,
Expand All @@ -71,7 +96,7 @@ export const runConfig = async (): Promise<void> => {
} catch (error) {
console.error('handler error', error);
} finally {
await delay(config.delay);
await wait(config.delay);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export function checkPath(path: string, isDir = false): boolean {
}
}

export const delay = async (ms: number) => new Promise((res) => setTimeout(res, ms));
export const wait = async (ms: number) => new Promise((res) => setTimeout(res, ms));

0 comments on commit 1076db7

Please sign in to comment.