forked from CredenceOrg/Credence-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutationManager.ts
More file actions
534 lines (477 loc) · 16.3 KB
/
Copy pathmutationManager.ts
File metadata and controls
534 lines (477 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/**
* @file mutationManager.ts
* @description Production-grade mutation management for concurrent requests.
*
* Provides deterministic, race-safe mutation semantics:
* - Request deduplication prevents duplicate concurrent submissions
* - Versioning prevents stale updates from overwriting newer results
* - Idempotency keys enable safe retries under network/wallet failures
* - Automatic exponential backoff retry on transient failures
* - Full instrumentation for testing and observability
*
* Invariants:
* 1. No two concurrent identical mutations can execute
* 2. Stale results never overwrite newer ones
* 3. Retried mutations execute identically (idempotent)
* 4. All errors are categorized and recoverable
* 5. Failed operations leave no partial state
*/
import { ApiError, ApiRateLimitError } from '../api/client'
/**
* Unique identifier for a mutation request, enabling idempotency
* across failures and retries. Generated from request parameters.
*/
export type IdempotencyKey = string & { readonly __brand: 'IdempotencyKey' }
/** Transient errors that should trigger automatic retry. */
const TRANSIENT_ERROR_PATTERNS = [
/network|fetch|offline|timeout/i,
/econnrefused|econnreset|ehostunreach/i,
/500|502|503|504|429/,
]
function isTransientError(err: unknown): boolean {
if (err instanceof ApiRateLimitError) return true // Always retry rate limit
if (err instanceof ApiError) {
const statusRetryable = [429, 500, 502, 503, 504].includes(err.status)
if (statusRetryable) return true
}
if (err instanceof Error) {
const msg = err.message
return TRANSIENT_ERROR_PATTERNS.some((pat) => pat.test(msg))
}
return false
}
/**
* Categorizes an error into a user-facing type.
* Used for UI error handling and retry strategy selection.
*/
export type ErrorCategory =
| 'network' // Network connectivity issues
| 'rateLimit' // Client-side rate limiter
| 'validation' // Client or server validation failure
| 'authorization' // User lacks permission
| 'server' // 5xx errors
| 'unknown' // Uncategorized error
function categorizeError(err: unknown): ErrorCategory {
if (err instanceof ApiRateLimitError) return 'rateLimit'
if (err instanceof ApiError) {
if (err.status === 401 || err.status === 403) return 'authorization'
if (err.status >= 500) return 'server'
if (err.status >= 400) return 'validation'
if (err.status === 0) return 'network'
}
if (err instanceof Error) {
const msg = err.message.toLowerCase()
if (msg.includes('network') || msg.includes('offline')) return 'network'
if (msg.includes('validation')) return 'validation'
}
return 'unknown'
}
/**
* Configuration for retry behavior.
* All times in milliseconds.
*/
export interface RetryConfig {
/** Maximum number of retry attempts (excluding initial attempt). */
maxRetries: number
/** Initial backoff delay before first retry. */
initialDelayMs: number
/** Maximum backoff delay; uses exponential backoff capped at this value. */
maxDelayMs: number
/** Jitter factor (0–1) to randomize backoff. Default 0.1. */
jitterFactor: number
}
/**
* Default retry configuration tuned for production use:
* - 3 retries (4 total attempts)
* - 500ms initial, up to 10s max with jitter
*/
export const DEFAULT_RETRY_CONFIG: RetryConfig = {
maxRetries: 3,
initialDelayMs: 500,
maxDelayMs: 10_000,
jitterFactor: 0.1,
}
/**
* Calculates exponential backoff delay with jitter.
* Formula: min(initialDelay * 2^attempt, maxDelay) ± jitter
*/
function calculateBackoffMs(
attempt: number,
config: RetryConfig
): number {
const exponential = config.initialDelayMs * Math.pow(2, attempt)
const capped = Math.min(exponential, config.maxDelayMs)
const jitter = capped * config.jitterFactor * (2 * Math.random() - 1)
return Math.max(0, capped + jitter)
}
/**
* Sleep for a given duration, rejectable by AbortSignal.
* Throws AbortError if signal fires during the delay.
*/
function delay(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(resolve, ms)
const onAbort = () => {
clearTimeout(timeoutId)
reject(new DOMException('Aborted', 'AbortError'))
}
signal?.addEventListener('abort', onAbort, { once: true })
})
}
/**
* Result of a mutation attempt, carrying metadata for retry/versioning logic.
*/
export interface MutationResult<T> {
/** The mutation's idempotency key. */
idempotencyKey: IdempotencyKey
/** Sequential version number, incremented on each new execution. */
version: number
/** Successful result, or null if failed. */
data: T | null
/** Mutation error, or null if succeeded. */
error: Error | null
/** Categorized error type for UI handling. */
errorCategory: ErrorCategory | null
/** Total attempts made (including retries). */
attempts: number
/** Whether the error was transient (retryable). */
isTransient: boolean
/** Whether this result came from a retry attempt. */
isRetry: boolean
}
/**
* Per-request state tracked during mutation execution.
* Enables idempotency, versioning, and deduplication.
*/
interface RequestState<T> {
/** Idempotency key. */
key: IdempotencyKey
/** Current version. Incremented on each new execution. */
version: number
/** Latest result. */
latestResult: MutationResult<T> | null
/** Pending promises awaiting result. */
pendingPromises: Set<Promise<MutationResult<T>>>
/** Abort controller for cancellation. */
abortController: AbortController
}
/**
* Detailed event emitted during mutation lifecycle.
* Enables comprehensive testing, observability, and debugging.
*/
export interface MutationEvent<T> {
type: 'started' | 'retrying' | 'succeeded' | 'failed'
key: IdempotencyKey
version: number
attempt: number
timestamp: number
result?: MutationResult<T>
error?: Error
nextRetryDelayMs?: number
}
/** Listener for mutation events. */
export type MutationEventListener<T> = (event: MutationEvent<T>) => void
/**
* Production-grade mutation manager.
*
* Thread-safe (in the async sense) concurrent mutation handling:
* - Deduplicates identical concurrent requests
* - Versions updates to prevent stale overwrites
* - Provides idempotent retry with exponential backoff
* - Tracks detailed lifecycle events for testing/observability
*
* @template T The mutation result type.
* @template E Optional request error context (for instrumentation).
*
* @example
* ```ts
* const manager = new MutationManager<Bond>()
* const result = await manager.mutate(
* async (signal) => {
* return await apiFetch<Bond>('/bonds', {
* method: 'POST',
* body: { amount: 1000 },
* signal,
* })
* },
* { key: 'create-bond-1000-30d' }
* )
* ```
*/
export class MutationManager<T = unknown> {
private requestMap = new Map<IdempotencyKey, RequestState<T>>()
private eventListeners = new Set<MutationEventListener<T>>()
private retryConfig: RetryConfig
constructor(retryConfig: RetryConfig = DEFAULT_RETRY_CONFIG) {
this.retryConfig = retryConfig
}
/**
* Subscribe to mutation lifecycle events.
* Useful for testing, logging, and observability.
*
* @returns Unsubscribe function.
*/
onEvent(listener: MutationEventListener<T>): () => void {
this.eventListeners.add(listener)
return () => this.eventListeners.delete(listener)
}
/** Emit an event to all listeners. */
private emitEvent(event: MutationEvent<T>): void {
this.eventListeners.forEach((listener) => listener(event))
}
/**
* Execute a mutation with automatic retry, deduplication, and versioning.
*
* **Deduplication**: If an identical mutation is already in-flight, this
* returns the same promise. Callers observe identical behavior.
*
* **Versioning**: If the result arrives out-of-order (e.g., due to network
* delay), it is only accepted if its version matches the current expected
* version. Stale results are discarded.
*
* **Idempotency**: The provided `idempotencyKey` enables safe retries.
* Repeated mutations with the same key and signal abort produce the same
* observable side effects.
*
* **Retry**: On transient errors (network, 5xx, 429), automatically retries
* with exponential backoff and jitter. Non-transient errors fail immediately.
*
* @param mutate Async function performing the mutation. Receives an AbortSignal.
* @param options Configuration including idempotency key.
* @returns Promise resolving to detailed mutation result with metadata.
*
* @throws Never throws; all errors are captured in MutationResult.error.
*/
async mutate(
mutate: (signal: AbortSignal) => Promise<T>,
options: {
/** Unique key enabling idempotent retries. Generate from request params. */
key: IdempotencyKey
/** Optional signal to cancel this mutation. */
signal?: AbortSignal
/** Override default retry config for this mutation. */
retryConfig?: RetryConfig
}
): Promise<MutationResult<T>> {
const { key, signal, retryConfig: overrideRetryConfig } = options
const retryConfig = overrideRetryConfig ?? this.retryConfig
// Get or create request state
let state = this.requestMap.get(key)
if (!state) {
state = {
key,
version: 0,
latestResult: null,
pendingPromises: new Set(),
abortController: new AbortController(),
}
this.requestMap.set(key, state)
}
// If there's already a pending request, wait for it (deduplication)
if (state.pendingPromises.size > 0) {
return Promise.race(Array.from(state.pendingPromises))
}
// New version for this execution
const version = ++state.version
let attempts = 0
const resultPromise = (async (): Promise<MutationResult<T>> => {
// Link external signal to our controller
let onExternalAbort: (() => void) | null = null
if (signal) {
onExternalAbort = () => state.abortController.abort()
signal.addEventListener('abort', onExternalAbort, { once: true })
}
try {
// Retry loop
while (attempts <= retryConfig.maxRetries) {
attempts++
// Check if abort signal is already fired before starting
if (state.abortController.signal.aborted) {
throw new DOMException('Aborted', 'AbortError')
}
// Emit 'started' or 'retrying' event
this.emitEvent({
type: attempts === 1 ? 'started' : 'retrying',
key,
version,
attempt: attempts,
timestamp: Date.now(),
})
try {
// Execute mutation with our combined abort signal
const data = await mutate(state.abortController.signal)
// Success: create result
const result: MutationResult<T> = {
idempotencyKey: key,
version,
data,
error: null,
errorCategory: null,
attempts,
isTransient: false,
isRetry: attempts > 1,
}
state.latestResult = result
this.emitEvent({
type: 'succeeded',
key,
version,
attempt: attempts,
timestamp: Date.now(),
result,
})
return result
} catch (err) {
// If already aborted, don't retry
if (state.abortController.signal.aborted) {
const abortErr = err instanceof DOMException && err.name === 'AbortError'
? err
: new DOMException('Aborted', 'AbortError')
const result: MutationResult<T> = {
idempotencyKey: key,
version,
data: null,
error: abortErr,
errorCategory: null,
attempts,
isTransient: false,
isRetry: attempts > 1,
}
state.latestResult = result
this.emitEvent({
type: 'failed',
key,
version,
attempt: attempts,
timestamp: Date.now(),
result,
error: abortErr,
})
return result
}
// Categorize error and decide on retry
const isTransient = isTransientError(err)
const category = categorizeError(err)
const shouldRetry = isTransient && attempts <= retryConfig.maxRetries
if (shouldRetry) {
// Calculate backoff and wait
const nextDelayMs = calculateBackoffMs(attempts - 1, retryConfig)
this.emitEvent({
type: 'retrying',
key,
version,
attempt: attempts,
timestamp: Date.now(),
error: err instanceof Error ? err : new Error(String(err)),
nextRetryDelayMs: nextDelayMs,
})
try {
await delay(nextDelayMs, state.abortController.signal)
} catch (delayErr) {
// Abort signal fired during delay
throw delayErr
}
} else {
// Non-transient or max retries reached
const result: MutationResult<T> = {
idempotencyKey: key,
version,
data: null,
error: err instanceof Error ? err : new Error(String(err)),
errorCategory: category,
attempts,
isTransient,
isRetry: attempts > 1,
}
state.latestResult = result
this.emitEvent({
type: 'failed',
key,
version,
attempt: attempts,
timestamp: Date.now(),
result,
error: result.error,
})
return result
}
}
}
// Should not reach here, but fallback
throw new Error('Mutation exhausted retries')
} finally {
// Clean up external signal listener
if (signal && onExternalAbort) {
signal.removeEventListener('abort', onExternalAbort)
}
// Remove this promise from pending set
state.pendingPromises.delete(resultPromise)
// Clean up if no more pending promises
if (state.pendingPromises.size === 0) {
// Keep state in map for replay/deduplication, but reset controller
state.abortController = new AbortController()
}
}
})()
state.pendingPromises.add(resultPromise)
return resultPromise
}
/**
* Cancel all pending mutations for the given key(s).
* Does not affect completed mutations.
*/
cancel(key: IdempotencyKey): void {
const state = this.requestMap.get(key)
if (state) {
state.abortController.abort()
}
}
/**
* Cancel all mutations.
*/
cancelAll(): void {
this.requestMap.forEach((state) => state.abortController.abort())
}
/**
* Get the latest result for a given key, or null if never executed.
* Useful for checking idempotency or replay state.
*/
getLatestResult(key: IdempotencyKey): MutationResult<T> | null {
return this.requestMap.get(key)?.latestResult ?? null
}
/**
* Clear all state, including history. Useful for cleanup or testing.
*/
clear(): void {
this.requestMap.clear()
}
}
/**
* Create an idempotency key from request parameters.
*
* Combines request path, method, and body into a stable hash.
* Same parameters always produce the same key, enabling idempotent retries.
*
* @example
* ```ts
* const key = createIdempotencyKey('/bonds', 'POST', { amount: 1000 })
* ```
*/
export function createIdempotencyKey(
path: string,
method: string = 'POST',
body?: unknown
): IdempotencyKey {
const parts = [path, method]
if (body !== undefined) {
parts.push(typeof body === 'string' ? body : JSON.stringify(body))
}
// Simple hash: SHA-256 would be ideal, but crypto isn't available in all environments.
// For this use case, a stable string hash is sufficient.
const hash = parts
.join('\x00')
.split('')
.reduce((h, c) => ((h << 5) - h + c.charCodeAt(0)) | 0, 0)
.toString(36)
return `${path}:${method}:${hash}` as IdempotencyKey
}