@metarouter/react-native-sdk 1.2.1 → 1.3.0
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.
- package/lib/analytics/MetaRouterAnalyticsClient.d.ts +3 -11
- package/lib/analytics/MetaRouterAnalyticsClient.d.ts.map +1 -1
- package/lib/analytics/MetaRouterAnalyticsClient.js +56 -213
- package/lib/analytics/dispatcher.d.ts +51 -0
- package/lib/analytics/dispatcher.d.ts.map +1 -0
- package/lib/analytics/dispatcher.js +211 -0
- package/package.json +1 -1
|
@@ -10,8 +10,7 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
10
10
|
private lifecycle;
|
|
11
11
|
private initPromise;
|
|
12
12
|
private queue;
|
|
13
|
-
private
|
|
14
|
-
private flushTimer;
|
|
13
|
+
private flushIntervalSeconds;
|
|
15
14
|
private ingestionHost;
|
|
16
15
|
private writeKey;
|
|
17
16
|
private context;
|
|
@@ -19,12 +18,8 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
19
18
|
private appStateSubscription;
|
|
20
19
|
private identityManager;
|
|
21
20
|
private static readonly MAX_QUEUE_SIZE;
|
|
22
|
-
private flushInFlight;
|
|
23
|
-
private maxBatchSize;
|
|
24
|
-
private circuit;
|
|
25
|
-
private nextTimer;
|
|
26
|
-
private nextScheduledAt;
|
|
27
21
|
private maxQueueEvents;
|
|
22
|
+
private dispatcher;
|
|
28
23
|
/**
|
|
29
24
|
* Initializes the analytics client with the provided options.
|
|
30
25
|
* @param options - The initialization options.
|
|
@@ -41,7 +36,6 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
41
36
|
*/
|
|
42
37
|
private startFlushLoop;
|
|
43
38
|
private isReady;
|
|
44
|
-
private makeBreaker;
|
|
45
39
|
/**
|
|
46
40
|
* Fetches with a timeout.
|
|
47
41
|
* @param url - The URL to fetch.
|
|
@@ -60,11 +54,9 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
60
54
|
* @param event - The event to enqueue.
|
|
61
55
|
*/
|
|
62
56
|
private enqueue;
|
|
63
|
-
private drainBatch;
|
|
64
57
|
private scheduleFlushIn;
|
|
65
58
|
private stopFlushLoop;
|
|
66
59
|
private clearNextTimer;
|
|
67
|
-
private parseRetryAfter;
|
|
68
60
|
/**
|
|
69
61
|
* Sets up the app state listener.
|
|
70
62
|
*/
|
|
@@ -122,7 +114,7 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
122
114
|
queueLength: number;
|
|
123
115
|
ingestionHost: string;
|
|
124
116
|
writeKey: string | undefined;
|
|
125
|
-
|
|
117
|
+
flushIntervalSeconds: number;
|
|
126
118
|
anonymousId: string | null;
|
|
127
119
|
userId: string | undefined;
|
|
128
120
|
groupId: string | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MetaRouterAnalyticsClient.d.ts","sourceRoot":"","sources":["../../src/analytics/MetaRouterAnalyticsClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,WAAW,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"MetaRouterAnalyticsClient.d.ts","sourceRoot":"","sources":["../../src/analytics/MetaRouterAnalyticsClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,WAAW,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAS7E;;;;;;GAMG;AACH,qBAAa,yBAAyB;IACpC,OAAO,CAAC,SAAS,CAAqB;IACtC,OAAO,CAAC,WAAW,CAA8B;IACjD,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,oBAAoB,CAAM;IAClC,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,QAAQ,CAAyC;IACzD,OAAO,CAAC,oBAAoB,CAAwC;IACpE,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAM;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,CAAC,IAAI,CAAC;CAyBpC"}
|
|
@@ -4,6 +4,7 @@ import { IdentityManager } from "./IdentityManager";
|
|
|
4
4
|
import { enrichEvent } from "./utils/enrichEvent";
|
|
5
5
|
import { getContextInfo } from "./utils/contextInfo";
|
|
6
6
|
import CircuitBreaker from "./utils/circuitBreaker";
|
|
7
|
+
import Dispatcher from "./dispatcher";
|
|
7
8
|
/**
|
|
8
9
|
* Analytics client for MetaRouter.
|
|
9
10
|
* - Handles event queueing, batching, and delivery with retries.
|
|
@@ -21,14 +22,10 @@ export class MetaRouterAnalyticsClient {
|
|
|
21
22
|
this.lifecycle = "idle";
|
|
22
23
|
this.initPromise = null;
|
|
23
24
|
this.queue = [];
|
|
24
|
-
this.
|
|
25
|
-
this.flushTimer = null;
|
|
25
|
+
this.flushIntervalSeconds = 10;
|
|
26
26
|
this.appState = AppState.currentState;
|
|
27
27
|
this.appStateSubscription = null;
|
|
28
|
-
this.
|
|
29
|
-
this.maxBatchSize = 100;
|
|
30
|
-
this.nextTimer = null;
|
|
31
|
-
this.nextScheduledAt = null;
|
|
28
|
+
this.maxQueueEvents = 2000;
|
|
32
29
|
/**
|
|
33
30
|
* Handles the app state change event.
|
|
34
31
|
* @param nextState - The new app state.
|
|
@@ -66,13 +63,43 @@ export class MetaRouterAnalyticsClient {
|
|
|
66
63
|
}
|
|
67
64
|
this.ingestionHost = ingestionHost;
|
|
68
65
|
this.writeKey = writeKey;
|
|
69
|
-
this.
|
|
70
|
-
? flushIntervalSeconds * 1000
|
|
71
|
-
: 10000;
|
|
66
|
+
this.flushIntervalSeconds = flushIntervalSeconds !== null && flushIntervalSeconds !== void 0 ? flushIntervalSeconds : 10;
|
|
72
67
|
setDebugLogging((_a = options.debug) !== null && _a !== void 0 ? _a : false);
|
|
73
68
|
this.identityManager = new IdentityManager();
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
69
|
+
this.maxQueueEvents = (_b = options.maxQueueEvents) !== null && _b !== void 0 ? _b : this.maxQueueEvents;
|
|
70
|
+
this.dispatcher = new Dispatcher({
|
|
71
|
+
maxQueueEvents: this.maxQueueEvents,
|
|
72
|
+
autoFlushThreshold: MetaRouterAnalyticsClient.MAX_QUEUE_SIZE,
|
|
73
|
+
maxBatchSize: 100,
|
|
74
|
+
flushIntervalSeconds: this.flushIntervalSeconds,
|
|
75
|
+
endpoint: (path) => this.endpoint(path),
|
|
76
|
+
fetchWithTimeout: (url, init, timeoutMs) => this.fetchWithTimeout(url, init, timeoutMs),
|
|
77
|
+
canSend: () => this.lifecycle === "ready" && !!this.identityManager.getAnonymousId(),
|
|
78
|
+
isOperational: () => this.lifecycle === "ready",
|
|
79
|
+
createBreaker: () => new CircuitBreaker({
|
|
80
|
+
failureThreshold: 3,
|
|
81
|
+
cooldownMs: 10000,
|
|
82
|
+
maxCooldownMs: 120000,
|
|
83
|
+
jitterRatio: 0.2,
|
|
84
|
+
halfOpenMaxConcurrent: 1,
|
|
85
|
+
onStateChange: (prev, next, meta) => {
|
|
86
|
+
log(`[MetaRouter] Circuit ${prev} → ${next}` +
|
|
87
|
+
(meta.cooldownMs != null
|
|
88
|
+
? ` (cooldown=${meta.cooldownMs}ms)`
|
|
89
|
+
: ""), { failures: meta.failures, openCount: meta.openCount });
|
|
90
|
+
},
|
|
91
|
+
}),
|
|
92
|
+
log,
|
|
93
|
+
warn,
|
|
94
|
+
error,
|
|
95
|
+
onScheduleFlushIn: (ms) => {
|
|
96
|
+
this.scheduleFlushIn(ms, { fromDispatcher: true });
|
|
97
|
+
},
|
|
98
|
+
onFatalConfig: () => {
|
|
99
|
+
this.lifecycle = "disabled";
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
this.queue = this.dispatcher.getQueueRef();
|
|
76
103
|
log("Analytics client constructor completed, initialization in progress...");
|
|
77
104
|
}
|
|
78
105
|
/**
|
|
@@ -99,7 +126,7 @@ export class MetaRouterAnalyticsClient {
|
|
|
99
126
|
await this.identityManager.init();
|
|
100
127
|
log("IdentityManager initialized successfully");
|
|
101
128
|
this.startFlushLoop();
|
|
102
|
-
log("Flush loop started with interval:", this.
|
|
129
|
+
log("Flush loop started with interval:", this.flushIntervalSeconds, "seconds");
|
|
103
130
|
this.setupAppStateListener();
|
|
104
131
|
log("App state listener setup completed");
|
|
105
132
|
this.context = await getContextInfo();
|
|
@@ -121,26 +148,11 @@ export class MetaRouterAnalyticsClient {
|
|
|
121
148
|
* Starts the flush loop.
|
|
122
149
|
*/
|
|
123
150
|
startFlushLoop() {
|
|
124
|
-
|
|
125
|
-
clearInterval(this.flushTimer);
|
|
126
|
-
this.flushTimer = setInterval(() => this.flush(), this.flushIntervalMs);
|
|
151
|
+
this.dispatcher.start();
|
|
127
152
|
}
|
|
128
153
|
isReady() {
|
|
129
154
|
return this.lifecycle === "ready";
|
|
130
155
|
}
|
|
131
|
-
makeBreaker() {
|
|
132
|
-
return new CircuitBreaker({
|
|
133
|
-
failureThreshold: 3,
|
|
134
|
-
cooldownMs: 10000,
|
|
135
|
-
maxCooldownMs: 120000,
|
|
136
|
-
jitterRatio: 0.2,
|
|
137
|
-
halfOpenMaxConcurrent: 1,
|
|
138
|
-
onStateChange: (prev, next, meta) => {
|
|
139
|
-
log(`[MetaRouter] Circuit ${prev} → ${next}` +
|
|
140
|
-
(meta.cooldownMs != null ? ` (cooldown=${meta.cooldownMs}ms)` : ""), { failures: meta.failures, openCount: meta.openCount });
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
156
|
/**
|
|
145
157
|
* Fetches with a timeout.
|
|
146
158
|
* @param url - The URL to fetch.
|
|
@@ -177,64 +189,18 @@ export class MetaRouterAnalyticsClient {
|
|
|
177
189
|
}
|
|
178
190
|
const eventWithIdentity = this.identityManager.addIdentityInfo(event);
|
|
179
191
|
const enrichedEvent = enrichEvent(eventWithIdentity, this.writeKey, this.context);
|
|
180
|
-
|
|
181
|
-
while (this.queue.length >= this.maxQueueEvents) {
|
|
182
|
-
this.queue.shift();
|
|
183
|
-
warn(`[MetaRouter] Queue cap ${this.maxQueueEvents} reached — dropped oldest event`);
|
|
184
|
-
}
|
|
185
|
-
log("Enqueuing event", {
|
|
186
|
-
type: enrichedEvent === null || enrichedEvent === void 0 ? void 0 : enrichedEvent.type,
|
|
187
|
-
messageId: enrichedEvent === null || enrichedEvent === void 0 ? void 0 : enrichedEvent.messageId,
|
|
188
|
-
});
|
|
189
|
-
this.queue.push(enrichedEvent);
|
|
190
|
-
if (this.queue.length >= MetaRouterAnalyticsClient.MAX_QUEUE_SIZE) {
|
|
191
|
-
log(`Event queue reached ${MetaRouterAnalyticsClient.MAX_QUEUE_SIZE}. Flushing queued events.`);
|
|
192
|
-
this.flush();
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
drainBatch() {
|
|
196
|
-
const n = Math.min(this.queue.length, this.maxBatchSize);
|
|
197
|
-
const batch = this.queue
|
|
198
|
-
.splice(0, n)
|
|
199
|
-
.map((e) => ({ ...e, sentAt: this.now() }));
|
|
200
|
-
return batch;
|
|
192
|
+
this.dispatcher.enqueue(enrichedEvent);
|
|
201
193
|
}
|
|
202
|
-
scheduleFlushIn(ms) {
|
|
203
|
-
|
|
204
|
-
// If we already have a timer sooner or equal, keep it.
|
|
205
|
-
if (this.nextScheduledAt && target >= this.nextScheduledAt - 25)
|
|
194
|
+
scheduleFlushIn(ms, opts) {
|
|
195
|
+
if (opts === null || opts === void 0 ? void 0 : opts.fromDispatcher)
|
|
206
196
|
return;
|
|
207
|
-
|
|
208
|
-
if (this.nextTimer)
|
|
209
|
-
clearTimeout(this.nextTimer);
|
|
210
|
-
this.nextScheduledAt = target;
|
|
211
|
-
this.nextTimer = setTimeout(() => {
|
|
212
|
-
this.nextScheduledAt = null;
|
|
213
|
-
this.nextTimer = null;
|
|
214
|
-
this.flush();
|
|
215
|
-
}, Math.max(0, ms));
|
|
197
|
+
this.dispatcher.scheduleFlushIn(ms);
|
|
216
198
|
}
|
|
217
199
|
stopFlushLoop() {
|
|
218
|
-
|
|
219
|
-
clearInterval(this.flushTimer);
|
|
220
|
-
this.flushTimer = null;
|
|
221
|
-
}
|
|
200
|
+
this.dispatcher.stop();
|
|
222
201
|
}
|
|
223
202
|
clearNextTimer() {
|
|
224
|
-
|
|
225
|
-
clearTimeout(this.nextTimer);
|
|
226
|
-
this.nextTimer = null;
|
|
227
|
-
}
|
|
228
|
-
this.nextScheduledAt = null;
|
|
229
|
-
}
|
|
230
|
-
parseRetryAfter(h) {
|
|
231
|
-
if (!h)
|
|
232
|
-
return null;
|
|
233
|
-
const secs = Number(h);
|
|
234
|
-
if (!Number.isNaN(secs))
|
|
235
|
-
return secs * 1000;
|
|
236
|
-
const dateMs = Date.parse(h);
|
|
237
|
-
return Number.isNaN(dateMs) ? null : Math.max(0, dateMs - Date.now());
|
|
203
|
+
// Timers owned by dispatcher
|
|
238
204
|
}
|
|
239
205
|
/**
|
|
240
206
|
* Sets up the app state listener.
|
|
@@ -326,21 +292,21 @@ export class MetaRouterAnalyticsClient {
|
|
|
326
292
|
* Get current state for debugging
|
|
327
293
|
*/
|
|
328
294
|
async getDebugInfo() {
|
|
329
|
-
const
|
|
295
|
+
const d = this.dispatcher.getDebugInfo();
|
|
330
296
|
return {
|
|
331
297
|
lifecycle: this.lifecycle,
|
|
332
|
-
queueLength:
|
|
298
|
+
queueLength: d.queueLength,
|
|
333
299
|
ingestionHost: this.ingestionHost,
|
|
334
300
|
writeKey: this.writeKey ? "***" + this.writeKey.slice(-4) : undefined,
|
|
335
|
-
|
|
301
|
+
flushIntervalSeconds: this.flushIntervalSeconds,
|
|
336
302
|
anonymousId: this.identityManager.getAnonymousId(),
|
|
337
303
|
userId: this.identityManager.getUserId(),
|
|
338
304
|
groupId: this.identityManager.getGroupId(),
|
|
339
305
|
proxy: false,
|
|
340
|
-
flushInFlight:
|
|
341
|
-
circuitState:
|
|
342
|
-
circuitRemainingMs:
|
|
343
|
-
maxQueueEvents:
|
|
306
|
+
flushInFlight: d.flushInFlight,
|
|
307
|
+
circuitState: d.circuitState,
|
|
308
|
+
circuitRemainingMs: d.circuitRemainingMs,
|
|
309
|
+
maxQueueEvents: d.maxQueueEvents,
|
|
344
310
|
};
|
|
345
311
|
}
|
|
346
312
|
/**
|
|
@@ -348,126 +314,7 @@ export class MetaRouterAnalyticsClient {
|
|
|
348
314
|
* Singleflight: coalesces concurrent callers.
|
|
349
315
|
*/
|
|
350
316
|
async flush() {
|
|
351
|
-
|
|
352
|
-
return;
|
|
353
|
-
if (this.flushInFlight)
|
|
354
|
-
return this.flushInFlight;
|
|
355
|
-
if (!this.isReady())
|
|
356
|
-
return;
|
|
357
|
-
const anonId = this.identityManager.getAnonymousId();
|
|
358
|
-
if (!anonId) {
|
|
359
|
-
warn("Anonymous ID not yet ready, delaying flush");
|
|
360
|
-
return;
|
|
361
|
-
}
|
|
362
|
-
const doFlush = async () => {
|
|
363
|
-
var _a, _b;
|
|
364
|
-
// Keep sending while there is work. New events enqueued during the loop
|
|
365
|
-
// will be included in this same flush cycle.
|
|
366
|
-
while (this.queue.length) {
|
|
367
|
-
if (!this.circuit.allowRequest()) {
|
|
368
|
-
const state = this.circuit.getState();
|
|
369
|
-
const wait = this.circuit.remainingCooldownMs();
|
|
370
|
-
log(`[MetaRouter] Circuit ${state} — skip; wait ${wait}ms`);
|
|
371
|
-
this.scheduleFlushIn(wait);
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
const chunk = this.drainBatch();
|
|
375
|
-
try {
|
|
376
|
-
log("Making API call to:", this.endpoint("/v1/batch"));
|
|
377
|
-
const res = await this.fetchWithTimeout(this.endpoint("/v1/batch"), {
|
|
378
|
-
method: "POST",
|
|
379
|
-
headers: { "Content-Type": "application/json" },
|
|
380
|
-
body: JSON.stringify({ batch: chunk }),
|
|
381
|
-
}, 8000);
|
|
382
|
-
if (!res.ok) {
|
|
383
|
-
const s = res.status;
|
|
384
|
-
// Retryable (do NOT throw; mark failure once and schedule)
|
|
385
|
-
if (s >= 500 || s === 408) {
|
|
386
|
-
this.circuit.onFailure();
|
|
387
|
-
// ⬇️ Only requeue/schedule if still operable
|
|
388
|
-
if (this.isReady()) {
|
|
389
|
-
this.queue.unshift(...chunk);
|
|
390
|
-
const retryAfter = (_a = this.parseRetryAfter(res.headers.get("retry-after"))) !== null && _a !== void 0 ? _a : 0;
|
|
391
|
-
const wait = Math.max(this.circuit.remainingCooldownMs(), retryAfter, 1000);
|
|
392
|
-
warn(`Retryable status ${s}; scheduling retry in ${wait}ms`);
|
|
393
|
-
this.scheduleFlushIn(wait);
|
|
394
|
-
}
|
|
395
|
-
else {
|
|
396
|
-
warn("Flush aborted during reset/disable — dropping current chunk");
|
|
397
|
-
}
|
|
398
|
-
return;
|
|
399
|
-
}
|
|
400
|
-
if (s === 429) {
|
|
401
|
-
this.circuit.onFailure();
|
|
402
|
-
if (this.isReady()) {
|
|
403
|
-
this.queue.unshift(...chunk);
|
|
404
|
-
const h = (_b = this.parseRetryAfter(res.headers.get("retry-after"))) !== null && _b !== void 0 ? _b : 0;
|
|
405
|
-
const b = this.circuit.remainingCooldownMs();
|
|
406
|
-
const wait = Math.max(h, b, 1000);
|
|
407
|
-
warn(`429 throttled; scheduling in ${wait}ms (retry-after=${h}, breaker=${b})`);
|
|
408
|
-
this.scheduleFlushIn(wait);
|
|
409
|
-
}
|
|
410
|
-
else {
|
|
411
|
-
warn("Throttle received but client resetting/disabled — dropping chunk");
|
|
412
|
-
}
|
|
413
|
-
return;
|
|
414
|
-
}
|
|
415
|
-
// Fatal config
|
|
416
|
-
if (s === 401 || s === 403 || s === 404) {
|
|
417
|
-
error(`Fatal config error ${s}. Disabling client.`);
|
|
418
|
-
this.lifecycle = "disabled";
|
|
419
|
-
this.queue = [];
|
|
420
|
-
this.clearNextTimer();
|
|
421
|
-
this.stopFlushLoop();
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
// Too large: shrink batch and retry later
|
|
425
|
-
if (s === 413) {
|
|
426
|
-
this.circuit.onNonRetryable();
|
|
427
|
-
if (this.maxBatchSize > 1) {
|
|
428
|
-
this.maxBatchSize = Math.max(1, Math.floor(this.maxBatchSize / 2));
|
|
429
|
-
warn(`Payload too large; reducing maxBatchSize to ${this.maxBatchSize}`);
|
|
430
|
-
this.queue.unshift(...chunk);
|
|
431
|
-
this.scheduleFlushIn(500);
|
|
432
|
-
}
|
|
433
|
-
else {
|
|
434
|
-
// Single event still too large — drop with signal
|
|
435
|
-
const ids = chunk.map((e) => e.messageId).join(",");
|
|
436
|
-
warn(`Dropping oversize event(s) after 413 at batchSize=1; messageIds=${ids}`);
|
|
437
|
-
}
|
|
438
|
-
return;
|
|
439
|
-
}
|
|
440
|
-
// Other 4xx: drop bad payload
|
|
441
|
-
this.circuit.onNonRetryable();
|
|
442
|
-
warn(`Dropping batch due to client error ${s}`);
|
|
443
|
-
continue;
|
|
444
|
-
}
|
|
445
|
-
// Success path
|
|
446
|
-
this.circuit.onSuccess();
|
|
447
|
-
log("API call successful");
|
|
448
|
-
// continue loop to send next chunk
|
|
449
|
-
}
|
|
450
|
-
catch (err) {
|
|
451
|
-
// Network/timeout/abort -> single failure mark
|
|
452
|
-
this.circuit.onFailure();
|
|
453
|
-
if (this.isReady()) {
|
|
454
|
-
this.queue.unshift(...chunk);
|
|
455
|
-
const wait = Math.max(this.circuit.remainingCooldownMs(), 1000);
|
|
456
|
-
warn("Flush attempt failed; scheduling retry in", wait, "ms", err === null || err === void 0 ? void 0 : err.message);
|
|
457
|
-
this.scheduleFlushIn(wait);
|
|
458
|
-
}
|
|
459
|
-
else {
|
|
460
|
-
warn("Flush failed during reset/disable — dropping current chunk");
|
|
461
|
-
}
|
|
462
|
-
return;
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
log("Flush completed successfully");
|
|
466
|
-
};
|
|
467
|
-
this.flushInFlight = doFlush().finally(() => {
|
|
468
|
-
this.flushInFlight = null;
|
|
469
|
-
});
|
|
470
|
-
return this.flushInFlight;
|
|
317
|
+
return this.dispatcher.flush();
|
|
471
318
|
}
|
|
472
319
|
/**
|
|
473
320
|
* Resets the analytics client.
|
|
@@ -478,14 +325,10 @@ export class MetaRouterAnalyticsClient {
|
|
|
478
325
|
// Flip lifecycle first so other paths see we're resetting
|
|
479
326
|
this.lifecycle = "resetting";
|
|
480
327
|
// Stop background work
|
|
481
|
-
|
|
482
|
-
clearInterval(this.flushTimer);
|
|
483
|
-
this.flushTimer = null;
|
|
484
|
-
}
|
|
328
|
+
this.dispatcher.stop();
|
|
485
329
|
(_b = (_a = this.appStateSubscription) === null || _a === void 0 ? void 0 : _a.remove) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
486
330
|
this.appStateSubscription = null;
|
|
487
|
-
this.
|
|
488
|
-
this.circuit = this.makeBreaker();
|
|
331
|
+
this.dispatcher.reset();
|
|
489
332
|
// Clear identity (must remove persisted IDs)
|
|
490
333
|
await this.identityManager.reset();
|
|
491
334
|
// Allow a clean future init
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { EventPayload } from "./types";
|
|
2
|
+
import type CircuitBreaker from "./utils/circuitBreaker";
|
|
3
|
+
export type CircuitState = "CLOSED" | "OPEN" | "HALF_OPEN";
|
|
4
|
+
export interface DispatcherOptions {
|
|
5
|
+
maxQueueEvents: number;
|
|
6
|
+
autoFlushThreshold: number;
|
|
7
|
+
maxBatchSize: number;
|
|
8
|
+
flushIntervalSeconds: number;
|
|
9
|
+
endpoint: (path: string) => string;
|
|
10
|
+
fetchWithTimeout: (url: string, init: RequestInit, timeoutMs: number) => Promise<Response>;
|
|
11
|
+
canSend: () => boolean;
|
|
12
|
+
isOperational: () => boolean;
|
|
13
|
+
createBreaker: () => CircuitBreaker;
|
|
14
|
+
log: (...args: any[]) => void;
|
|
15
|
+
warn: (...args: any[]) => void;
|
|
16
|
+
error: (...args: any[]) => void;
|
|
17
|
+
onScheduleFlushIn?: (ms: number) => void;
|
|
18
|
+
onFatalConfig?: () => void;
|
|
19
|
+
}
|
|
20
|
+
export default class Dispatcher {
|
|
21
|
+
private readonly opts;
|
|
22
|
+
private readonly queue;
|
|
23
|
+
private flushTimer;
|
|
24
|
+
private nextTimer;
|
|
25
|
+
private nextScheduledAt;
|
|
26
|
+
private flushInFlight;
|
|
27
|
+
private maxBatchSize;
|
|
28
|
+
private circuit;
|
|
29
|
+
constructor(opts: DispatcherOptions);
|
|
30
|
+
getQueueRef(): EventPayload[];
|
|
31
|
+
start(): void;
|
|
32
|
+
stop(): void;
|
|
33
|
+
reset(): void;
|
|
34
|
+
private clearNextTimer;
|
|
35
|
+
scheduleFlushIn(ms: number): void;
|
|
36
|
+
enqueue(event: EventPayload): void;
|
|
37
|
+
private drainBatch;
|
|
38
|
+
private parseRetryAfter;
|
|
39
|
+
flush(): Promise<void>;
|
|
40
|
+
getDebugInfo(): {
|
|
41
|
+
queueLength: number;
|
|
42
|
+
flushIntervalSeconds: number;
|
|
43
|
+
proxy: boolean;
|
|
44
|
+
flushInFlight: boolean;
|
|
45
|
+
circuitState: import("./utils/circuitBreaker").CircuitState;
|
|
46
|
+
circuitRemainingMs: number;
|
|
47
|
+
maxQueueEvents: number;
|
|
48
|
+
maxBatchSize: number;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=dispatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../../src/analytics/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,cAAc,MAAM,wBAAwB,CAAC;AAEzD,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AAE3D,MAAM,WAAW,iBAAiB;IAChC,cAAc,EAAE,MAAM,CAAC;IACvB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,YAAY,EAAE,MAAM,CAAC;IACrB,oBAAoB,EAAE,MAAM,CAAC;IAE7B,QAAQ,EAAE,CAAC,IA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;IAQjB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAkJ5B,YAAY;;;;;;;;;;CAYb"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
export default class Dispatcher {
|
|
2
|
+
constructor(opts) {
|
|
3
|
+
this.queue = [];
|
|
4
|
+
this.flushTimer = null;
|
|
5
|
+
this.nextTimer = null;
|
|
6
|
+
this.nextScheduledAt = null;
|
|
7
|
+
this.flushInFlight = null;
|
|
8
|
+
this.opts = opts;
|
|
9
|
+
this.maxBatchSize = Math.max(1, opts.maxBatchSize);
|
|
10
|
+
this.circuit = opts.createBreaker();
|
|
11
|
+
}
|
|
12
|
+
getQueueRef() {
|
|
13
|
+
return this.queue;
|
|
14
|
+
}
|
|
15
|
+
start() {
|
|
16
|
+
if (this.flushTimer)
|
|
17
|
+
clearInterval(this.flushTimer);
|
|
18
|
+
this.flushTimer = setInterval(() => this.flush(), this.opts.flushIntervalSeconds * 1000);
|
|
19
|
+
}
|
|
20
|
+
stop() {
|
|
21
|
+
if (this.flushTimer) {
|
|
22
|
+
clearInterval(this.flushTimer);
|
|
23
|
+
this.flushTimer = null;
|
|
24
|
+
}
|
|
25
|
+
this.clearNextTimer();
|
|
26
|
+
}
|
|
27
|
+
reset() {
|
|
28
|
+
this.stop();
|
|
29
|
+
this.queue.length = 0;
|
|
30
|
+
this.circuit = this.opts.createBreaker();
|
|
31
|
+
this.maxBatchSize = Math.max(1, this.opts.maxBatchSize);
|
|
32
|
+
}
|
|
33
|
+
clearNextTimer() {
|
|
34
|
+
if (this.nextTimer) {
|
|
35
|
+
clearTimeout(this.nextTimer);
|
|
36
|
+
this.nextTimer = null;
|
|
37
|
+
}
|
|
38
|
+
this.nextScheduledAt = null;
|
|
39
|
+
}
|
|
40
|
+
scheduleFlushIn(ms) {
|
|
41
|
+
var _a, _b;
|
|
42
|
+
const target = Date.now() + Math.max(0, ms);
|
|
43
|
+
if (this.nextScheduledAt && target >= this.nextScheduledAt - 25)
|
|
44
|
+
return;
|
|
45
|
+
if (this.nextTimer)
|
|
46
|
+
clearTimeout(this.nextTimer);
|
|
47
|
+
(_b = (_a = this.opts).onScheduleFlushIn) === null || _b === void 0 ? void 0 : _b.call(_a, ms);
|
|
48
|
+
this.nextScheduledAt = target;
|
|
49
|
+
this.nextTimer = setTimeout(() => {
|
|
50
|
+
this.nextScheduledAt = null;
|
|
51
|
+
this.nextTimer = null;
|
|
52
|
+
this.flush();
|
|
53
|
+
}, Math.max(0, ms));
|
|
54
|
+
}
|
|
55
|
+
enqueue(event) {
|
|
56
|
+
// Hard cap: drop oldest until there's room
|
|
57
|
+
while (this.queue.length >= this.opts.maxQueueEvents) {
|
|
58
|
+
this.queue.shift();
|
|
59
|
+
this.opts.warn(`[MetaRouter] Queue cap ${this.opts.maxQueueEvents} reached — dropped oldest event`);
|
|
60
|
+
}
|
|
61
|
+
this.opts.log("Enqueuing event", {
|
|
62
|
+
type: event === null || event === void 0 ? void 0 : event.type,
|
|
63
|
+
messageId: event === null || event === void 0 ? void 0 : event.messageId,
|
|
64
|
+
});
|
|
65
|
+
this.queue.push(event);
|
|
66
|
+
if (this.queue.length >= this.opts.autoFlushThreshold) {
|
|
67
|
+
this.opts.log(`Event queue reached ${this.opts.autoFlushThreshold}. Flushing queued events.`);
|
|
68
|
+
void this.flush();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
drainBatch() {
|
|
72
|
+
const n = Math.min(this.queue.length, this.maxBatchSize);
|
|
73
|
+
const nowIso = new Date().toISOString();
|
|
74
|
+
const batch = this.queue.splice(0, n).map((e) => ({
|
|
75
|
+
...e,
|
|
76
|
+
sentAt: nowIso,
|
|
77
|
+
}));
|
|
78
|
+
return batch;
|
|
79
|
+
}
|
|
80
|
+
parseRetryAfter(h) {
|
|
81
|
+
if (!h)
|
|
82
|
+
return null;
|
|
83
|
+
const secs = Number(h);
|
|
84
|
+
if (!Number.isNaN(secs))
|
|
85
|
+
return secs * 1000;
|
|
86
|
+
const dateMs = Date.parse(h);
|
|
87
|
+
return Number.isNaN(dateMs) ? null : Math.max(0, dateMs - Date.now());
|
|
88
|
+
}
|
|
89
|
+
async flush() {
|
|
90
|
+
if (!this.queue.length)
|
|
91
|
+
return;
|
|
92
|
+
if (this.flushInFlight)
|
|
93
|
+
return this.flushInFlight;
|
|
94
|
+
if (!this.opts.canSend())
|
|
95
|
+
return;
|
|
96
|
+
const doFlush = async () => {
|
|
97
|
+
var _a, _b, _c, _d;
|
|
98
|
+
while (this.queue.length) {
|
|
99
|
+
if (!this.circuit.allowRequest()) {
|
|
100
|
+
const state = this.circuit.getState();
|
|
101
|
+
const wait = this.circuit.remainingCooldownMs();
|
|
102
|
+
this.opts.log(`[MetaRouter] Circuit ${state} — skip; wait ${wait}ms`);
|
|
103
|
+
this.scheduleFlushIn(wait);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const chunk = this.drainBatch();
|
|
107
|
+
try {
|
|
108
|
+
this.opts.log("Making API call to:", this.opts.endpoint("/v1/batch"));
|
|
109
|
+
const res = await this.opts.fetchWithTimeout(this.opts.endpoint("/v1/batch"), {
|
|
110
|
+
method: "POST",
|
|
111
|
+
headers: { "Content-Type": "application/json" },
|
|
112
|
+
body: JSON.stringify({ batch: chunk }),
|
|
113
|
+
}, 8000);
|
|
114
|
+
if (!res.ok) {
|
|
115
|
+
const s = res.status;
|
|
116
|
+
// Retryable
|
|
117
|
+
if (s >= 500 || s === 408) {
|
|
118
|
+
this.circuit.onFailure();
|
|
119
|
+
if (this.opts.isOperational()) {
|
|
120
|
+
this.queue.unshift(...chunk);
|
|
121
|
+
const retryAfter = (_a = this.parseRetryAfter(res.headers.get("retry-after"))) !== null && _a !== void 0 ? _a : 0;
|
|
122
|
+
const wait = Math.max(this.circuit.remainingCooldownMs(), retryAfter, 1000);
|
|
123
|
+
this.opts.warn(`Retryable status ${s}; scheduling retry in ${wait}ms`);
|
|
124
|
+
this.scheduleFlushIn(wait);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
this.opts.warn("Flush aborted during reset/disable — dropping current chunk");
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (s === 429) {
|
|
132
|
+
this.circuit.onFailure();
|
|
133
|
+
if (this.opts.isOperational()) {
|
|
134
|
+
this.queue.unshift(...chunk);
|
|
135
|
+
const h = (_b = this.parseRetryAfter(res.headers.get("retry-after"))) !== null && _b !== void 0 ? _b : 0;
|
|
136
|
+
const b = this.circuit.remainingCooldownMs();
|
|
137
|
+
const wait = Math.max(h, b, 1000);
|
|
138
|
+
this.opts.warn(`429 throttled; scheduling in ${wait}ms (retry-after=${h}, breaker=${b})`);
|
|
139
|
+
this.scheduleFlushIn(wait);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
this.opts.warn("Throttle received but client resetting/disabled — dropping chunk");
|
|
143
|
+
}
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (s === 401 || s === 403 || s === 404) {
|
|
147
|
+
this.opts.error(`Fatal config error ${s}. Disabling client.`);
|
|
148
|
+
this.queue.length = 0;
|
|
149
|
+
this.stop();
|
|
150
|
+
(_d = (_c = this.opts).onFatalConfig) === null || _d === void 0 ? void 0 : _d.call(_c);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (s === 413) {
|
|
154
|
+
this.circuit.onNonRetryable();
|
|
155
|
+
if (this.maxBatchSize > 1) {
|
|
156
|
+
this.maxBatchSize = Math.max(1, Math.floor(this.maxBatchSize / 2));
|
|
157
|
+
this.opts.warn(`Payload too large; reducing maxBatchSize to ${this.maxBatchSize}`);
|
|
158
|
+
this.queue.unshift(...chunk);
|
|
159
|
+
this.scheduleFlushIn(500);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
const ids = chunk
|
|
163
|
+
.map((e) => e.messageId)
|
|
164
|
+
.join(",");
|
|
165
|
+
this.opts.warn(`Dropping oversize event(s) after 413 at batchSize=1; messageIds=${ids}`);
|
|
166
|
+
}
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
// Other 4xx: drop bad payload
|
|
170
|
+
this.circuit.onNonRetryable();
|
|
171
|
+
this.opts.warn(`Dropping batch due to client error ${s}`);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
// Success
|
|
175
|
+
this.circuit.onSuccess();
|
|
176
|
+
this.opts.log("API call successful");
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
this.circuit.onFailure();
|
|
180
|
+
if (this.opts.isOperational()) {
|
|
181
|
+
this.queue.unshift(...chunk);
|
|
182
|
+
const wait = Math.max(this.circuit.remainingCooldownMs(), 1000);
|
|
183
|
+
this.opts.warn("Flush attempt failed; scheduling retry in", wait, "ms", err === null || err === void 0 ? void 0 : err.message);
|
|
184
|
+
this.scheduleFlushIn(wait);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
this.opts.warn("Flush failed during reset/disable — dropping current chunk");
|
|
188
|
+
}
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
this.opts.log("Flush completed successfully");
|
|
193
|
+
};
|
|
194
|
+
this.flushInFlight = doFlush().finally(() => {
|
|
195
|
+
this.flushInFlight = null;
|
|
196
|
+
});
|
|
197
|
+
return this.flushInFlight;
|
|
198
|
+
}
|
|
199
|
+
getDebugInfo() {
|
|
200
|
+
return {
|
|
201
|
+
queueLength: this.queue.length,
|
|
202
|
+
flushIntervalSeconds: this.opts.flushIntervalSeconds,
|
|
203
|
+
proxy: false,
|
|
204
|
+
flushInFlight: !!this.flushInFlight,
|
|
205
|
+
circuitState: this.circuit.getState(),
|
|
206
|
+
circuitRemainingMs: this.circuit.remainingCooldownMs(),
|
|
207
|
+
maxQueueEvents: this.opts.maxQueueEvents,
|
|
208
|
+
maxBatchSize: this.maxBatchSize,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
}
|