mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 02:24:09 +01:00
fix(player): stop retrying the save every second while the server is down
The sync tick re-offers a failed upload on every pass, so an outage turned into a failing request a second for as long as it lasted. While the connection layer knows the server is down, a write now holds its bytes and frame and makes no request at all; the next tick after the connection comes back hands them over. A failure from a server that did answer (a refusal, or a 5xx before the outage is noticed, or the v1 shell, which runs no connection probe) spaces the retries out from 2s to 30s instead, and that wait starts over once an upload lands or the connection returns. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QTsTgdoBinvvxzXeLUjBmV
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
<script setup lang="ts">
|
||||
import type { Emitter } from "mitt";
|
||||
import { storeToRefs } from "pinia";
|
||||
import { inject, onBeforeUnmount, onMounted, onUnmounted, ref } from "vue";
|
||||
import {
|
||||
inject,
|
||||
onBeforeUnmount,
|
||||
onMounted,
|
||||
onUnmounted,
|
||||
ref,
|
||||
watch,
|
||||
} from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { onBeforeRouteLeave, useRouter } from "vue-router";
|
||||
import { useTheme } from "vuetify";
|
||||
@@ -19,6 +26,7 @@ import pendingAssetStore, {
|
||||
} from "@/services/pending-asset";
|
||||
import storeAuth from "@/stores/auth";
|
||||
import storeConfig from "@/stores/config";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
import storeLanguage from "@/stores/language";
|
||||
import storePlaying from "@/stores/playing";
|
||||
import storeRoms, { type DetailedRom } from "@/stores/roms";
|
||||
@@ -45,6 +53,7 @@ import {
|
||||
createQuickLoadButton,
|
||||
createSaveQuitButton,
|
||||
createExitEmulationButton,
|
||||
createRetryBackoff,
|
||||
createSaveSyncTracker,
|
||||
bytesEqual,
|
||||
pollSaveFiles,
|
||||
@@ -58,6 +67,7 @@ const authStore = storeAuth();
|
||||
const romsStore = storeRoms();
|
||||
const playingStore = storePlaying();
|
||||
const configStore = storeConfig();
|
||||
const heartbeatStore = storeHeartbeat();
|
||||
const languageStore = storeLanguage();
|
||||
const router = useRouter();
|
||||
const { t } = useI18n();
|
||||
@@ -162,6 +172,9 @@ function writeSave(
|
||||
if (!held || held.screenshotBytes !== screenshotFile) {
|
||||
await rememberPendingSave(file.saveFile, screenshotFile);
|
||||
}
|
||||
// Nothing gets through while the server is down; the held bytes go once
|
||||
// it is back.
|
||||
if (!heartbeatStore.connected) return null;
|
||||
const save = await saveSave({
|
||||
rom: romRef.value,
|
||||
save: sessionSaveRef.value,
|
||||
@@ -460,6 +473,12 @@ let autoSaveSyncEmulator: object | null = null;
|
||||
let stopSavePolling: (() => void) | null = null;
|
||||
// The boot path awaits before installing, so it may land after unmount.
|
||||
let disposed = false;
|
||||
const retryBackoff = createRetryBackoff();
|
||||
// Back from an outage, the held save goes on the next tick, not after a wait.
|
||||
watch(
|
||||
() => heartbeatStore.connected,
|
||||
(connected) => connected && retryBackoff.reset(),
|
||||
);
|
||||
function installAutoSaveSync() {
|
||||
const emulator = window.EJS_emulator;
|
||||
if (disposed || !emulator?.gameManager) return;
|
||||
@@ -475,6 +494,10 @@ function installAutoSaveSync() {
|
||||
)
|
||||
return;
|
||||
if (!saveTracker.shouldUpload(saveFile)) return;
|
||||
// Down, the write makes no request, so only a server that answered with a
|
||||
// failure has anything to back off from.
|
||||
const online = heartbeatStore.connected;
|
||||
if (online && !retryBackoff.ready()) return;
|
||||
uploading = true;
|
||||
try {
|
||||
// The capture needs the game running, so it happens here, once per save:
|
||||
@@ -487,6 +510,7 @@ function installAutoSaveSync() {
|
||||
screenshotFile,
|
||||
});
|
||||
if (save) {
|
||||
retryBackoff.reset();
|
||||
heldBackSave = null;
|
||||
romsStore.update(romRef.value);
|
||||
displayMessage(t("play.save-synced"), {
|
||||
@@ -494,9 +518,12 @@ function installAutoSaveSync() {
|
||||
tone: "success",
|
||||
icon: "mdi-cloud-sync",
|
||||
});
|
||||
// A write voided by a restore holds nothing back, so there is nothing
|
||||
// to promise for it either.
|
||||
} else if (
|
||||
return;
|
||||
}
|
||||
if (online) retryBackoff.failed();
|
||||
// A write voided by a restore holds nothing back, so there is nothing to
|
||||
// promise for it either.
|
||||
if (
|
||||
heldFor(pendingSave, saveFile) &&
|
||||
!bytesEqual(saveFile, heldBackSave)
|
||||
) {
|
||||
|
||||
@@ -8,7 +8,10 @@ import {
|
||||
captureScreenshot,
|
||||
dumpSaveFile,
|
||||
heldFor,
|
||||
createRetryBackoff,
|
||||
createSaveSyncTracker,
|
||||
RETRY_BACKOFF_MAX_MS,
|
||||
RETRY_BACKOFF_MIN_MS,
|
||||
installEJSDefaultOptionsTrap,
|
||||
pollSaveFiles,
|
||||
resolveScreenshot,
|
||||
@@ -272,6 +275,54 @@ describe("createSaveSyncTracker", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("createRetryBackoff", () => {
|
||||
let clock = 0;
|
||||
const backoff = () => createRetryBackoff(() => clock);
|
||||
|
||||
beforeEach(() => {
|
||||
clock = 0;
|
||||
});
|
||||
|
||||
it("tries straight away until something has failed", () => {
|
||||
expect(backoff().ready()).toBe(true);
|
||||
});
|
||||
|
||||
// Once a second only hammers a server that keeps saying no.
|
||||
it("doubles the wait after each failure, up to a cap", () => {
|
||||
const retry = backoff();
|
||||
const waits: number[] = [];
|
||||
for (let i = 0; i < 6; i++) {
|
||||
retry.failed();
|
||||
const start = clock;
|
||||
while (!retry.ready()) clock += 1_000;
|
||||
waits.push(clock - start);
|
||||
}
|
||||
|
||||
expect(waits).toEqual([
|
||||
RETRY_BACKOFF_MIN_MS,
|
||||
4_000,
|
||||
8_000,
|
||||
16_000,
|
||||
RETRY_BACKOFF_MAX_MS,
|
||||
RETRY_BACKOFF_MAX_MS,
|
||||
]);
|
||||
});
|
||||
|
||||
it("starts over once an upload lands or the server is back", () => {
|
||||
const retry = backoff();
|
||||
retry.failed();
|
||||
retry.failed();
|
||||
expect(retry.ready()).toBe(false);
|
||||
|
||||
retry.reset();
|
||||
expect(retry.ready()).toBe(true);
|
||||
|
||||
retry.failed();
|
||||
clock += RETRY_BACKOFF_MIN_MS;
|
||||
expect(retry.ready()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("pollSaveFiles", () => {
|
||||
const emulatorWith = (sramBytes: number) => ({
|
||||
started: true,
|
||||
|
||||
@@ -331,6 +331,31 @@ export function createSaveSyncTracker() {
|
||||
};
|
||||
}
|
||||
|
||||
// The tick re-offers a failed upload every second, which only hammers a server
|
||||
// that is refusing it or on its way down.
|
||||
export const RETRY_BACKOFF_MIN_MS = 2_000;
|
||||
export const RETRY_BACKOFF_MAX_MS = 30_000;
|
||||
|
||||
/** Spaces out the retries of a failing upload, doubling the wait up to a cap. */
|
||||
export function createRetryBackoff(now: () => number = Date.now) {
|
||||
let delay = 0;
|
||||
let retryAt = 0;
|
||||
return {
|
||||
ready: (): boolean => now() >= retryAt,
|
||||
failed() {
|
||||
delay = Math.min(
|
||||
Math.max(delay * 2, RETRY_BACKOFF_MIN_MS),
|
||||
RETRY_BACKOFF_MAX_MS,
|
||||
);
|
||||
retryAt = now() + delay;
|
||||
},
|
||||
reset() {
|
||||
delay = 0;
|
||||
retryAt = 0;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// EmulatorJS reads each tick off the FS into a fresh buffer, so the tracker can
|
||||
// hold on to one rather than fingerprint it.
|
||||
export function bytesEqual(
|
||||
|
||||
Reference in New Issue
Block a user