Files
Pulse/frontend-modern/src/utils/relativeTimeClock.ts
rcourtman ba26b3581e Settle slow polled reads and keep replication links honest
A review of #2578 found three gaps it left.

The shared poll in createNonSuspendingQuery replaced any read still in
flight on every tick. The API answers 429 with Retry-After: 60 and
apiFetch waits it out, so under rate limiting a polled query slower than
its 30-second interval never settled. Replication reads also ignored the
abort signal, so each replaced read kept running. A poll now skips its
tick while a read an earlier poll started is still in flight; a refresh,
source change or remount revalidation stays replaceable at the next tick
as before. A polled read in flight for 150 seconds is presumed stalled
and replaced, which also clears any spinner it held. Replication reads
forward the signal to apiFetch.

A failed first replication read made the job count zero, so a direct
/proxmox/replication link fell to Overview without its error or Retry.
Replication presence is now unknown until a read succeeds, and a failed
poll after that keeps the last answer instead of reopening the route.

The shared clock returned its last tick, so a cell mounted between ticks
measured from up to 30 seconds ago and the desktop backup age could
disagree with its band. Every read now returns the wall clock and the
tick only schedules re-reads, which retires the backup Math.max
workaround. The #2578 age tests now jump the clock and fire one tick
instead of looping hundreds of re-renders, which timed out under load.
2026-10-06 20:15:12 +01:00

40 lines
1.5 KiB
TypeScript

import { createSignal, getOwner, onCleanup } from 'solid-js';
// One clock for every relative time on screen ("2h ago"), so ages keep moving
// while rows stay mounted. Tables keep their rows across data refreshes, and a
// timestamp that does not change would otherwise freeze its age at whatever it
// read when the row first rendered. It ticks while anything reads it and stops
// when nothing does.
export const RELATIVE_TIME_TICK_MS = 30_000;
// The tick only tells readers to re-read; every read returns the wall clock.
// A cell that mounts between ticks would otherwise measure from up to one tick
// ago, so a timestamp from the last few seconds could read as in the future
// and two cells formatted from the same time could disagree.
const [tick, setTick] = createSignal(0);
const now = (): number => {
tick();
return Date.now();
};
let readers = 0;
let timer: ReturnType<typeof setInterval> | undefined;
// Inside a component or reactive root, the returned accessor tracks the shared
// tick. Outside one there is nothing to stop a timer, so it reads the time
// directly.
export function useRelativeTimeNow(): () => number {
if (!getOwner()) return () => Date.now();
readers += 1;
if (timer === undefined) {
timer = setInterval(() => setTick((count) => count + 1), RELATIVE_TIME_TICK_MS);
}
onCleanup(() => {
readers -= 1;
if (readers === 0 && timer !== undefined) {
clearInterval(timer);
timer = undefined;
}
});
return now;
}