forked from anuraghazra/github-readme-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactored retryer logic & handled invalid tokens
- Loading branch information
1 parent
3d8dea9
commit 429d65a
Showing
6 changed files
with
112 additions
and
45 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
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"); | ||
} | ||
}); | ||
}); |