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

requestLatencyState.ts

153 lines4.5 KB

1import { useAtomValue } from "@effect/atom-react";
2import { WS_METHODS } from "@t3tools/contracts";
3import { Atom } from "effect/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;
24​
25interface PendingRpcAckRequest {
26 readonly request: SlowRpcAckRequest;
27 readonly timeoutId: ReturnType<typeof setTimeout>;
29​
30const pendingRpcAckRequests = new Map<string, PendingRpcAckRequest>();
31const untrackedRpcAckMethods = new Set<string>([WS_METHODS.serverGetUsageSummary]);
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]);
46​
47function getSlowRpcAckRequestsValue(): ReadonlyArray<SlowRpcAckRequest> {
48 return appAtomRegistry.get(slowRpcAckRequestsAtom);
50​
51function shouldTrackRpcAck(method: string): boolean {
52 return (
53 !method.includes("subscribe") &&
54 !method.startsWith("pullRequests.") &&
55 !untrackedRpcAckMethods.has(method)
56 );
58​
59function rpcAckThresholdMs(method: string): number {
60 return longRunningRpcAckMethods.has(method)
61 ? Math.max(slowRpcAckThresholdMs, LONG_RUNNING_RPC_ACK_THRESHOLD_MS)
62 : slowRpcAckThresholdMs;
64​
65export function getSlowRpcAckRequests(): ReadonlyArray<SlowRpcAckRequest> {
66 return getSlowRpcAckRequestsValue();
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 clearAllTrackedRpcRequests(): void {
108 for (const pending of pendingRpcAckRequests.values()) {
109 clearTimeout(pending.timeoutId);
110 }
111 pendingRpcAckRequests.clear();
112 setSlowRpcAckRequests([]);
114​
115function clearTrackedRpcRequest(requestId: string): void {
116 const pending = pendingRpcAckRequests.get(requestId);
117 if (!pending) {
118 return;
119 }
120​
121 clearTimeout(pending.timeoutId);
122 pendingRpcAckRequests.delete(requestId);
124​
125function appendSlowRpcAckRequest(request: SlowRpcAckRequest): void {
126 const requests = [...getSlowRpcAckRequestsValue(), request];
127 if (requests.length <= MAX_TRACKED_RPC_ACK_REQUESTS) {
128 setSlowRpcAckRequests(requests);
129 return;
130 }
131​
132 setSlowRpcAckRequests(requests.slice(-MAX_TRACKED_RPC_ACK_REQUESTS));
134​
135function evictOldestPendingRpcRequestIfNeeded(): void {
136 while (pendingRpcAckRequests.size >= MAX_TRACKED_RPC_ACK_REQUESTS) {
137 const oldestRequestId = pendingRpcAckRequests.keys().next().value;
138 if (oldestRequestId === undefined) {
139 return;
140 }
141​
142 clearTrackedRpcRequest(oldestRequestId);
143 }
145​
146export function resetRequestLatencyStateForTests(): void {
147 slowRpcAckThresholdMs = SLOW_RPC_ACK_THRESHOLD_MS;
148 clearAllTrackedRpcRequests();
150​
151export function useSlowRpcAckRequests(): ReadonlyArray<SlowRpcAckRequest> {
152 return useAtomValue(slowRpcAckRequestsAtom);