@peerbit/shared-log 16.0.34 → 16.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,119 @@
1
+ import { type DiagnosticValue, diagnosticNow } from "@peerbit/diagnostics";
2
+ import type { SyncProfileFn } from "./profile.js";
3
+
4
+ // Process-local correlation only, never a receipt, wire id or persistent key.
5
+ let nextTrace = 0;
6
+ const MAX_EVENTS = 256;
7
+ export const PERSISTED_DELIVERY_PROFILE_ENTRY_LIMIT = 16;
8
+
9
+ /** Bounded, invocation-owned diagnostics. No retained entries or peer objects. */
10
+ export const createPersistedDeliveryProfile = (
11
+ sink: SyncProfileFn | undefined,
12
+ entries: number,
13
+ minAcks: number,
14
+ leaderDegree: number,
15
+ ) => {
16
+ if (!sink) return undefined;
17
+ nextTrace = (nextTrace % Number.MAX_SAFE_INTEGER) + 1;
18
+ const traceId = `persisted:${nextTrace}`;
19
+ const startedAt = diagnosticNow();
20
+ let emitted = 0;
21
+ let dropped = 0;
22
+ let closed = false;
23
+ const send = (
24
+ name: string,
25
+ details: Record<string, DiagnosticValue>,
26
+ peer?: string,
27
+ phaseStartedAt = startedAt,
28
+ ) => {
29
+ try {
30
+ const now = diagnosticNow();
31
+ const result: unknown = sink({
32
+ name: `sharedLog.persistedDelivery.${name}`,
33
+ component: "shared-log",
34
+ traceId,
35
+ entries,
36
+ peer,
37
+ durationMs: Math.max(0, now - phaseStartedAt),
38
+ details: {
39
+ ...details,
40
+ v: 1,
41
+ minAcks,
42
+ leaderDegree,
43
+ entrySampleWindow: Math.min(
44
+ entries,
45
+ PERSISTED_DELIVERY_PROFILE_ENTRY_LIMIT,
46
+ ),
47
+ entriesOutsideSampleWindow: Math.max(
48
+ 0,
49
+ entries - PERSISTED_DELIVERY_PROFILE_ENTRY_LIMIT,
50
+ ),
51
+ elapsedMs: Math.max(0, now - startedAt),
52
+ },
53
+ });
54
+ // A void callback can still be async in TypeScript. Never await it, and
55
+ // own its rejection just as we isolate a synchronous diagnostic throw.
56
+ if (
57
+ result &&
58
+ typeof (result as PromiseLike<unknown>).then === "function"
59
+ ) {
60
+ void Promise.resolve(result).catch(() => undefined);
61
+ }
62
+ } catch {
63
+ // Diagnostics must not enter protocol recovery or replace its outcome.
64
+ }
65
+ };
66
+ const emit = (
67
+ name: string,
68
+ details: Record<string, DiagnosticValue>,
69
+ peer?: string,
70
+ phaseStartedAt?: number,
71
+ ) => {
72
+ if (closed) return;
73
+ if (emitted >= MAX_EVENTS) {
74
+ dropped = Math.min(Number.MAX_SAFE_INTEGER, dropped + 1);
75
+ return;
76
+ }
77
+ emitted++;
78
+ send(name, details, peer, phaseStartedAt);
79
+ };
80
+ return {
81
+ now: diagnosticNow,
82
+ emit,
83
+ phase: (
84
+ phase: string,
85
+ options: {
86
+ round: number;
87
+ peer: string;
88
+ requestedEntries: number;
89
+ attempt?: number;
90
+ timeoutMs?: number;
91
+ startedAt?: number;
92
+ },
93
+ ) => {
94
+ const { peer, startedAt = diagnosticNow(), ...details } = options;
95
+ emit(
96
+ "peerPhase",
97
+ { ...details, phase, edge: "start", outcome: "pending" },
98
+ peer,
99
+ startedAt,
100
+ );
101
+ return (outcome: string, acceptedEntries?: number) =>
102
+ emit(
103
+ "peerPhase",
104
+ { ...details, phase, edge: "end", outcome, acceptedEntries },
105
+ peer,
106
+ startedAt,
107
+ );
108
+ },
109
+ finish: (details: Record<string, DiagnosticValue>) => {
110
+ if (closed) return;
111
+ closed = true;
112
+ send("settle", {
113
+ ...details,
114
+ emittedEvents: emitted + 1,
115
+ droppedEvents: dropped,
116
+ });
117
+ },
118
+ };
119
+ };