Skip to content

Commit 90dc9c0

Browse files
committed
Enhance data fetching in visualizer/main.js to prevent duplicate requests
- Introduced a flag to track ongoing fetch requests, preventing multiple simultaneous calls to the API. - The problem of retry storm can happen if the data is large or the bandwidth is limited. fetchAndRender does not finish before the next interval and a buildup of requests will happen.
1 parent 65cbbe8 commit 90dc9c0

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

scripts/static/js/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ function loadAndRenderData(data) {
118118
if (window.STATIC_DATA) {
119119
loadAndRenderData(window.STATIC_DATA);
120120
} else {
121+
let isFetching = false;
122+
121123
function fetchAndRender() {
124+
if (isFetching) return;
125+
isFetching = true;
126+
122127
fetch('/api/data')
123128
.then(resp => resp.json())
124129
.then(data => {
@@ -128,6 +133,12 @@ if (window.STATIC_DATA) {
128133
}
129134
lastDataStr = dataStr;
130135
loadAndRenderData(data);
136+
})
137+
.catch(err => {
138+
console.error('Failed to fetch data:', err);
139+
})
140+
.finally(() => {
141+
isFetching = false;
131142
});
132143
}
133144
fetchAndRender();

0 commit comments

Comments
 (0)