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>([
32 WS_METHODS.previewAutomationConnect,
33 WS_METHODS.serverGetUsageSummary,
34]);
35const longRunningRpcAckMethods = new Set<string>([
36 WS_METHODS.serverUpdateProvider,
37 WS_METHODS.serverRefreshProviders,
38 WS_METHODS.serverUpdateServer,
39]);
40
41const slowRpcAckRequestsAtom = Atom.make<ReadonlyArray<SlowRpcAckRequest>>([]).pipe(
42 Atom.keepAlive,
43 Atom.withLabel("slow-rpc-ack-requests"),
44);
45
46function setSlowRpcAckRequests(requests: ReadonlyArray<SlowRpcAckRequest>) {
47 appAtomRegistry.set(slowRpcAckRequestsAtom, [...requests]);
48}
49
50function getSlowRpcAckRequestsValue(): ReadonlyArray<SlowRpcAckRequest> {
51 return appAtomRegistry.get(slowRpcAckRequestsAtom);
52}
53
54function shouldTrackRpcAck(method: string): boolean {
55 return (
56 !method.includes("subscribe") &&
57 !method.startsWith("pullRequests.") &&
58 !untrackedRpcAckMethods.has(method)
59 );
60}
61
62function rpcAckThresholdMs(method: string): number {
63 return longRunningRpcAckMethods.has(method)
64 ? Math.max(slowRpcAckThresholdMs, LONG_RUNNING_RPC_ACK_THRESHOLD_MS)
65 : slowRpcAckThresholdMs;
66}
67
68export function getSlowRpcAckRequests(): ReadonlyArray<SlowRpcAckRequest> {
69 return getSlowRpcAckRequestsValue();
70}
71
72/**
73 * Starts the slow-request timer for one in-flight unary RPC. `method` is the
74 * bare WS method (used to decide whether and how long to wait); `tag` is the
75 * human-readable label shown in the toast, which defaults to the method.
76 */
77export function trackRpcRequestSent(requestId: string, method: string, tag = method): void {
78 if (!shouldTrackRpcAck(method)) {
79 return;
80 }
81
82 clearTrackedRpcRequest(requestId);
83 evictOldestPendingRpcRequestIfNeeded();
84
85 const startedAtMs = Date.now();
86 const thresholdMs = rpcAckThresholdMs(method);
87 const request: SlowRpcAckRequest = {
88 requestId,
89 startedAt: new Date(startedAtMs).toISOString(),
90 startedAtMs,
91 tag,
92 thresholdMs,
93 };
94 const timeoutId = setTimeout(() => {
95 pendingRpcAckRequests.delete(requestId);
96 appendSlowRpcAckRequest(request);
97 }, thresholdMs);
98
99 pendingRpcAckRequests.set(requestId, {
100 request,
101 timeoutId,
102 });
103}
104
105export function acknowledgeRpcRequest(requestId: string): void {
106 clearTrackedRpcRequest(requestId);
107 const slowRequests = getSlowRpcAckRequestsValue();
108 if (!slowRequests.some((request) => request.requestId === requestId)) {
109 return;
110 }
111
112 setSlowRpcAckRequests(slowRequests.filter((request) => request.requestId !== requestId));
113}
114
115function clearAllTrackedRpcRequests(): void {
116 for (const pending of pendingRpcAckRequests.values()) {
117 clearTimeout(pending.timeoutId);
118 }
119 pendingRpcAckRequests.clear();
120 setSlowRpcAckRequests([]);
121}
122
123function clearTrackedRpcRequest(requestId: string): void {
124 const pending = pendingRpcAckRequests.get(requestId);
125 if (!pending) {
126 return;
127 }
128
129 clearTimeout(pending.timeoutId);
130 pendingRpcAckRequests.delete(requestId);
131}
132
133function appendSlowRpcAckRequest(request: SlowRpcAckRequest): void {
134 const requests = [...getSlowRpcAckRequestsValue(), request];
135 if (requests.length <= MAX_TRACKED_RPC_ACK_REQUESTS) {
136 setSlowRpcAckRequests(requests);
137 return;
138 }
139
140 setSlowRpcAckRequests(requests.slice(-MAX_TRACKED_RPC_ACK_REQUESTS));
141}
142
143function evictOldestPendingRpcRequestIfNeeded(): void {
144 while (pendingRpcAckRequests.size >= MAX_TRACKED_RPC_ACK_REQUESTS) {
145 const oldestRequestId = pendingRpcAckRequests.keys().next().value;
146 if (oldestRequestId === undefined) {
147 return;
148 }
149
150 clearTrackedRpcRequest(oldestRequestId);
151 }
152}
153
154export function resetRequestLatencyStateForTests(): void {
155 slowRpcAckThresholdMs = SLOW_RPC_ACK_THRESHOLD_MS;
156 clearAllTrackedRpcRequests();
157}
158
159export function useSlowRpcAckRequests(): ReadonlyArray<SlowRpcAckRequest> {
160 return useAtomValue(slowRpcAckRequestsAtom);
161}