Skip to content

Commit 1f981a6

Browse files
committed
fix: increase the client-side timeout
1 parent 8ecd7c4 commit 1f981a6

3 files changed

Lines changed: 36 additions & 22 deletions

File tree

components/leaderboard/LeaderBoard.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,24 @@ export const LeaderBoard: React.FC = () => {
2323

2424
try {
2525
const response = await axios.get("/api/topUsers", {
26-
timeout: 30000, // Increased timeout to 30 seconds
26+
timeout: 60000, // Increased timeout to 60 seconds
27+
headers: {
28+
'Cache-Control': 'no-cache',
29+
'Pragma': 'no-cache',
30+
'Expires': '0',
31+
}
2732
});
2833
setLeaderBoardData(response.data);
2934
} catch (error) {
3035
console.error("Failed to fetch leaderboard data:", error);
3136
setIsError(true);
3237

33-
// Retry logic
38+
// Retry logic with exponential backoff
3439
if (retryCount < 3) {
40+
const backoffTime = Math.pow(2, retryCount) * 2000; // 2s, 4s, 8s
3541
setTimeout(() => {
3642
setRetryCount(prev => prev + 1);
37-
}, 2000); // Wait 2 seconds before retrying
43+
}, backoffTime);
3844
}
3945
} finally {
4046
setIsLoading(false);
@@ -99,3 +105,4 @@ export const LeaderBoard: React.FC = () => {
99105

100106

101107

108+

pages/api/topUsers.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getTopUsersFromDb, storeTopUsersInDb } from "../../utils/fetchTopUsersF
33

44
export default async function handler(req, res) {
55
// Add CORS headers
6-
res.setHeader('Cache-Control', 's-maxage=300, stale-while-revalidate');
6+
res.setHeader('Cache-Control', 'public, s-maxage=300, stale-while-revalidate=599');
77

88
try {
99
console.log('API: Checking DB for cached top users...');
@@ -17,21 +17,30 @@ export default async function handler(req, res) {
1717
// Optionally refresh cache in background if data is old
1818
const cacheAge = Date.now() - new Date(data[0].timestamp).getTime();
1919
if (cacheAge > 3600000) { // 1 hour
20-
refreshCache();
20+
refreshCache().catch(console.error);
2121
}
2222
return;
2323
}
2424

25-
// If no cached data, fetch new data
25+
// If no cached data, fetch new data with timeout
2626
console.log('API: No cached data found, fetching from GitHub...');
27-
data = await fetchTopUsersByPullRequests("fork-commit-merge/fork-commit-merge");
27+
const controller = new AbortController();
28+
const timeout = setTimeout(() => controller.abort(), 50000); // 50 second timeout
2829

29-
if (data && data.length > 0) {
30-
console.log('API: Storing new data in DB...');
31-
await storeTopUsersInDb(data);
32-
res.status(200).json(data);
33-
} else {
34-
res.status(404).json({ error: 'No data available' });
30+
try {
31+
data = await fetchTopUsersByPullRequests("fork-commit-merge/fork-commit-merge");
32+
clearTimeout(timeout);
33+
34+
if (data && data.length > 0) {
35+
console.log('API: Storing new data in DB...');
36+
await storeTopUsersInDb(data);
37+
res.status(200).json(data);
38+
} else {
39+
res.status(404).json({ error: 'No data available' });
40+
}
41+
} catch (error) {
42+
clearTimeout(timeout);
43+
throw error;
3544
}
3645
} catch (error) {
3746
console.error('API route error:', error);
@@ -52,3 +61,4 @@ async function refreshCache() {
5261
}
5362

5463

64+

utils/fetchTopUsersFromDb.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
import { connectToDB } from './db';
22

3-
interface UserStat {
4-
login: string;
5-
avatar_url: string;
6-
html_url: string;
7-
contributions: number;
8-
}
9-
103
export async function getTopUsersFromDb() {
114
try {
125
const { db } = await connectToDB();
13-
const data = await db.collection('topUsers').find().toArray();
6+
const data = await db.collection('topUsers')
7+
.find()
8+
.maxTimeMS(5000) // 5 second timeout for DB query
9+
.toArray();
1410
return data.length > 0 ? data : null;
1511
} catch (error) {
1612
console.error('DB Error:', error);
1713
return null;
1814
}
1915
}
2016

21-
export async function storeTopUsersInDb(data: UserStat[]) {
17+
export async function storeTopUsersInDb(data: any[]) {
2218
try {
2319
const { db } = await connectToDB();
2420
await db.collection('topUsers').deleteMany({});
@@ -65,3 +61,4 @@ export async function storeTopThreeUsersInDb(data: any[]) {
6561

6662

6763

64+

0 commit comments

Comments
 (0)