-
-
Notifications
You must be signed in to change notification settings - Fork 23.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from anuraghazra/fix-ratelimit
fix: increase github rate limit with multiple PATs - special thanks to @filiptronicek @ApurvShah007 @garvit-joshi for helping out :D
- Loading branch information
Showing
8 changed files
with
150 additions
and
30 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const retryer = async (fetcher, variables, retries = 0) => { | ||
if (retries > 7) { | ||
throw new Error("Maximum retries exceeded"); | ||
} | ||
try { | ||
console.log(`Trying PAT_${retries + 1}`); | ||
|
||
// try to fetch with the first token since RETRIES is 0 index i'm adding +1 | ||
let response = await fetcher( | ||
variables, | ||
process.env[`PAT_${retries + 1}`], | ||
retries | ||
); | ||
|
||
// prettier-ignore | ||
const isRateExceeded = response.data.errors && response.data.errors[0].type === "RATE_LIMITED"; | ||
|
||
// if rate limit is hit increase the RETRIES and recursively call the retryer | ||
// with username, and current RETRIES | ||
if (isRateExceeded) { | ||
console.log(`PAT_${retries + 1} Failed`); | ||
retries++; | ||
// directly return from the function | ||
return retryer(fetcher, variables, retries); | ||
} | ||
|
||
// finally return the response | ||
return response; | ||
} catch (err) { | ||
// prettier-ignore | ||
// also checking for bad credentials if any tokens gets invalidated | ||
const isBadCredential = err.response.data && err.response.data.message === "Bad credentials"; | ||
|
||
if (isBadCredential) { | ||
console.log(`PAT_${retries + 1} Failed`); | ||
retries++; | ||
// directly return from the function | ||
return retryer(fetcher, variables, retries); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = retryer; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require("@testing-library/jest-dom"); | ||
const retryer = require("../src/retryer"); | ||
|
||
const fetcher = jest.fn((variables, token) => { | ||
console.log(variables, token); | ||
return new Promise((res, rej) => res({ data: "ok" })); | ||
}); | ||
|
||
const fetcherFail = jest.fn(() => { | ||
return new Promise((res, rej) => | ||
res({ data: { errors: [{ type: "RATE_LIMITED" }] } }) | ||
); | ||
}); | ||
|
||
const fetcherFailOnSecondTry = jest.fn((_vars, _token, retries) => { | ||
return new Promise((res, rej) => { | ||
// faking rate limit | ||
if (retries < 1) { | ||
return res({ data: { errors: [{ type: "RATE_LIMITED" }] } }); | ||
} | ||
return res({ data: "ok" }); | ||
}); | ||
}); | ||
|
||
describe("Test Retryer", () => { | ||
it("retryer should return value and have zero retries on first try", async () => { | ||
let res = await retryer(fetcher, {}); | ||
|
||
expect(fetcher).toBeCalledTimes(1); | ||
expect(res).toStrictEqual({ data: "ok" }); | ||
}); | ||
|
||
it("retryer should return value and have 2 retries", async () => { | ||
let res = await retryer(fetcherFailOnSecondTry, {}); | ||
|
||
expect(fetcherFailOnSecondTry).toBeCalledTimes(2); | ||
expect(res).toStrictEqual({ data: "ok" }); | ||
}); | ||
|
||
it("retryer should throw error if maximum retries reached", async () => { | ||
let res; | ||
|
||
try { | ||
res = await retryer(fetcherFail, {}); | ||
} catch (err) { | ||
expect(fetcherFail).toBeCalledTimes(8); | ||
expect(err.message).toBe("Maximum retries exceeded"); | ||
} | ||
}); | ||
}); |