Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { WS_METHODS } from "@t3tools/contracts";
3import { Atom } from "effect/unstable/reactivity";
4
5import { appAtomRegistry } from "./atomRegistry";
6
7export const SLOW_RPC_ACK_THRESHOLD_MS = 15_000;
8/**
9 * Some requests are slow by design — they shell out to a package manager on the
10 * server and only respond once the install finishes. Warning about those after
11 * 15s is noise, so they get a much longer leash.
12 */
13export const LONG_RUNNING_RPC_ACK_THRESHOLD_MS = 120_000;
14export const MAX_TRACKED_RPC_ACK_REQUESTS = 256;
15let slowRpcAckThresholdMs = SLOW_RPC_ACK_THRESHOLD_MS;
16
17export interface SlowRpcAckRequest {
18 readonly requestId: string;
19 readonly startedAt: string;
20 readonly startedAtMs: number;
21 readonly tag: string;
22 readonly thresholdMs: number;
23}
24
25interface PendingRpcAckRequest {
26 readonly request: SlowRpcAckRequest;
27 readonly timeoutId: ReturnType<typeof setTimeout>;
28}
29
30const pendingRpcAckRequests = new Map<string, PendingRpcAckRequest>();
31const untrackedRpcAckMethods = new Set<string>([WS_METHODS.previewAutomationConnect]);
32const longRunningRpcAckMethods = new Set<string>([
33 WS_METHODS.serverUpdateProvider,
34 WS_METHODS.serverRefreshProviders,
35 WS_METHODS.serverUpdateServer,
36]);
37
38const slowRpcAckRequestsAtom = Atom.make<ReadonlyArray<SlowRpcAckRequest>>([]).pipe(
39 Atom.keepAlive,
40 Atom.withLabel("slow-rpc-ack-requests"),
41);
42
43function setSlowRpcAckRequests(requests: ReadonlyArray<SlowRpcAckRequest>) {
44 appAtomRegistry.set(slowRpcAckRequestsAtom, [...requests]);
45}
46
47function getSlowRpcAckRequestsValue(): ReadonlyArray<SlowRpcAckRequest> {
48 return appAtomRegistry.get(slowRpcAckRequestsAtom);
49}
50
51function shouldTrackRpcAck(method: string): boolean {
52 return !method.includes("subscribe") && !untrackedRpcAckMethods.has(method);
53}
54
55function rpcAckThresholdMs(method: string): number {
56 return longRunningRpcAckMethods.has(method)
57 ? Math.max(slowRpcAckThresholdMs, LONG_RUNNING_RPC_ACK_THRESHOLD_MS)
58 : slowRpcAckThresholdMs;
59}
60
61export function getSlowRpcAckRequests(): ReadonlyArray<SlowRpcAckRequest> {
62 return getSlowRpcAckRequestsValue();
63}
64
65/**
66 * Starts the slow-request timer for one in-flight unary RPC. `method` is the
67 * bare WS method (used to decide whether and how long to wait); `tag` is the
68 * human-readable label shown in the toast, which defaults to the method.
69 */
70export function trackRpcRequestSent(requestId: string, method: string, tag = method): void {
71 if (!shouldTrackRpcAck(method)) {
72 return;
73 }
74
75 clearTrackedRpcRequest(requestId);
76 evictOldestPendingRpcRequestIfNeeded();
77
78 const startedAtMs = Date.now();
79 const thresholdMs = rpcAckThresholdMs(method);
80 const request: SlowRpcAckRequest = {
81 requestId,
82 startedAt: new Date(startedAtMs).toISOString(),
83 startedAtMs,
84 tag,
85 thresholdMs,
86 };
87 const timeoutId = setTimeout(() => {
88 pendingRpcAckRequests.delete(requestId);
89 appendSlowRpcAckRequest(request);
90 }, thresholdMs);
91
92 pendingRpcAckRequests.set(requestId, {
93 request,
94 timeoutId,
95 });
96}
97
98export function acknowledgeRpcRequest(requestId: string): void {
99 clearTrackedRpcRequest(requestId);
100 const slowRequests = getSlowRpcAckRequestsValue();
101 if (!slowRequests.some((request) => request.requestId === requestId)) {
102 return;
103 }
104
105 setSlowRpcAckRequests(slowRequests.filter((request) => request.requestId !== requestId));
106}
107
108export function clearAllTrackedRpcRequests(): void {
109 for (const pending of pendingRpcAckRequests.values()) {
110 clearTimeout(pending.timeoutId);
111 }
112 pendingRpcAckRequests.clear();
113 setSlowRpcAckRequests([]);
114}
115
116function clearTrackedRpcRequest(requestId: string): void {
117 const pending = pendingRpcAckRequests.get(requestId);
118 if (!pending) {
119 return;
120 }
121
122 clearTimeout(pending.timeoutId);
123 pendingRpcAckRequests.delete(requestId);
124}
125
126function appendSlowRpcAckRequest(request: SlowRpcAckRequest): void {
127 const requests = [...getSlowRpcAckRequestsValue(), request];
128 if (requests.length <= MAX_TRACKED_RPC_ACK_REQUESTS) {
129 setSlowRpcAckRequests(requests);
130 return;
131 }
132
133 setSlowRpcAckRequests(requests.slice(-MAX_TRACKED_RPC_ACK_REQUESTS));
134}
135
136function evictOldestPendingRpcRequestIfNeeded(): void {
137 while (pendingRpcAckRequests.size >= MAX_TRACKED_RPC_ACK_REQUESTS) {
138 const oldestRequestId = pendingRpcAckRequests.keys().next().value;
139 if (oldestRequestId === undefined) {
140 return;
141 }
142
143 clearTrackedRpcRequest(oldestRequestId);
144 }
145}
146
147export function resetRequestLatencyStateForTests(): void {
148 slowRpcAckThresholdMs = SLOW_RPC_ACK_THRESHOLD_MS;
149 clearAllTrackedRpcRequests();
150}
151
152export function setSlowRpcAckThresholdMsForTests(thresholdMs: number): void {
153 slowRpcAckThresholdMs = thresholdMs;
154}
155
156export function useSlowRpcAckRequests(): ReadonlyArray<SlowRpcAckRequest> {
157 return useAtomValue(slowRpcAckRequestsAtom);
158}