Skip to content

Commit 42f5841

Browse files
authored
fix: add retry mechanism to fetch.js to handle rate limiting
- Implemented a retry mechanism with exponential backoff for both GitHub and Medium API requests to handle HTTP 429 (Too Many Requests) errors. - Improved error handling to ensure more graceful recovery from transient errors. - Added logging for better visibility into retry attempts and errors.
1 parent 5d9216f commit 42f5841

File tree

1 file changed

+64
-44
lines changed

1 file changed

+64
-44
lines changed

fetch.js

+64-44
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,48 @@ const ERR = {
1818
"The request to Medium didn't succeed. Check if Medium username in your .env file is correct."
1919
};
2020

21+
async function fetchWithRetry(options, data, retries = 5, delay = 5000) {
22+
for (let i = 0; i < retries; i++) {
23+
try {
24+
return await new Promise((resolve, reject) => {
25+
const req = https.request(options, res => {
26+
let responseData = "";
27+
28+
res.on("data", d => {
29+
responseData += d;
30+
});
31+
32+
res.on("end", () => {
33+
if (res.statusCode === 200) {
34+
resolve(responseData);
35+
} else if (res.statusCode === 429 && i < retries - 1) {
36+
console.log(`Rate limited. Retrying in ${delay}ms...`);
37+
setTimeout(() => resolve(fetchWithRetry(options, data, retries - 1, delay)), delay);
38+
} else {
39+
reject(new Error(`${options.path} request failed with status code: ${res.statusCode}`));
40+
}
41+
});
42+
});
43+
44+
req.on("error", error => {
45+
reject(error);
46+
});
47+
48+
if (data) {
49+
req.write(data);
50+
}
51+
req.end();
52+
});
53+
} catch (error) {
54+
if (i === retries - 1) {
55+
throw error;
56+
}
57+
console.log(`Error fetching data. Retrying in ${delay}ms...`);
58+
await new Promise(resolve => setTimeout(resolve, delay));
59+
}
60+
}
61+
}
62+
2163
if (USE_GITHUB_DATA === "true") {
2264
if (GITHUB_USERNAME === undefined) {
2365
throw new Error(ERR.noUserName);
@@ -69,31 +111,16 @@ if (USE_GITHUB_DATA === "true") {
69111
}
70112
};
71113

72-
const req = https.request(default_options, res => {
73-
let data = "";
74-
75-
console.log(`statusCode: ${res.statusCode}`);
76-
if (res.statusCode !== 200) {
77-
throw new Error(ERR.requestFailed);
78-
}
79-
80-
res.on("data", d => {
81-
data += d;
82-
});
83-
res.on("end", () => {
84-
fs.writeFile("./public/profile.json", data, function (err) {
114+
fetchWithRetry(default_options, data)
115+
.then(responseData => {
116+
fs.writeFile("./public/profile.json", responseData, function (err) {
85117
if (err) return console.log(err);
86118
console.log("saved file to public/profile.json");
87119
});
120+
})
121+
.catch(error => {
122+
console.error("Error:", error);
88123
});
89-
});
90-
91-
req.on("error", error => {
92-
throw error;
93-
});
94-
95-
req.write(data);
96-
req.end();
97124
}
98125

99126
if (MEDIUM_USERNAME !== undefined) {
@@ -106,29 +133,22 @@ if (MEDIUM_USERNAME !== undefined) {
106133
method: "GET"
107134
};
108135

109-
const req = https.request(options, res => {
110-
console.log(`statusCode: ${res.statusCode}`);
111-
if (res.statusCode !== 200) {
112-
throw new Error(ERR.requestMediumFailed);
113-
}
114-
115-
feed2json.fromStream(res, url, {}, (err, json) => {
116-
if (err) {
117-
console.error("Error converting feed to JSON:", err);
118-
return;
119-
}
136+
fetchWithRetry(options)
137+
.then(responseData => {
138+
feed2json.fromString(responseData, url, {}, (err, json) => {
139+
if (err) {
140+
console.error("Error converting feed to JSON:", err);
141+
return;
142+
}
120143

121-
const mediumData = JSON.stringify(json, null, 2);
122-
fs.writeFile("./public/blogs.json", mediumData, function (err) {
123-
if (err) return console.error(err);
124-
console.log("Saved file to public/blogs.json");
144+
const mediumData = JSON.stringify(json, null, 2);
145+
fs.writeFile("./public/blogs.json", mediumData, function (err) {
146+
if (err) return console.error(err);
147+
console.log("Saved file to public/blogs.json");
148+
});
125149
});
150+
})
151+
.catch(error => {
152+
console.error("Error:", error);
126153
});
127-
});
128-
129-
req.on("error", error => {
130-
throw error;
131-
});
132-
133-
req.end();
134-
}
154+
}

0 commit comments

Comments
 (0)