|
1 | 1 | /** |
2 | | - * Tracks state transitions during iteration loops for diagnostic purposes. |
3 | | - * Used by circuit breakers to report where iterations were spent when limits are exceeded. |
| 2 | + * Creates a simple iteration counter with a limit check. |
| 3 | + * When the limit is exceeded, calls the provided diagnostic function to capture state. |
4 | 4 | * |
5 | | - * The tracker collects a history of state transitions, where each entry records |
6 | | - * a period of time (iteration range) spent in a particular state. When the iteration |
7 | | - * limit is exceeded, this history helps diagnose infinite loop causes. |
| 5 | + * This design avoids per-iteration overhead - state capture only happens when needed. |
8 | 6 | * |
9 | 7 | * @example |
10 | 8 | * ```ts |
11 | | - * const tracker = createIterationTracker<{ operators: string }>(100000) |
| 9 | + * const checkLimit = createIterationLimitChecker(100000) |
12 | 10 | * |
13 | 11 | * while (pendingWork()) { |
14 | | - * const state = { operators: getOperatorsWithWork().join(',') } |
15 | | - * if (tracker.trackAndCheckLimit(state)) { |
16 | | - * console.warn(tracker.formatWarning('D2 graph execution', { |
17 | | - * totalOperators: operators.length, |
18 | | - * })) |
| 12 | + * if (checkLimit(() => ({ |
| 13 | + * context: 'D2 graph execution', |
| 14 | + * diagnostics: { totalOperators: operators.length } |
| 15 | + * }))) { |
19 | 16 | * break |
20 | 17 | * } |
21 | 18 | * step() |
22 | 19 | * } |
23 | 20 | * ``` |
24 | 21 | */ |
25 | 22 |
|
26 | | -export type StateHistoryEntry<TState> = { |
27 | | - state: TState |
28 | | - startIter: number |
29 | | - endIter: number |
30 | | -} |
31 | | - |
32 | | -export type IterationTracker<TState> = { |
33 | | - /** |
34 | | - * Records the current state and increments the iteration counter. |
35 | | - * Returns true if the iteration limit has been exceeded. |
36 | | - */ |
37 | | - trackAndCheckLimit: (state: TState) => boolean |
38 | | - |
39 | | - /** |
40 | | - * Formats a warning message with iteration breakdown and diagnostic info. |
41 | | - * Call this after trackAndCheckLimit returns true. |
42 | | - */ |
43 | | - formatWarning: ( |
44 | | - context: string, |
45 | | - diagnosticInfo?: Record<string, unknown>, |
46 | | - ) => string |
47 | | - |
48 | | - /** |
49 | | - * Returns the current iteration count. |
50 | | - */ |
51 | | - getIterations: () => number |
52 | | - |
53 | | - /** |
54 | | - * Returns the state history for inspection. |
55 | | - */ |
56 | | - getHistory: () => Array<StateHistoryEntry<TState>> |
| 23 | +export type LimitExceededInfo = { |
| 24 | + context: string |
| 25 | + diagnostics?: Record<string, unknown> |
57 | 26 | } |
58 | 27 |
|
59 | 28 | /** |
60 | | - * Creates an iteration tracker that monitors loop iterations and records state transitions. |
| 29 | + * Creates an iteration limit checker that logs a warning when the limit is exceeded. |
61 | 30 | * |
62 | 31 | * @param maxIterations - The maximum number of iterations before the limit is exceeded |
63 | | - * @param stateToKey - Optional function to convert state to a string key for comparison. |
64 | | - * Defaults to JSON.stringify. |
| 32 | + * @returns A function that increments the counter and returns true if limit exceeded |
65 | 33 | */ |
66 | | -export function createIterationTracker<TState>( |
| 34 | +export function createIterationLimitChecker( |
67 | 35 | maxIterations: number, |
68 | | - stateToKey: (state: TState) => string = (state) => JSON.stringify(state), |
69 | | -): IterationTracker<TState> { |
70 | | - const history: Array<StateHistoryEntry<TState>> = [] |
71 | | - let currentStateKey: string | null = null |
72 | | - let currentState: TState | null = null |
73 | | - let stateStartIter = 1 |
| 36 | +): (getInfo: () => LimitExceededInfo) => boolean { |
74 | 37 | let iterations = 0 |
75 | 38 |
|
76 | | - function recordCurrentState(): void { |
77 | | - if (currentStateKey !== null && currentState !== null) { |
78 | | - history.push({ |
79 | | - state: currentState, |
80 | | - startIter: stateStartIter, |
81 | | - endIter: iterations, |
82 | | - }) |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - function trackAndCheckLimit(state: TState): boolean { |
87 | | - const stateKey = stateToKey(state) |
88 | | - |
89 | | - if (stateKey !== currentStateKey) { |
90 | | - recordCurrentState() |
91 | | - currentStateKey = stateKey |
92 | | - currentState = state |
93 | | - stateStartIter = iterations + 1 |
94 | | - } |
95 | | - |
| 39 | + return function checkLimit(getInfo: () => LimitExceededInfo): boolean { |
96 | 40 | iterations++ |
97 | 41 |
|
98 | 42 | if (iterations > maxIterations) { |
99 | | - recordCurrentState() |
| 43 | + // Only capture diagnostic info when we actually exceed the limit |
| 44 | + const { context, diagnostics } = getInfo() |
| 45 | + |
| 46 | + const diagnosticSection = diagnostics |
| 47 | + ? `\nDiagnostic info: ${JSON.stringify(diagnostics, null, 2)}\n` |
| 48 | + : `\n` |
| 49 | + |
| 50 | + console.warn( |
| 51 | + `[TanStack DB] ${context} exceeded ${maxIterations} iterations. ` + |
| 52 | + `Continuing with available data.` + |
| 53 | + diagnosticSection + |
| 54 | + `Please report this issue at https://github.com/TanStack/db/issues`, |
| 55 | + ) |
100 | 56 | return true |
101 | 57 | } |
102 | 58 |
|
103 | 59 | return false |
104 | 60 | } |
105 | | - |
106 | | - function formatWarning( |
107 | | - context: string, |
108 | | - diagnosticInfo?: Record<string, unknown>, |
109 | | - ): string { |
110 | | - const iterationBreakdown = history |
111 | | - .map((h) => ` ${h.startIter}-${h.endIter}: ${stateToKey(h.state)}`) |
112 | | - .join(`\n`) |
113 | | - |
114 | | - const diagnosticSection = diagnosticInfo |
115 | | - ? `\nDiagnostic info: ${JSON.stringify(diagnosticInfo, null, 2)}\n` |
116 | | - : `\n` |
117 | | - |
118 | | - return ( |
119 | | - `[TanStack DB] ${context} exceeded ${maxIterations} iterations. ` + |
120 | | - `Continuing with available data.\n` + |
121 | | - `Iteration breakdown (where the loop spent time):\n${iterationBreakdown}` + |
122 | | - diagnosticSection + |
123 | | - `Please report this issue at https://github.com/TanStack/db/issues` |
124 | | - ) |
125 | | - } |
126 | | - |
127 | | - function getIterations(): number { |
128 | | - return iterations |
129 | | - } |
130 | | - |
131 | | - function getHistory(): Array<StateHistoryEntry<TState>> { |
132 | | - return [...history] |
133 | | - } |
134 | | - |
135 | | - return { |
136 | | - trackAndCheckLimit, |
137 | | - formatWarning, |
138 | | - getIterations, |
139 | | - getHistory, |
140 | | - } |
141 | 61 | } |
0 commit comments