mirror of
https://github.com/WowUp/WowUp.git
synced 2026-04-22 06:49:05 -04:00
#1201 Add progressive backoff for Wago frame
This commit is contained in:
@@ -5,6 +5,11 @@ const { inspect } = require("util");
|
||||
|
||||
const LOG_PATH = getArg("log-path");
|
||||
|
||||
const BACKOFF_KEY = "wago-backoff";
|
||||
const BACKOFF_SET_KEY = "wago-backoff-set";
|
||||
const BACKOFF_RESET_AGE = 5 * 60000;
|
||||
const BACKOFF_MAX_WAIT = 2 * 60000;
|
||||
|
||||
let keyExpectedTimeout = 0;
|
||||
|
||||
function getArg(argKey) {
|
||||
@@ -40,7 +45,7 @@ window.addEventListener(
|
||||
const errMsg = e.error?.toString() || "unknown error on " + window.location;
|
||||
console.error(`[wago-preload] error listener:`, e.message, errMsg);
|
||||
ipcRenderer.send("webview-error", inspect(e.error), e.message);
|
||||
window.setTimeout(() => window.location.reload(), 2000);
|
||||
backoffReload();
|
||||
},
|
||||
true
|
||||
);
|
||||
@@ -62,7 +67,29 @@ contextBridge.exposeInMainWorld("wago", {
|
||||
// Can happen if the page returns bad responses (500 etc)
|
||||
keyExpectedTimeout = window.setTimeout(() => {
|
||||
console.log("[wago-preload] failed to get key in time, reloading");
|
||||
window.location.reload();
|
||||
backoffReload();
|
||||
}, 30000);
|
||||
|
||||
console.log(`[wago-preload] init`);
|
||||
console.log(`[wago-preload] init`, window.location.href);
|
||||
|
||||
function backoffReload() {
|
||||
let backoffSet = window.sessionStorage.getItem("wago-backoff-set");
|
||||
backoffSet = backoffSet ? parseInt(backoffSet, 10) : 0;
|
||||
|
||||
let backoff = window.sessionStorage.getItem(BACKOFF_KEY);
|
||||
backoff = Math.min(backoff ? parseInt(backoff, 10) * 2 : 2000, BACKOFF_MAX_WAIT);
|
||||
|
||||
// If the backoff time is old, reset the backoff
|
||||
if (Date.now() - backoffSet > BACKOFF_RESET_AGE) {
|
||||
backoff = 2000;
|
||||
}
|
||||
|
||||
console.log("[wago] setting reload backoff", backoff);
|
||||
window.sessionStorage.setItem(BACKOFF_KEY, backoff);
|
||||
window.sessionStorage.setItem(BACKOFF_SET_KEY, Date.now().toString());
|
||||
|
||||
// Wait the calculated time
|
||||
window.setTimeout(() => {
|
||||
window.location.reload();
|
||||
}, backoff);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user