Created
September 27, 2023 14:21
-
-
Save AlexcastroDev/c732f7f041b02f18e4cbcdada89d3b5c to your computer and use it in GitHub Desktop.
retry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async requestWithRetry( | |
query: string, | |
username: string, | |
retryDelay: number = 1000, // Set the delay between retries (in milliseconds) | |
) { | |
const tokens = [ | |
Deno.env.get("GITHUB_TOKEN1"), | |
Deno.env.get("GITHUB_TOKEN2"), | |
]; | |
const maxRetries = tokens.length | |
const variables = { username: username }; | |
let response; | |
for (let attempt = 1; attempt <= maxRetries; attempt++) { | |
try { | |
response = await soxa.post( | |
this.apiEndpoint, | |
{}, | |
{ | |
data: { query: query, variables }, | |
headers: { Authorization: `bearer ${tokens[0]}` }, | |
}, | |
); | |
if (response.data.data !== undefined) { | |
return response.data.data.user; | |
} | |
} catch (error) { | |
console.error(`Attempt ${attempt} failed with token ${tokens[0]}:`, error); | |
} | |
if (attempt < maxRetries) { | |
console.log(`Retrying in ${retryDelay / 1000} seconds...`); | |
await new Promise((resolve) => setTimeout(resolve, retryDelay)); | |
} | |
try { | |
response = await soxa.post( | |
this.apiEndpoint, | |
{}, | |
{ | |
data: { query: query, variables }, | |
headers: { Authorization: `bearer ${tokens[1]}` }, | |
}, | |
); | |
if (response.data.data !== undefined) { | |
return response.data.data.user; | |
} | |
} catch (error) { | |
console.error(`Attempt ${attempt} failed with token ${tokens[1]}:`, error); | |
} | |
if (attempt < maxRetries) { | |
console.log(`Retrying in ${retryDelay / 1000} seconds...`); | |
await new Promise((resolve) => setTimeout(resolve, retryDelay)); | |
} | |
} | |
throw new Error(`Max retries (${maxRetries}) exceeded.`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment