|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 4 | +import { |
| 5 | + ApiError, |
| 6 | + type HammerStatus, |
| 7 | + type HammerWrite, |
| 8 | + getHammerStatus, |
| 9 | + hammerRun, |
| 10 | +} from "@/lib/api-client"; |
| 11 | + |
| 12 | +const TOTAL = 50; |
| 13 | + |
| 14 | +type Phase = "idle" | "dispatching" | "watching" | "done" | "error"; |
| 15 | + |
| 16 | +export function HammerDemo() { |
| 17 | + const [phase, setPhase] = useState<Phase>("idle"); |
| 18 | + const [runId, setRunId] = useState<string | null>(null); |
| 19 | + const [dispatchedAt, setDispatchedAt] = useState<number | null>(null); |
| 20 | + const [dispatchMs, setDispatchMs] = useState<number | null>(null); |
| 21 | + const [status, setStatus] = useState<HammerStatus | null>(null); |
| 22 | + const [error, setError] = useState<string | null>(null); |
| 23 | + const pollRef = useRef<ReturnType<typeof setInterval> | null>(null); |
| 24 | + |
| 25 | + const stopPolling = useCallback(() => { |
| 26 | + if (pollRef.current) { |
| 27 | + clearInterval(pollRef.current); |
| 28 | + pollRef.current = null; |
| 29 | + } |
| 30 | + }, []); |
| 31 | + |
| 32 | + useEffect(() => stopPolling, [stopPolling]); |
| 33 | + |
| 34 | + const start = useCallback(async () => { |
| 35 | + setError(null); |
| 36 | + setStatus(null); |
| 37 | + setRunId(null); |
| 38 | + setDispatchMs(null); |
| 39 | + setPhase("dispatching"); |
| 40 | + const t0 = performance.now(); |
| 41 | + try { |
| 42 | + const result = await hammerRun(TOTAL); |
| 43 | + const t1 = performance.now(); |
| 44 | + setRunId(result.runId); |
| 45 | + setDispatchedAt(Date.parse(result.dispatchedAt)); |
| 46 | + setDispatchMs(Math.round(t1 - t0)); |
| 47 | + setPhase("watching"); |
| 48 | + } catch (err) { |
| 49 | + setPhase("error"); |
| 50 | + if (err instanceof ApiError) { |
| 51 | + setError(err.message); |
| 52 | + } else if (err instanceof Error) { |
| 53 | + setError(err.message); |
| 54 | + } else { |
| 55 | + setError("request failed"); |
| 56 | + } |
| 57 | + } |
| 58 | + }, []); |
| 59 | + |
| 60 | + useEffect(() => { |
| 61 | + if (phase !== "watching" || !runId) return; |
| 62 | + let cancelled = false; |
| 63 | + const poll = async () => { |
| 64 | + try { |
| 65 | + const next = await getHammerStatus(runId); |
| 66 | + if (cancelled) return; |
| 67 | + setStatus(next); |
| 68 | + if (next.done) { |
| 69 | + stopPolling(); |
| 70 | + setPhase("done"); |
| 71 | + } |
| 72 | + } catch { |
| 73 | + // Swallow transient polling errors; next tick retries. |
| 74 | + } |
| 75 | + }; |
| 76 | + poll(); |
| 77 | + pollRef.current = setInterval(poll, 150); |
| 78 | + return () => { |
| 79 | + cancelled = true; |
| 80 | + stopPolling(); |
| 81 | + }; |
| 82 | + }, [phase, runId, stopPolling]); |
| 83 | + |
| 84 | + const tiles = buildTiles(status?.writes ?? []); |
| 85 | + const completedCount = tiles.filter((t) => t.kind === "filled").length; |
| 86 | + const spread = spreadMs(status?.writes ?? []); |
| 87 | + |
| 88 | + return ( |
| 89 | + <section |
| 90 | + className="px-[80px] py-[64px] border-b" |
| 91 | + style={{ borderColor: "#3d3838" }} |
| 92 | + > |
| 93 | + <h3 className="text-[16px] font-bold text-[#f2eded] mb-3"> |
| 94 | + See it for yourself — hammer the queue |
| 95 | + </h3> |
| 96 | + <p className="text-[#b8b2b2] mb-6 leading-[24px]"> |
| 97 | + <span className="text-[#716b6a]">[*]</span> Click the button. We fire{" "} |
| 98 | + {TOTAL} parallel writes at a synthetic sheet through the real pipeline — |
| 99 | + same advisory lock, same Redis stream, same ledger. Watch them land in |
| 100 | + order. |
| 101 | + </p> |
| 102 | + |
| 103 | + <div className="flex items-center gap-3 mb-4"> |
| 104 | + <button |
| 105 | + type="button" |
| 106 | + onClick={start} |
| 107 | + disabled={phase === "dispatching" || phase === "watching"} |
| 108 | + className="rounded px-6 py-2 font-medium transition-opacity hover:opacity-90 disabled:opacity-50" |
| 109 | + style={{ backgroundColor: "#f2eded", color: "#131010" }} |
| 110 | + > |
| 111 | + {phase === "dispatching" |
| 112 | + ? "dispatching…" |
| 113 | + : phase === "watching" |
| 114 | + ? "watching…" |
| 115 | + : phase === "done" |
| 116 | + ? `run again (${TOTAL} writes)` |
| 117 | + : `fire ${TOTAL} parallel writes`} |
| 118 | + </button> |
| 119 | + {dispatchMs !== null && ( |
| 120 | + <span className="text-xs text-[#7f7a7a]"> |
| 121 | + dispatched {TOTAL} writes in {dispatchMs}ms |
| 122 | + </span> |
| 123 | + )} |
| 124 | + </div> |
| 125 | + |
| 126 | + {error && ( |
| 127 | + <p className="text-sm mb-4" style={{ color: "#f87171" }}> |
| 128 | + [!] {error} |
| 129 | + </p> |
| 130 | + )} |
| 131 | + |
| 132 | + <div |
| 133 | + className="border rounded p-4" |
| 134 | + style={{ borderColor: "#3d3838", backgroundColor: "#131010" }} |
| 135 | + > |
| 136 | + <div className="grid grid-cols-10 gap-1"> |
| 137 | + {tiles.map((tile, i) => ( |
| 138 | + <div |
| 139 | + key={i} |
| 140 | + className="aspect-square border rounded flex items-center justify-center text-[10px] font-mono" |
| 141 | + style={{ |
| 142 | + borderColor: tile.kind === "filled" ? "#3d3838" : "#2a2626", |
| 143 | + backgroundColor: |
| 144 | + tile.kind === "filled" ? "#1b1818" : "#131010", |
| 145 | + color: tile.kind === "filled" ? "#f2eded" : "#4a4545", |
| 146 | + }} |
| 147 | + title={ |
| 148 | + tile.kind === "filled" |
| 149 | + ? `#${tile.arrival} · ordinal ${tile.ordinal} · ${tile.writeId.slice(0, 8)}` |
| 150 | + : "pending" |
| 151 | + } |
| 152 | + > |
| 153 | + {tile.kind === "filled" ? tile.arrival : "·"} |
| 154 | + </div> |
| 155 | + ))} |
| 156 | + </div> |
| 157 | + |
| 158 | + <div className="flex items-center justify-between mt-4 text-xs text-[#7f7a7a]"> |
| 159 | + <span> |
| 160 | + {completedCount}/{TOTAL} writes landed |
| 161 | + {spread !== null && ( |
| 162 | + <> |
| 163 | + {" "} |
| 164 | + · serialized over {spread}ms · zero collisions |
| 165 | + </> |
| 166 | + )} |
| 167 | + </span> |
| 168 | + {runId && ( |
| 169 | + <span> |
| 170 | + run <code className="text-[#b8b2b2]">{runId.slice(0, 8)}</code> |
| 171 | + </span> |
| 172 | + )} |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + |
| 176 | + <p className="text-xs text-[#7f7a7a] mt-3 leading-[18px]"> |
| 177 | + Numbers are the arrival order. Dispatched in parallel, processed one at |
| 178 | + a time per sheet — that's the whole product. |
| 179 | + </p> |
| 180 | + </section> |
| 181 | + ); |
| 182 | +} |
| 183 | + |
| 184 | +interface Tile { |
| 185 | + kind: "empty" | "filled"; |
| 186 | + arrival: number; |
| 187 | + ordinal: number; |
| 188 | + writeId: string; |
| 189 | +} |
| 190 | + |
| 191 | +function buildTiles(writes: HammerWrite[]): Tile[] { |
| 192 | + const tiles: Tile[] = Array.from({ length: TOTAL }, () => ({ |
| 193 | + kind: "empty", |
| 194 | + arrival: 0, |
| 195 | + ordinal: 0, |
| 196 | + writeId: "", |
| 197 | + })); |
| 198 | + const completed = writes.filter((w) => w.completedAt !== null); |
| 199 | + completed.forEach((w, i) => { |
| 200 | + if (i < TOTAL) { |
| 201 | + tiles[i] = { |
| 202 | + kind: "filled", |
| 203 | + arrival: i + 1, |
| 204 | + ordinal: w.ordinal, |
| 205 | + writeId: w.writeId, |
| 206 | + }; |
| 207 | + } |
| 208 | + }); |
| 209 | + return tiles; |
| 210 | +} |
| 211 | + |
| 212 | +function spreadMs(writes: HammerWrite[]): number | null { |
| 213 | + const completed = writes |
| 214 | + .filter((w) => w.completedAt !== null) |
| 215 | + .map((w) => Date.parse(w.completedAt as string)); |
| 216 | + if (completed.length < 2) return null; |
| 217 | + return Math.max(...completed) - Math.min(...completed); |
| 218 | +} |
0 commit comments