-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.ts
More file actions
618 lines (560 loc) · 17.3 KB
/
watcher.ts
File metadata and controls
618 lines (560 loc) · 17.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/**
* ConfigWatcher and WatchedField -- live configuration subscriptions.
*
* ConfigWatcher manages a server-streaming Subscribe RPC, loads an initial
* snapshot via GetConfig, and pushes changes to registered WatchedField instances.
* WatchedField provides the current value, EventEmitter change notifications,
* and async iteration via Symbol.asyncIterator.
*/
import { EventEmitter } from "node:events";
import { type ClientReadableStream, type Metadata, type ServiceError, status } from "@grpc/grpc-js";
import type { Converter } from "./convert.js";
import { convertValue, typedValueToString } from "./convert.js";
import { DecreeError, mapGrpcError, TypeMismatchError } from "./errors.js";
import type {
GetConfigRequest,
GetConfigResponse,
ConfigServiceClient as GrpcConfigServiceClient,
SubscribeRequest,
SubscribeResponse,
} from "./generated/centralconfig/v1/config_service.js";
import type { Change } from "./types.js";
/** gRPC status codes that trigger automatic reconnection. */
const RETRYABLE_CODES = new Set([status.UNAVAILABLE, status.INTERNAL]);
/** Maximum reconnect backoff in milliseconds. */
const MAX_RECONNECT_BACKOFF = 30_000;
/** Default maximum number of unread changes buffered per WatchedField before oldest are dropped. */
const DEFAULT_QUEUE_SIZE = 1024;
/** Initial reconnect backoff in milliseconds. */
const INITIAL_RECONNECT_BACKOFF = 500;
/** Backoff multiplier between reconnect attempts. */
const RECONNECT_MULTIPLIER = 2;
/**
* Options for registering a watched field.
*/
interface FieldOptions<T> {
/** Default value returned when the field has no value on the server. */
readonly default: T;
/**
* Maximum number of unread changes buffered for async iteration.
* When the queue is full, the oldest entry is dropped and `droppedChanges` is incremented.
* Default: 1024.
*/
readonly queueSize?: number;
}
/**
* WatchedField provides live access to a single configuration value.
*
* The value is always available synchronously via the `.value` getter.
* Changes can be observed via the EventEmitter `'change'` event or
* by iterating with `for await...of`.
*
* @typeParam T - The converted type (string, number, or boolean).
*
* @example
* ```ts
* const fee = watcher.field('payments.fee', Number, { default: 0.01 });
* await watcher.start();
*
* // Synchronous access
* console.log(fee.value);
*
* // EventEmitter
* fee.on('change', (oldVal, newVal) => {
* console.log(`Fee: ${oldVal} -> ${newVal}`);
* });
*
* // Async iteration
* for await (const change of fee) {
* console.log(change);
* }
* ```
*/
export class WatchedField<T> extends EventEmitter {
private currentValue: T;
private readonly defaultValue: T;
private readonly converter: Converter;
/** The dot-separated field path this WatchedField is bound to. */
readonly path: string;
private stopped = false;
private pendingResolve: ((value: IteratorResult<Change>) => void) | null = null;
private readonly changeQueue: Change[] = [];
private readonly maxQueueSize: number;
private _droppedChanges = 0;
/** @internal */
constructor(path: string, converter: Converter, options: FieldOptions<T>) {
super();
this.path = path;
this.converter = converter;
this.defaultValue = options.default;
this.currentValue = options.default;
this.maxQueueSize = options.queueSize ?? DEFAULT_QUEUE_SIZE;
}
/**
* Number of changes dropped because the queue was full.
*
* Increments whenever a slow consumer causes a buffered change to be evicted.
* Reset to zero only by creating a new WatchedField instance.
*/
get droppedChanges(): number {
return this._droppedChanges;
}
/**
* The current value of this field.
*
* Always returns the latest known value. Before `watcher.start()` completes,
* this returns the default value. After the initial snapshot loads, it reflects
* the server value. Subsequently it updates in real-time from the Subscribe stream.
*
* @returns The current value, converted to type T.
*/
get value(): T {
return this.currentValue;
}
/**
* Async iterator that yields Change objects as they arrive.
*
* The iterator completes when the watcher is stopped.
*
* @example
* ```ts
* for await (const change of field) {
* console.log(`${change.oldValue} -> ${change.newValue}`);
* }
* ```
*/
async *[Symbol.asyncIterator](): AsyncIterableIterator<Change> {
while (!this.stopped) {
const queued = this.changeQueue.shift();
if (queued) {
yield queued;
continue;
}
const result = await new Promise<IteratorResult<Change>>((resolve) => {
if (this.stopped) {
resolve({ done: true, value: undefined });
return;
}
this.pendingResolve = resolve;
});
if (result.done) {
return;
}
yield result.value;
}
}
/**
* Load the initial value from a GetConfig snapshot.
*
* If `convertValue` throws (e.g. type mismatch or unsupported converter),
* the field falls back to its default value and emits a `'conversionError'` event.
* The error is non-fatal — the stream continues.
*
* @param rawValue - The raw string value from the snapshot, or null if absent.
* @internal
*/
_loadInitial(rawValue: string | null): void {
if (rawValue === null) {
this.currentValue = this.defaultValue;
} else {
try {
this.currentValue = convertValue(rawValue, this.converter) as T;
} catch (err) {
console.warn(
`[decree] convertValue failed for field "${this.path}" (value=${JSON.stringify(rawValue)}): ${err instanceof Error ? err.message : String(err)}`,
);
this.currentValue = this.defaultValue;
const decreeErr =
err instanceof DecreeError
? err
: new TypeMismatchError(err instanceof Error ? err.message : String(err));
this.emit("conversionError", decreeErr, rawValue);
}
}
}
/**
* Update the field value from a ConfigChange event.
*
* Emits a `'change'` event if the new value differs from the current value.
* Enqueues a Change for async iteration.
*
* If `convertValue` throws (e.g. type mismatch or unsupported converter),
* the field retains its current value, emits a `'conversionError'` event,
* and returns without updating. The stream continues processing other fields.
*
* @param rawValue - The new raw string value, or null if set to null.
* @param change - The Change object describing this update.
* @internal
*/
_update(rawValue: string | null, change: Change): void {
const oldValue = this.currentValue;
if (rawValue === null) {
this.currentValue = this.defaultValue;
} else {
try {
this.currentValue = convertValue(rawValue, this.converter) as T;
} catch (err) {
console.warn(
`[decree] convertValue failed for field "${this.path}" (value=${JSON.stringify(rawValue)}): ${err instanceof Error ? err.message : String(err)}`,
);
const decreeErr =
err instanceof DecreeError
? err
: new TypeMismatchError(err instanceof Error ? err.message : String(err));
this.emit("conversionError", decreeErr, rawValue);
return;
}
}
// Only emit if the value actually changed.
if (oldValue === this.currentValue) {
return;
}
this.emit("change", oldValue, this.currentValue);
if (this.pendingResolve) {
const resolve = this.pendingResolve;
this.pendingResolve = null;
resolve({ done: false, value: change });
} else {
if (this.changeQueue.length >= this.maxQueueSize) {
this.changeQueue.shift();
this._droppedChanges++;
}
this.changeQueue.push(change);
}
}
/**
* Signal that the watcher has stopped, ending async iteration.
*
* @internal
*/
_stop(): void {
this.stopped = true;
if (this.pendingResolve) {
const resolve = this.pendingResolve;
this.pendingResolve = null;
resolve({ done: true, value: undefined });
}
}
}
/**
* Typed event map for WatchedField.
*/
export interface WatchedFieldEvents {
/**
* Emitted when the field value changes.
*
* Arguments are the old value and the new value.
*/
change: [oldValue: unknown, newValue: unknown];
/**
* Emitted when `convertValue` throws during `_loadInitial` or `_update`.
*
* The field retains its previous value (or default for `_loadInitial`).
* The gRPC stream continues — this error is non-fatal.
*/
conversionError: [err: DecreeError, rawValue: string];
}
/**
* Typed event map for ConfigWatcher.
*
* @example
* ```ts
* watcher.on('subscriptionError', (err) => {
* console.warn('subscription error:', err.message);
* });
* ```
*/
export interface ConfigWatcherEvents {
/**
* Emitted when a subscription error occurs.
*
* For retryable errors (UNAVAILABLE, INTERNAL) the watcher reconnects automatically.
* For non-retryable errors the watcher stops after emitting this event.
* The `err` argument is a typed `DecreeError` (e.g. `UnavailableError`).
*/
subscriptionError: [err: DecreeError];
}
/**
* ConfigWatcher subscribes to live configuration changes for a tenant.
*
* Created via `client.watch(tenantId)`. Register fields with `field()` before
* calling `start()`. The watcher loads an initial snapshot via GetConfig, then
* opens a Subscribe stream for real-time updates. On transient errors
* (UNAVAILABLE, INTERNAL), it automatically reconnects with exponential backoff.
*
* Subscription errors (both retryable and fatal) are emitted as `'subscriptionError'`
* events. Retryable errors trigger automatic reconnection; fatal errors cause the
* watcher to stop.
*
* @example
* ```ts
* const client = new ConfigClient('localhost:9090', { subject: 'myapp' });
* const watcher = client.watch('tenant-id');
*
* const fee = watcher.field('payments.fee', Number, { default: 0.01 });
* const enabled = watcher.field('payments.enabled', Boolean, { default: false });
*
* watcher.on('subscriptionError', (err) => {
* console.warn('watcher error:', err.message);
* });
*
* await watcher.start();
* console.log(fee.value); // current value from server
*
* fee.on('change', (oldVal, newVal) => {
* console.log(`Fee changed: ${oldVal} -> ${newVal}`);
* });
*
* // Later:
* await watcher.stop();
* client.close();
* ```
*/
export class ConfigWatcher extends EventEmitter {
private readonly configStub: InstanceType<typeof GrpcConfigServiceClient>;
private readonly metadata: Metadata;
private readonly timeout: number;
private readonly tenantId: string;
private readonly fields = new Map<string, WatchedField<unknown>>();
private started = false;
private stopped = false;
private stream: ClientReadableStream<SubscribeResponse> | null = null;
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
/** @internal */
constructor(
configStub: InstanceType<typeof GrpcConfigServiceClient>,
metadata: Metadata,
timeout: number,
tenantId: string,
) {
super();
this.configStub = configStub;
this.metadata = metadata;
this.timeout = timeout;
this.tenantId = tenantId;
}
override on<K extends keyof ConfigWatcherEvents>(
event: K,
listener: (...args: ConfigWatcherEvents[K]) => void,
): this;
override on(event: string | symbol, listener: (...args: unknown[]) => void): this;
override on(event: string | symbol, listener: (...args: unknown[]) => void): this {
return super.on(event, listener);
}
override once<K extends keyof ConfigWatcherEvents>(
event: K,
listener: (...args: ConfigWatcherEvents[K]) => void,
): this;
override once(event: string | symbol, listener: (...args: unknown[]) => void): this;
override once(event: string | symbol, listener: (...args: unknown[]) => void): this {
return super.once(event, listener);
}
override emit<K extends keyof ConfigWatcherEvents>(
event: K,
...args: ConfigWatcherEvents[K]
): boolean;
override emit(event: string | symbol, ...args: unknown[]): boolean;
override emit(event: string | symbol, ...args: unknown[]): boolean {
return super.emit(event, ...args);
}
/**
* Register a field to watch.
*
* Must be called before `start()`. Returns a WatchedField that will be
* populated with the initial value from the snapshot and updated in
* real-time from the Subscribe stream.
*
* @param path - Dot-separated field path (e.g. "payments.fee").
* @param converter - Type converter: String, Number, or Boolean.
* @param options - Options including the default value.
* @returns A WatchedField instance for this path.
* @throws DecreeError if called after start().
*
* @example
* ```ts
* const fee = watcher.field('payments.fee', Number, { default: 0.01 });
* ```
*/
field<T>(path: string, converter: Converter, options: FieldOptions<T>): WatchedField<T> {
if (this.started) {
throw new DecreeError("cannot register fields after start()");
}
const wf = new WatchedField<T>(path, converter, options);
this.fields.set(path, wf as WatchedField<unknown>);
return wf;
}
/**
* Load the initial snapshot and start the Subscribe stream.
*
* Fetches the current config via GetConfig, populates all registered fields,
* then opens a server-streaming Subscribe RPC. On transient errors, the
* stream automatically reconnects with exponential backoff.
*
* @throws DecreeError if called more than once.
* @throws DecreeError if the initial GetConfig call fails.
*/
async start(): Promise<void> {
if (this.started) {
throw new DecreeError("watcher already started");
}
this.started = true;
this.stopped = false;
// Load initial snapshot.
await this.loadSnapshot();
// Start the subscribe stream.
this.subscribe();
}
/**
* Stop the watcher, cancelling the Subscribe stream and cleaning up.
*
* Safe to call multiple times. After stopping, registered WatchedField
* async iterators will complete.
*/
stop(): Promise<void> {
if (this.stopped) {
return Promise.resolve();
}
this.stopped = true;
if (this.reconnectTimer !== null) {
clearTimeout(this.reconnectTimer);
this.reconnectTimer = null;
}
if (this.stream) {
this.stream.cancel();
this.stream = null;
}
for (const field of this.fields.values()) {
field._stop();
}
return Promise.resolve();
}
/**
* Dispose pattern support (TypeScript 5.2+).
*
* Calls stop() synchronously (best-effort). For full cleanup, prefer
* `await using` or calling `await watcher.stop()` explicitly.
*/
[Symbol.dispose](): void {
void this.stop();
}
/**
* Async dispose pattern support — use with `await using`.
*/
async [Symbol.asyncDispose](): Promise<void> {
await this.stop();
}
private async loadSnapshot(): Promise<void> {
const resp = await this.callGetConfig({
tenantId: this.tenantId,
includeDescriptions: false,
});
const valueMap = new Map<string, string>();
if (resp.config) {
for (const cv of resp.config.values) {
valueMap.set(cv.fieldPath, typedValueToString(cv.value));
}
}
for (const [path, field] of this.fields) {
const raw = valueMap.get(path);
field._loadInitial(raw ?? null);
}
}
private subscribe(initialBackoff = INITIAL_RECONNECT_BACKOFF): void {
if (this.stopped) {
return;
}
const fieldPaths = [...this.fields.keys()];
const request: SubscribeRequest = {
tenantId: this.tenantId,
fieldPaths,
};
this.stream = this.configStub.subscribe(request, this.metadata);
let backoff = initialBackoff;
this.stream.on("data", (resp: SubscribeResponse) => {
backoff = INITIAL_RECONNECT_BACKOFF;
this.processChange(resp);
});
this.stream.on("error", (err: ServiceError) => {
if (this.stopped) {
return;
}
this.emit("subscriptionError", mapGrpcError(err));
if (isRetryableError(err)) {
this.scheduleReconnect(backoff);
} else {
void this.stop();
}
});
this.stream.on("end", () => {
if (this.stopped) {
return;
}
// Server ended the stream (graceful shutdown). Reconnect.
this.scheduleReconnect(backoff);
});
}
private scheduleReconnect(backoff: number): void {
if (this.stopped) {
return;
}
const jitter = 0.5 + Math.random();
const delay = Math.min(backoff * jitter, MAX_RECONNECT_BACKOFF);
const nextBackoff = Math.min(backoff * RECONNECT_MULTIPLIER, MAX_RECONNECT_BACKOFF);
this.reconnectTimer = setTimeout(() => {
this.reconnectTimer = null;
void this.reloadAndSubscribe(nextBackoff);
}, delay);
}
private async reloadAndSubscribe(backoff: number): Promise<void> {
if (this.stopped) {
return;
}
try {
await this.loadSnapshot();
} catch {
this.scheduleReconnect(backoff);
return;
}
this.subscribe(INITIAL_RECONNECT_BACKOFF);
}
private processChange(resp: SubscribeResponse): void {
if (!resp.change) {
return;
}
const ch = resp.change;
const field = this.fields.get(ch.fieldPath);
if (!field) {
// Change for an unregistered field -- ignore.
return;
}
const oldRaw = ch.oldValue ? typedValueToString(ch.oldValue) : null;
const newRaw = ch.newValue ? typedValueToString(ch.newValue) : null;
const change: Change = {
fieldPath: ch.fieldPath,
oldValue: oldRaw,
newValue: newRaw,
version: ch.version,
changedBy: ch.changedBy,
};
field._update(newRaw, change);
}
private callGetConfig(request: GetConfigRequest): Promise<GetConfigResponse> {
return new Promise((resolve, reject) => {
this.configStub.getConfig(
request,
this.metadata,
{ deadline: Date.now() + this.timeout },
(err: ServiceError | null, resp: GetConfigResponse) => {
if (err) {
reject(mapGrpcError(err));
} else {
resolve(resp);
}
},
);
});
}
}
function isRetryableError(err: ServiceError): boolean {
return RETRYABLE_CODES.has(err.code);
}