Skip to content

Commit 63a474d

Browse files
chore: web worker shreds cache
1 parent 80bc04c commit 63a474d

16 files changed

Lines changed: 1186 additions & 964 deletions

File tree

src/api/useSetAtomWsData.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import type {
77
FromWorkerMessage,
88
HistoryArrayKey,
99
KeyedValuesWithHistory,
10+
LiveShredsItem,
1011
WsEntity,
1112
} from "./worker/types";
12-
import { isEmaObjectKey } from "./worker/types";
13+
import { isEmaObjectKey, liveShredsKey } from "./worker/types";
1314
import { DateTime } from "luxon";
1415
import { useInterval } from "react-use";
1516
import { useThrottledCallback, useDebouncedCallback } from "use-debounce";
@@ -36,7 +37,7 @@ import {
3637
deleteSupermajorityDeltaEntriesAtom,
3738
resetSupermajorityAtom,
3839
} from "../atoms";
39-
import { shredsAtoms } from "../features/Overview/ShredsProgression/atoms";
40+
import { liveShredsDataAtom } from "../features/Overview/ShredsProgression/atoms";
4041
import { rateLiveWaterfallAtom } from "../features/Overview/SlotPerformance/atoms";
4142
import {
4243
addTurbineSlotsAtom,
@@ -118,12 +119,11 @@ import type {
118119
PeerRemove,
119120
} from "./types";
120121
import { SocketState } from "./ws/types";
121-
import { xRangeMs } from "../features/Overview/ShredsProgression/const";
122+
import { useServerMessages } from "./ws/utils";
122123
import {
123-
bootProgressPhaseAtom,
124124
showStartupProgressAtom,
125+
bootProgressPhaseAtom,
125126
} from "../features/StartupProgress/atoms";
126-
import { useServerMessages } from "./ws/utils";
127127

128128
export function useSetAtomWsData() {
129129
const setSocketState = useSetAtom(socketStateAtom);
@@ -169,6 +169,18 @@ export function useSetAtomWsData() {
169169
[setTileTimerHistory],
170170
);
171171

172+
const setLiveShreds = useSetAtom(liveShredsDataAtom);
173+
const updateLiveShredsObject = useCallback(
174+
({ key, data }: LiveShredsItem) => {
175+
switch (key) {
176+
case liveShredsKey:
177+
setLiveShreds(data);
178+
break;
179+
}
180+
},
181+
[setLiveShreds],
182+
);
183+
172184
const onMessage = useCallback(
173185
(msg: FromWorkerMessage) => {
174186
switch (msg.type) {
@@ -207,6 +219,11 @@ export function useSetAtomWsData() {
207219
updateEmaHistoryObject(item);
208220
}
209221
break;
222+
case "liveShredsObject":
223+
for (const item of msg.items) {
224+
updateLiveShredsObject(item);
225+
}
226+
break;
210227
}
211228
},
212229
[
@@ -215,6 +232,7 @@ export function useSetAtomWsData() {
215232
updateEmaHistoryArray,
216233
updateHistoryArray,
217234
updateEmaHistoryObject,
235+
updateLiveShredsObject,
218236
],
219237
);
220238

@@ -430,8 +448,6 @@ function useUpdateAtoms() {
430448
);
431449
const setSlotCaughtUp = useSetAtom(slotCaughtUpAtom);
432450

433-
const addLiveShreds = useSetAtom(shredsAtoms.addShredEvents);
434-
435451
const setLiveProgramCache = useSetAtom(liveProgramCacheAtom);
436452

437453
const peersBuffer = useRef(new Map<string, Peer>());
@@ -717,7 +733,7 @@ function useUpdateAtoms() {
717733
break;
718734
}
719735
case "live_shreds": {
720-
addLiveShreds(value);
736+
// processed by web worker
721737
break;
722738
}
723739
case "late_votes_history": {
@@ -755,7 +771,6 @@ function useUpdateAtoms() {
755771
}
756772
},
757773
[
758-
addLiveShreds,
759774
addRepairSlot,
760775
addRepairSlots,
761776
addSkippedClusterSlots,
@@ -847,14 +862,6 @@ function useUpdateAtoms() {
847862
const isSocketDisconnected =
848863
useAtomValue(socketStateAtom) === SocketState.Disconnected;
849864

850-
const deleteLiveShreds = useSetAtom(shredsAtoms.deleteSlots);
851-
852-
useEffect(() => {
853-
if (isSocketDisconnected) {
854-
deleteLiveShreds(isSocketDisconnected, isStartup);
855-
}
856-
}, [deleteLiveShreds, isSocketDisconnected, isStartup]);
857-
858865
const resetTurbineSlots = useSetAtom(resetTurbineSlotsAtom);
859866
const resetRepairSlots = useSetAtom(resetRepairSlotsAtom);
860867
useEffect(() => {
@@ -864,13 +871,6 @@ function useUpdateAtoms() {
864871
}
865872
}, [isStartup, resetRepairSlots, resetTurbineSlots]);
866873

867-
useInterval(
868-
() => {
869-
deleteLiveShreds(isSocketDisconnected, isStartup);
870-
},
871-
isStartup ? 1_000 : xRangeMs / 4,
872-
);
873-
874874
const deleteSupermajorityDeltaEntries = useSetAtom(
875875
deleteSupermajorityDeltaEntriesAtom,
876876
);

src/api/worker/cache/batchPublisher.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface BatchPublisherConfig<
2222
/** Sends the collected batch of messages to the main thread. */
2323
post: (items: TMessage[]) => void;
2424
onReset?: (entry: TEntry) => void;
25+
onStop?: (entry: TEntry) => void;
2526
}
2627

2728
export function createBatchPublisher<
@@ -45,6 +46,24 @@ export function createBatchPublisher<
4546
return e.lastPublishMs + e.publishIntervalMs;
4647
}
4748

49+
function collectAndPost(entries: Iterable<TEntry>, isForced: boolean) {
50+
const nowMs = performance.now();
51+
const batch: TMessage[] = [];
52+
for (const e of entries) {
53+
if (!isForced && (!e.subscribed || nowMs < nextDueAt(e))) {
54+
continue;
55+
}
56+
const item = config.collect(e, nowMs);
57+
if (item !== undefined) {
58+
batch.push(item);
59+
}
60+
e.lastPublishMs = nowMs;
61+
}
62+
if (batch.length) {
63+
config.post(batch);
64+
}
65+
}
66+
4867
function schedule() {
4968
if (timer) return;
5069

@@ -61,23 +80,7 @@ export function createBatchPublisher<
6180
timer = setTimeout(
6281
() => {
6382
timer = undefined;
64-
const nowMs = performance.now();
65-
const batch: TMessage[] = [];
66-
67-
for (const e of entries.values()) {
68-
if (!e.subscribed) continue;
69-
if (nowMs < nextDueAt(e)) continue;
70-
71-
const item = config.collect(e, nowMs);
72-
if (item !== undefined) {
73-
batch.push(item);
74-
}
75-
e.lastPublishMs = nowMs;
76-
}
77-
78-
if (batch.length) {
79-
config.post(batch);
80-
}
83+
collectAndPost(entries.values(), false);
8184
schedule();
8285
},
8386
Math.max(0, nextDue),
@@ -114,6 +117,12 @@ export function createBatchPublisher<
114117
return entries.get(key);
115118
},
116119

120+
publishNow: (key: TEntry["key"]) => {
121+
const e = entries.get(key);
122+
if (!e) return;
123+
collectAndPost([e], true);
124+
},
125+
117126
reset: (key?: TEntry["key"]) => {
118127
const toReset = key
119128
? [entries.get(key)].filter(isDefined)
@@ -133,6 +142,7 @@ export function createBatchPublisher<
133142

134143
for (const e of entries.values()) {
135144
e.subscribed = false;
145+
config.onStop?.(e);
136146
}
137147
},
138148
};

src/api/worker/cache/consts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export const gossipHealthHistoryBufferMs = 5_000;
55
export const overviewPublishIntervalMs = 500;
66
export const overviewRenderWindowMs = 60_000;
77
export const overviewHistoryBufferMs = 5_000;
8+
9+
export const shredsPublishIntervalMs = 50;

0 commit comments

Comments
 (0)