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

requestLatencyState.ts

144 lines4.4 KB

1import { useAtomValue } from "@effect/atom-react";
2import { WS_METHODS } from "@t3tools/contracts";
3import { Atom } from "effect/reactivity";
4​
5import { appAtomRegistry } from "./atomRegistry";
6​
7const SLOW_RPC_ACK_THRESHOLD_MS = 15_000;
8/**
9 * Some requests are slow by design. Provider and server updates shell out to a
10 * package manager and only respond once the install finishes. A VCS status
11 * refresh fetches, may auto-pull, looks up the pull request, and first waits for
12 * any background refresh of the same repository, which often totals over 15s.
13 * Warning about those after 15s is noise, so they get a much longer leash.
14 */
15const LONG_RUNNING_RPC_ACK_THRESHOLD_MS = 120_000;
16const MAX_TRACKED_RPC_ACK_REQUESTS = 256;
17let slowRpcAckThresholdMs = SLOW_RPC_ACK_THRESHOLD_MS;
18​
19export interface SlowRpcAckRequest {
20 readonly requestId: string;
21 readonly startedAt: string;
22 readonly startedAtMs: number;
23 readonly tag: string;
24 readonly thresholdMs: number;
26​
27interface PendingRpcAckRequest {
28 readonly request: SlowRpcAckRequest;
29 readonly timeoutId: ReturnType<typeof setTimeout>;
31​
32const pendingRpcAckRequests = new Map<string, PendingRpcAckRequest>();
33// Background reports nobody waits on. A busy host delays them first, and warning
34// about a request the user never made does not say what is wrong.
35const untrackedRpcAckMethods = new Set<string>([
36 WS_METHODS.serverGetUsageSummary,
37 WS_METHODS.serverReportClientActivity,
38]);
39const longRunningRpcAckMethods = new Set<string>([
40 WS_METHODS.serverUpdateProvider,
41 WS_METHODS.serverRefreshProviders,
42 WS_METHODS.serverUpdateServer,
43 WS_METHODS.vcsRefreshStatus,
44]);
45​
46const slowRpcAckRequestsAtom = Atom.make<ReadonlyArray<SlowRpcAckRequest>>([]).pipe(
47 Atom.keepAlive,
48 Atom.withLabel("slow-rpc-ack-requests"),
49);
50​
51function setSlowRpcAckRequests(requests: ReadonlyArray<SlowRpcAckRequest>) {
52 appAtomRegistry.set(slowRpcAckRequestsAtom, [...requests]);
54​
55function getSlowRpcAckRequestsValue(): ReadonlyArray<SlowRpcAckRequest> {
56 return appAtomRegistry.get(slowRpcAckRequestsAtom);
58​
59function shouldTrackRpcAck(method: string): boolean {
60 return (
61 !method.includes("subscribe") &&
62 !method.startsWith("pullRequests.") &&
63 !untrackedRpcAckMethods.has(method)
64 );
66​
67function rpcAckThresholdMs(method: string): number {
68 return longRunningRpcAckMethods.has(method)
69 ? Math.max(slowRpcAckThresholdMs, LONG_RUNNING_RPC_ACK_THRESHOLD_MS)
70 : slowRpcAckThresholdMs;
72​
73export function trackRpcRequestSent(requestId: string, method: string, tag = method): void {
74 if (!shouldTrackRpcAck(method)) {
75 return;
76 }
77​
78 clearTrackedRpcRequest(requestId);
79 evictOldestPendingRpcRequestIfNeeded();
80​
81 const startedAtMs = Date.now();
82 const thresholdMs = rpcAckThresholdMs(method);
83 const request: SlowRpcAckRequest = {
84 requestId,
85 startedAt: new Date(startedAtMs).toISOString(),
86 startedAtMs,
87 tag,
88 thresholdMs,
89 };
90 const timeoutId = setTimeout(() => {
91 pendingRpcAckRequests.delete(requestId);
92 appendSlowRpcAckRequest(request);
93 }, thresholdMs);
94​
95 pendingRpcAckRequests.set(requestId, {
96 request,
97 timeoutId,
98 });
100​
101export function acknowledgeRpcRequest(requestId: string): void {
102 clearTrackedRpcRequest(requestId);
103 const slowRequests = getSlowRpcAckRequestsValue();
104 if (!slowRequests.some((request) => request.requestId === requestId)) {
105 return;
106 }
107​
108 setSlowRpcAckRequests(slowRequests.filter((request) => request.requestId !== requestId));
110​
111function clearTrackedRpcRequest(requestId: string): void {
112 const pending = pendingRpcAckRequests.get(requestId);
113 if (!pending) {
114 return;
115 }
116​
117 clearTimeout(pending.timeoutId);
118 pendingRpcAckRequests.delete(requestId);
120​
121function appendSlowRpcAckRequest(request: SlowRpcAckRequest): void {
122 const requests = [...getSlowRpcAckRequestsValue(), request];
123 if (requests.length <= MAX_TRACKED_RPC_ACK_REQUESTS) {
124 setSlowRpcAckRequests(requests);
125 return;
126 }
127​
128 setSlowRpcAckRequests(requests.slice(-MAX_TRACKED_RPC_ACK_REQUESTS));
130​
131function evictOldestPendingRpcRequestIfNeeded(): void {
132 while (pendingRpcAckRequests.size >= MAX_TRACKED_RPC_ACK_REQUESTS) {
133 const oldestRequestId = pendingRpcAckRequests.keys().next().value;
134 if (oldestRequestId === undefined) {
135 return;
136 }
137​
138 clearTrackedRpcRequest(oldestRequestId);
139 }
141​
142export function useSlowRpcAckRequests(): ReadonlyArray<SlowRpcAckRequest> {
143 return useAtomValue(slowRpcAckRequestsAtom);