Skip to content

Commit 8d48f0d

Browse files
committed
chore: prevent fetch flooding of external css links for code hints
1 parent 76b6337 commit 8d48f0d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/language/CSSUtils.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1920,11 +1920,22 @@ define(function (require, exports, module) {
19201920
}
19211921

19221922
const cacheInProgress = new Set();
1923+
// block fetches to only happen once every 10 seconds to prevent loading web servers on every code hint type
1924+
// Cached external links are never fetched again. problem happens when links fetch fails, and can be immediately
1925+
// retried on next key press. we need to block that and do it once in 10 seconds only.
1926+
const fetchBlock10Secs = new Phoenix.libs.LRUCache({
1927+
max: 5000,
1928+
ttl: 1000 * 10 // in ms
1929+
});
19231930
async function _precacheExternalStyleSheet(link) {
19241931
try {
1925-
if(cacheInProgress.has(link)){
1932+
// dont use fetchBlock10Secs.has as lru cache wont delete ttl expired items for performance.
1933+
// fetchBlock10Secs.get will return undefined for ttl expired items
1934+
// we purge this cache anyway on project switch/max entry reached.
1935+
if(cacheInProgress.has(link) || fetchBlock10Secs.get(link)){
19261936
return;
19271937
}
1938+
fetchBlock10Secs.set(link, true);
19281939
cacheInProgress.add(link);
19291940
const extension = path.extname(new URL(link).pathname).slice(1);
19301941
if (!extension || !CSS_MODE_MAP[extension]) {
@@ -2077,6 +2088,7 @@ define(function (require, exports, module) {
20772088
ProjectManager.on(ProjectManager.EVENT_PROJECT_FILE_CHANGED, _projectFileChanged);
20782089
ProjectManager.on(ProjectManager.EVENT_PROJECT_OPEN, ()=>{
20792090
CSSSelectorCache.clear();
2091+
fetchBlock10Secs.clear();
20802092
setTimeout(_populateSelectorCache, 2000);
20812093
});
20822094
setTimeout(_populateSelectorCache, 2000);

0 commit comments

Comments
 (0)