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

requestLatencyState.ts

142 lines4.2 KB

1import { WS_METHODS } from "@t3tools/contracts";
2import { Atom } from "effect/reactivity";
3​
4import { appAtomRegistry } from "./atomRegistry";
5​
6const SLOW_RPC_ACK_THRESHOLD_MS = 15_000;
7/**
8 * Updates wait for package installs; VCS refreshes may wait for background work and network calls.
9 * Give these requests longer than the normal 15s warning threshold.
10 */
11const LONG_RUNNING_RPC_ACK_THRESHOLD_MS = 120_000;
12const MAX_TRACKED_RPC_ACK_REQUESTS = 256;
13let slowRpcAckThresholdMs = SLOW_RPC_ACK_THRESHOLD_MS;
14​
15export interface SlowRpcAckRequest {
16 readonly requestId: string;
17 readonly startedAt: string;
18 readonly startedAtMs: number;
19 readonly tag: string;
20 readonly thresholdMs: number;
22​
23interface PendingRpcAckRequest {
24 readonly request: SlowRpcAckRequest;
25 readonly timeoutId: ReturnType<typeof setTimeout>;
27​
28const pendingRpcAckRequests = new Map<string, PendingRpcAckRequest>();
29// Background reports nobody waits on. A busy host delays them first, and warning
30// about a request the user never made does not say what is wrong.
31const untrackedRpcAckMethods = new Set<string>([
32 WS_METHODS.serverGetUsageSummary,
33 WS_METHODS.serverReportClientActivity,
34]);
35const longRunningRpcAckMethods = new Set<string>([
36 WS_METHODS.serverUpdateProvider,
37 WS_METHODS.serverRefreshProviders,
38 WS_METHODS.serverUpdateServer,
39 WS_METHODS.vcsRefreshStatus,
40]);
41​
42const slowRpcAckRequestsAtom = Atom.make<ReadonlyArray<SlowRpcAckRequest>>([]).pipe(
43 Atom.keepAlive,
44 Atom.withLabel("slow-rpc-ack-requests"),
45);
46​
47function setSlowRpcAckRequests(requests: ReadonlyArray<SlowRpcAckRequest>) {
48 appAtomRegistry.set(slowRpcAckRequestsAtom, [...requests]);
50​
51function getSlowRpcAckRequestsValue(): ReadonlyArray<SlowRpcAckRequest> {
52 return appAtomRegistry.get(slowRpcAckRequestsAtom);
54​
55function shouldTrackRpcAck(method: string): boolean {
56 return (
57 !method.includes("subscribe") &&
58 !method.startsWith("pullRequests.") &&
59 !untrackedRpcAckMethods.has(method)
60 );
62​
63function rpcAckThresholdMs(method: string): number {
64 return longRunningRpcAckMethods.has(method)
65 ? Math.max(slowRpcAckThresholdMs, LONG_RUNNING_RPC_ACK_THRESHOLD_MS)
66 : slowRpcAckThresholdMs;
68​
69export function trackRpcRequestSent(requestId: string, method: string, tag = method): void {
70 if (!shouldTrackRpcAck(method)) {
71 return;
72 }
73​
74 clearTrackedRpcRequest(requestId);
75 evictOldestPendingRpcRequestIfNeeded();
76​
77 const startedAtMs = Date.now();
78 const thresholdMs = rpcAckThresholdMs(method);
79 const request: SlowRpcAckRequest = {
80 requestId,
81 startedAt: new Date(startedAtMs).toISOString(),
82 startedAtMs,
83 tag,
84 thresholdMs,
85 };
86 const timeoutId = setTimeout(() => {
87 pendingRpcAckRequests.delete(requestId);
88 appendSlowRpcAckRequest(request);
89 }, thresholdMs);
90​
91 pendingRpcAckRequests.set(requestId, {
92 request,
93 timeoutId,
94 });
96​
97export function acknowledgeRpcRequest(requestId: string): void {
98 clearTrackedRpcRequest(requestId);
99 const slowRequests = getSlowRpcAckRequestsValue();
100 if (!slowRequests.some((request) => request.requestId === requestId)) {
101 return;
102 }
103​
104 setSlowRpcAckRequests(slowRequests.filter((request) => request.requestId !== requestId));
106​
107function clearTrackedRpcRequest(requestId: string): void {
108 const pending = pendingRpcAckRequests.get(requestId);
109 if (!pending) {
110 return;
111 }
112​
113 clearTimeout(pending.timeoutId);
114 pendingRpcAckRequests.delete(requestId);
116​
117function appendSlowRpcAckRequest(request: SlowRpcAckRequest): void {
118 const requests = [...getSlowRpcAckRequestsValue(), request];
119 if (requests.length <= MAX_TRACKED_RPC_ACK_REQUESTS) {
120 setSlowRpcAckRequests(requests);
121 return;
122 }
123​
124 setSlowRpcAckRequests(requests.slice(-MAX_TRACKED_RPC_ACK_REQUESTS));
126​
127function evictOldestPendingRpcRequestIfNeeded(): void {
128 while (pendingRpcAckRequests.size >= MAX_TRACKED_RPC_ACK_REQUESTS) {
129 const oldestRequestId = pendingRpcAckRequests.keys().next().value;
130 if (oldestRequestId === undefined) {
131 return;
132 }
133​
134 clearTrackedRpcRequest(oldestRequestId);
135 }
137​
138export function subscribeSlowRpcAckRequests(
139 listener: (requests: ReadonlyArray<SlowRpcAckRequest>) => void,
140): () => void {
141 return appAtomRegistry.subscribe(slowRpcAckRequestsAtom, listener);