Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

resyncRequests.ts

85 lines3.1 KB

1import { ResyncRequests } from "@t3tools/client-runtime/connection";
2​
3/**
4 * Coalescing wrapper around the shared resync signal.
5 *
6 * A reconcile costs a snapshot fetch and a resubscribe, and the callers that
7 * need one arrive in bursts — settling twenty threads at once would otherwise
8 * ask twenty times for work that a single reconcile already covers for the
9 * whole environment.
10 *
11 * Coalescing here is trailing-edge, not leading-edge-only. A request exists
12 * because something observed the client disagreeing with the server, so
13 * dropping it outright would defeat the recovery this exists to perform: a
14 * request suppressed behind an earlier one would leave that disagreement
15 * standing until the user happened to act again. Suppressed requests instead
16 * collapse into one reconcile at the end of the window.
17 */
18​
19/** Minimum spacing between reconciles. */
20export const RESYNC_MIN_INTERVAL_MS = 5_000;
21​
22export interface ResyncScheduler {
23 readonly now: () => number;
24 readonly setTimer: (run: () => void, delayMs: number) => void;
26​
27const defaultScheduler: ResyncScheduler = {
28 now: () => Date.now(),
29 setTimer: (run, delayMs) => {
30 setTimeout(run, delayMs);
31 },
32};
33​
34let lastFiredAtMs: number | null = null;
35let trailingScheduled = false;
36/** Whether a request has arrived that no reconcile has covered yet. */
37let requestOutstanding = false;
38​
39function fire(atMs: number): void {
40 lastFiredAtMs = atMs;
41 requestOutstanding = false;
42 ResyncRequests.requestResync();
44​
45/**
46 * Request a reconcile. Fires immediately when the window is open, otherwise
47 * defers to the end of the current window — never drops the request.
48 */
49export function requestResync(scheduler: ResyncScheduler = defaultScheduler): void {
50 const nowMs = scheduler.now();
51 const elapsedMs = lastFiredAtMs === null ? null : nowMs - lastFiredAtMs;
52 // A clock that jumped backward (NTP correction, manual change) must not
53 // wedge the throttle shut until wall time catches back up: a negative
54 // elapsed means the recorded timestamp is meaningless, so start a fresh
55 // window rather than suppressing every reconcile in the meantime.
56 if (elapsedMs === null || elapsedMs < 0 || elapsedMs >= RESYNC_MIN_INTERVAL_MS) {
57 fire(nowMs);
58 return;
59 }
60 requestOutstanding = true;
61 if (trailingScheduled) {
62 return;
63 }
64 trailingScheduled = true;
65 scheduler.setTimer(() => {
66 trailingScheduled = false;
67 // An immediate reconcile may have landed since this was scheduled — an
68 // open window, or a backward clock jump. It already covered whatever was
69 // outstanding, so firing again would be the double reconcile the
70 // coalescing exists to prevent. Tracking outstanding work rather than
71 // cancelling the timer keeps a request that arrived *after* that
72 // reconcile from being dropped along with the obsolete one.
73 if (!requestOutstanding) {
74 return;
75 }
76 fire(scheduler.now());
77 }, RESYNC_MIN_INTERVAL_MS - elapsedMs);
79​
80/** Test seam: forget the coalescing window and any pending trailing fire. */
81export function resetResyncThrottleForTesting(): void {
82 lastFiredAtMs = null;
83 trailingScheduled = false;
84 requestOutstanding = false;