@eliware/elera-lib 0.1.5 → 0.1.7
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/README.md +22 -3
- package/RELEASE_NOTES.md +44 -0
- package/package.json +1 -1
- package/src/client/create-db.mjs +1 -1
- package/src/index.d.ts +8 -4
- package/src/index.mjs +1 -0
- package/src/routing/event-contract.mjs +11 -0
- package/src/routing/stream-client.mjs +19 -7
- package/src/telemetry.mjs +3 -2
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
The alternative SQL client for Eliware applications. It provides generic
|
|
4
4
|
primary/balanced MySQL or MariaDB routing without embedding Elera, HAProxy,
|
|
5
|
-
backup, or GitOps policy. It is a v0.1.
|
|
5
|
+
backup, or GitOps policy. It is a v0.1.7 alternative to `@eliware/mysql`; the
|
|
6
6
|
existing package is intentionally unchanged. The current package version is
|
|
7
|
-
0.1.
|
|
7
|
+
0.1.7.
|
|
8
8
|
|
|
9
9
|
`primary` is the preferred connection path. `balanced` is an optional alternate
|
|
10
10
|
path. Both may accept writes; automatic routing sends only conservative,
|
|
@@ -58,6 +58,24 @@ state. Only conservative, single-statement reads are eligible for automatic
|
|
|
58
58
|
retry after a connection failure; uncertain writes are never retried
|
|
59
59
|
automatically.
|
|
60
60
|
|
|
61
|
+
When an attached routing stream receives a `routing.shutdown` event, the
|
|
62
|
+
client drains the identified node, performs a REST bundle resynchronization,
|
|
63
|
+
and closes the retiring WebSocket with restart code `1012`. If the event
|
|
64
|
+
contains `loadBalancerEndpoint`, that endpoint replaces the current endpoint
|
|
65
|
+
before resynchronization and reconnect. `reconnectDeadlineMs` bounds the
|
|
66
|
+
planned reconnect window; after it expires, no new reconnect is scheduled.
|
|
67
|
+
Reconnects, failovers, and measured reconnect delay are included in telemetry.
|
|
68
|
+
If the WebSocket remains unavailable before the deadline, REST
|
|
69
|
+
resynchronization and bounded reconnect backoff continue until the deadline or
|
|
70
|
+
until the caller closes the stream.
|
|
71
|
+
|
|
72
|
+
Routing events are validated before application. Shutdown events may include
|
|
73
|
+
`node`, `reason`, `reconnect`, `reconnectDeadlineMs`, and
|
|
74
|
+
`loadBalancerEndpoint`; invalid event fields are reported through `onError` and
|
|
75
|
+
do not change routing state. `routing.update` replaces the writer and reader
|
|
76
|
+
pools atomically, while `routing.drain` excludes only the named node from new
|
|
77
|
+
work and allows active operations to finish.
|
|
78
|
+
|
|
61
79
|
The public client intentionally exposes SQL operations, health, routing,
|
|
62
80
|
lifecycle, and optional routing-event synchronization methods. REST and
|
|
63
81
|
WebSocket transports are adapters, not supervisor or CLI policy. Underlying
|
|
@@ -73,7 +91,8 @@ Applications may opt into generic in-memory telemetry with
|
|
|
73
91
|
`createDb({ ..., telemetry: true })`. Query counts, failures, retries,
|
|
74
92
|
in-flight work, and latency are exposed through `client.telemetry` and sent
|
|
75
93
|
over an attached routing stream once per second. Telemetry is observational
|
|
76
|
-
only; it does not carry SQL or credentials.
|
|
94
|
+
only; it does not carry SQL or credentials. Reconnect, failover, and cumulative
|
|
95
|
+
reconnect-delay counters are included in the telemetry snapshot.
|
|
77
96
|
|
|
78
97
|
`createMaterializer` supports bounded plaintext use for a caller-provided
|
|
79
98
|
operation. It creates a mode-restricted temporary file and removes its entire
|
package/RELEASE_NOTES.md
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
# Release notes
|
|
2
2
|
|
|
3
|
+
## 0.1.7 — Routing shutdown contract completion
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Validates supervisor routing-control events before applying them.
|
|
8
|
+
- Supports node-specific shutdown events so only the retiring SQL node is
|
|
9
|
+
drained by the client.
|
|
10
|
+
- Honors an optional `loadBalancerEndpoint` supplied during shutdown before
|
|
11
|
+
REST resynchronization and WebSocket reconnect.
|
|
12
|
+
- Honors `reconnectDeadlineMs` and stops scheduling reconnect attempts after
|
|
13
|
+
the deadline expires.
|
|
14
|
+
- Exposes the active endpoint and reconnect deadline in routing-stream state.
|
|
15
|
+
- Publishes typed routing-event declarations for TypeScript consumers.
|
|
16
|
+
|
|
17
|
+
### Validation
|
|
18
|
+
|
|
19
|
+
- Adds regression coverage for node-specific shutdown draining, endpoint
|
|
20
|
+
replacement, deadline expiry, malformed events, and reconnect behavior.
|
|
21
|
+
- Maintains 100% statements, branches, functions, and lines coverage with
|
|
22
|
+
zero lint warnings.
|
|
23
|
+
- Typecheck and diff validation pass.
|
|
24
|
+
|
|
25
|
+
## 0.1.6 — Graceful routing shutdown handoff
|
|
26
|
+
|
|
27
|
+
### Added
|
|
28
|
+
|
|
29
|
+
- Handles supervisor `routing.shutdown` events without exposing supervisor-
|
|
30
|
+
specific internals to applications.
|
|
31
|
+
- Drains the affected SQL node so in-flight work can finish while new work is
|
|
32
|
+
routed elsewhere.
|
|
33
|
+
- Closes the retiring WebSocket with restart code `1012` and reconnects through
|
|
34
|
+
the configured load-balancer endpoint.
|
|
35
|
+
- Performs an immediate REST routing-bundle resynchronization when the stream
|
|
36
|
+
is being retired or temporarily unavailable.
|
|
37
|
+
- Tracks intentional reconnects, failovers, and reconnect delay in telemetry.
|
|
38
|
+
- Adds regression coverage for shutdown events, close codes, reconnects, REST
|
|
39
|
+
fallback, node draining, and telemetry behavior.
|
|
40
|
+
|
|
41
|
+
### Validation
|
|
42
|
+
|
|
43
|
+
- The complete test suite passes with 100% statements, branches, functions,
|
|
44
|
+
and lines coverage.
|
|
45
|
+
- Syntax and diff validation pass.
|
|
46
|
+
|
|
3
47
|
## 0.1.5 — Telemetry and convention alignment
|
|
4
48
|
|
|
5
49
|
### Added
|
package/package.json
CHANGED
package/src/client/create-db.mjs
CHANGED
|
@@ -36,7 +36,7 @@ export async function createDb({ primary, balanced, bundle, credentialProvider,
|
|
|
36
36
|
async transaction(callback) { const node = primaryPool.choose(); const connection = await node.getConnection(); try { await connection.beginTransaction(); const tx = { query: (sql, values) => connection.query(sql, values), execute: (sql, values) => connection.execute(sql, values) }; const result = await callback(tx); await connection.commit(); return result; } catch (error) { await connection.rollback().catch(() => {}); throw asSqlError(error); } finally { connection.release(); } },
|
|
37
37
|
async health(route = 'primary') { const started = now(); const selected = route === 'balanced' && balancedPool ? balancedPool : primaryPool; const nodes = await selected.health(); return { ok: nodes.some((node) => node.ok), route: selected === balancedPool ? 'balanced' : 'primary', nodes, latencyMs: now() - started }; },
|
|
38
38
|
async refresh(nextBundle) { const candidate = validateBundle(nextBundle); if (bundleExpired(candidate, now())) throw new Error('routing bundle is expired'); if (olderVersion(candidate.bundleVersion, activeBundle?.bundleVersion)) return { bundleVersion: activeBundle?.bundleVersion, refreshRequired: bundleNeedsRefresh(activeBundle, now()) }; const previous = [primaryPool, balancedPool]; const credentials = candidate.credentials ?? { username: primaryConfig.user, password: primaryConfig.password }; const writer = candidate.writer ?? candidate.routes.primary?.[0]; const reader = candidate.readers?.[0] ?? candidate.routes.balanced?.[0]; primaryConfig = validateProfile({ ...primaryConfig, host: writer?.host, port: writer?.port, user: credentials.username, password: credentials.password, database: candidate.database }, 'primary'); balancedConfig = reader ? validateProfile({ ...primaryConfig, host: reader.host, port: reader.port }, 'balanced') : undefined; activeBundle = candidate; primaryPool = makeRoute('primary', primaryConfig); balancedPool = balancedConfig ? makeRoute('balanced', balancedConfig) : null; await Promise.all(previous.filter(Boolean).map((pool) => pool.close())); return { bundleVersion: activeBundle.bundleVersion ?? null, refreshRequired: bundleNeedsRefresh(activeBundle, now()) }; },
|
|
39
|
-
async attachRoutingStream(stream) { if (!stream?.connect) throw new TypeError('routing stream is required'); metrics?.start?.(stream); stream.setOnUpdate?.(async (event) => { const update = event.type === 'routing.update' ? event : event.type === 'routing.resync' ? event.bundle : undefined; if (update && (update.writer || update.routes?.primary?.length)) await client.refresh({ ...activeBundle, ...update, database: update.database ?? activeBundle?.database ?? primaryConfig.database, credentials: update.credentials ?? activeBundle?.credentials, routes: update.routes ?? activeBundle?.routes, bundleVersion: update.bundleVersion ?? update.version ?? activeBundle?.bundleVersion, expiresAt: update.expiresAt ?? activeBundle?.expiresAt ?? new Date(now() + 60000).toISOString() }); if (event.type === 'routing.drain' || event.type === 'routing.
|
|
39
|
+
async attachRoutingStream(stream) { if (!stream?.connect) throw new TypeError('routing stream is required'); metrics?.start?.(stream); stream.setTelemetry?.(metrics); stream.setOnUpdate?.(async (event) => { const update = event.type === 'routing.update' ? event : event.type === 'routing.resync' ? event.bundle : undefined; if (update && (update.writer || update.routes?.primary?.length)) await client.refresh({ ...activeBundle, ...update, database: update.database ?? activeBundle?.database ?? primaryConfig.database, credentials: update.credentials ?? activeBundle?.credentials, routes: update.routes ?? activeBundle?.routes, bundleVersion: update.bundleVersion ?? update.version ?? activeBundle?.bundleVersion, expiresAt: update.expiresAt ?? activeBundle?.expiresAt ?? new Date(now() + 60000).toISOString() }); if (event.type === 'routing.drain' || event.type === 'routing.shutdown') for (const pool of [primaryPool, balancedPool].filter(Boolean)) (event.type === 'routing.drain' ? pool.drain : pool.drain)(event.node, drainTimeoutMs); if (event.type === 'routing.recovery') for (const pool of [primaryPool, balancedPool].filter(Boolean)) pool.recover(event.node, drainTimeoutMs); }); await stream.connect(); return () => stream.close?.(); },
|
|
40
40
|
drain(host, timeoutMs = drainTimeoutMs) { const effectiveTimeout = clientDrainTimeout(timeoutMs); const pools = [primaryPool, balancedPool].filter(Boolean); pools.forEach((pool) => pool.drain(host, effectiveTimeout)); return { host, timeoutMs: effectiveTimeout, wait: () => Promise.all(pools.map((pool) => pool.waitForIdle(effectiveTimeout))), forceClose: () => Promise.all(pools.map((pool) => pool.forceClose(host))) }; },
|
|
41
41
|
nodeStates() { return [primaryPool, balancedPool].filter(Boolean).flatMap((pool) => pool.nodes.map((node) => ({ host: node.host, port: node.port, route: pool === primaryPool ? 'primary' : 'balanced', state: node.state, active: node.active, available: node.available }))); },
|
|
42
42
|
setNodeAvailability(route, host, available) { const pool = route === 'balanced' ? balancedPool : primaryPool; pool?.setAvailability(host, available); },
|
package/src/index.d.ts
CHANGED
|
@@ -28,8 +28,8 @@ export interface CredentialProviderResult { user: string; password: string; }
|
|
|
28
28
|
export type CredentialProvider = (context: { database: string; identity: string | null; route: string }) => Promise<CredentialProviderResult> | CredentialProviderResult;
|
|
29
29
|
export type QueryFunction = (sql: string, values?: unknown) => Promise<any>;
|
|
30
30
|
export interface DbOptions { route?: 'auto' | 'primary' | 'balanced'; connection?: unknown; }
|
|
31
|
-
export interface TelemetrySnapshot { type: 'client.telemetry'; application: string; queries: number; failures: number; retries: number; reconnects: number; failoverCount: number; inflight: number; totalLatencyMs: number; maxLatencyMs: number; avgLatencyMs: number; sentAt: string; }
|
|
32
|
-
export interface Telemetry { begin(): number; record(event?: { latencyMs?: number; failed?: boolean; retry?: boolean; reconnect?: boolean; failover?: boolean }): void; snapshot(): TelemetrySnapshot; start(stream: Pick<RoutingStream, 'sendTelemetry'>): void; stop(): void; }
|
|
31
|
+
export interface TelemetrySnapshot { type: 'client.telemetry'; application: string; queries: number; failures: number; retries: number; reconnects: number; failoverCount: number; reconnectDelayMs: number; inflight: number; totalLatencyMs: number; maxLatencyMs: number; avgLatencyMs: number; sentAt: string; }
|
|
32
|
+
export interface Telemetry { begin(): number; record(event?: { latencyMs?: number; failed?: boolean; retry?: boolean; reconnect?: boolean; failover?: boolean }): void; recordReconnect(event?: { delayMs?: number; failover?: boolean }): void; snapshot(): TelemetrySnapshot; start(stream: Pick<RoutingStream, 'sendTelemetry'>): void; stop(): void; }
|
|
33
33
|
|
|
34
34
|
export interface DbClient {
|
|
35
35
|
query(sql: string, values?: unknown, options?: DbOptions): Promise<unknown>;
|
|
@@ -53,9 +53,13 @@ export interface RoutingStream {
|
|
|
53
53
|
setOnUpdate(handler: (event: unknown) => void | Promise<void>): void;
|
|
54
54
|
close(): void;
|
|
55
55
|
sendTelemetry(payload: unknown): void;
|
|
56
|
-
|
|
56
|
+
setTelemetry?(telemetry?: Pick<Telemetry, 'recordReconnect'>): void;
|
|
57
|
+
state(): { connected: boolean; mode: 'websocket' | 'rest' | 'disconnected'; expectedVersion: number | string; endpoint: string; reconnectDeadlineAt?: number };
|
|
57
58
|
}
|
|
58
59
|
|
|
60
|
+
export type RoutingEvent = { type: 'routing.update' | 'routing.resync' | 'routing.drain' | 'routing.recovery'; node?: string; version?: number | string; [key: string]: unknown } | { type: 'routing.shutdown'; node?: string; reason?: string; reconnect?: boolean; reconnectDeadlineMs?: number; loadBalancerEndpoint?: string };
|
|
61
|
+
export function validateRoutingEvent(event: unknown): RoutingEvent;
|
|
62
|
+
|
|
59
63
|
export function createDb(options: { primary: ConnectionProfile; balanced?: Partial<ConnectionProfile>; bundle?: RoutingBundle; credentialProvider?: CredentialProvider; identity?: string; mysqlLib?: unknown; log?: unknown; routing?: 'auto' | 'primary' | 'balanced'; quarantineMs?: number; drainTimeoutMs?: number; now?: () => number; telemetry?: true | Telemetry }): Promise<DbClient>;
|
|
60
64
|
export function createDbFromEnvironment(options?: { env?: Record<string, string | undefined>; mysqlLib?: unknown; log?: unknown; routing?: 'auto' | 'primary' | 'balanced'; bundle?: RoutingBundle; credentialProvider?: CredentialProvider; identity?: string; telemetry?: true | Telemetry }): Promise<DbClient>;
|
|
61
65
|
export function classifyQuery(sql: unknown): 'primary' | 'balanced';
|
|
@@ -71,7 +75,7 @@ export function bundleNeedsRefresh(bundle: RoutingBundle, now?: number): boolean
|
|
|
71
75
|
export function createAdminSql(options: { query: QueryFunction }): { transaction<T>(work: (context: { query: QueryFunction }) => Promise<T>): Promise<T>; migration(statements?: string[]): Promise<unknown> };
|
|
72
76
|
export function createMigrationRunner(options: { query: QueryFunction; migrations?: Array<{ version: number; name: string; statements: string[] }> }): { status(): Promise<{ applied: unknown[] }>; migrate(): Promise<unknown> };
|
|
73
77
|
export function selectRouteNodes(options: { bundle: RoutingBundle; route?: 'primary' | 'balanced'; now?: number }): RoutingNode[];
|
|
74
|
-
export function createRoutingStream(options: { endpoint: string; token?: string; application?: string; fetchBundle: (application: string) => Promise<RoutingBundle>; onUpdate?: (event: unknown) => void; onError?: (error: unknown) => void; reconnectMs?: number; maxReconnectMs?: number; heartbeatMs?: number }): RoutingStream;
|
|
78
|
+
export function createRoutingStream(options: { endpoint: string; token?: string; application?: string; fetchBundle: (application: string) => Promise<RoutingBundle>; onUpdate?: (event: unknown) => void; onError?: (error: unknown) => void; reconnectMs?: number; maxReconnectMs?: number; heartbeatMs?: number; telemetry?: Pick<Telemetry, 'recordReconnect'> }): RoutingStream;
|
|
75
79
|
export function writerAssignment(bundle: RoutingBundle): WriterAssignment;
|
|
76
80
|
export function failoverNodes(bundle: RoutingBundle): WriterAssignment[];
|
|
77
81
|
export function compareBundleVersions(left: number | string | undefined, right: number | string | undefined): number;
|
package/src/index.mjs
CHANGED
|
@@ -9,6 +9,7 @@ export { createAdminSql } from './admin/sql.mjs';
|
|
|
9
9
|
export { createMigrationRunner } from './admin/migrations.mjs';
|
|
10
10
|
export { selectRouteNodes } from './routing/node-set.mjs';
|
|
11
11
|
export { createRoutingStream } from './routing/stream-client.mjs';
|
|
12
|
+
export { validateRoutingEvent } from './routing/event-contract.mjs';
|
|
12
13
|
export { writerAssignment, failoverNodes } from './routing/assignment.mjs';
|
|
13
14
|
export { compareBundleVersions } from './routing/bundle-version.mjs';
|
|
14
15
|
export { CLIENT_DRAIN_TIMEOUT_MS, clientDrainTimeout } from './lifecycle/drain-policy.mjs';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const eventTypes = new Set(['routing.update', 'routing.resync', 'routing.drain', 'routing.recovery', 'routing.shutdown']);
|
|
2
|
+
|
|
3
|
+
export function validateRoutingEvent(event) {
|
|
4
|
+
if (!event || typeof event !== 'object' || !eventTypes.has(event.type)) throw new TypeError('unsupported routing event');
|
|
5
|
+
if (event.type === 'routing.shutdown') {
|
|
6
|
+
if (event.node !== undefined && typeof event.node !== 'string') throw new TypeError('routing shutdown node must be a string');
|
|
7
|
+
if (event.reconnectDeadlineMs !== undefined && (!Number.isFinite(Number(event.reconnectDeadlineMs)) || Number(event.reconnectDeadlineMs) < 0)) throw new TypeError('routing shutdown deadline must be non-negative');
|
|
8
|
+
if (event.loadBalancerEndpoint !== undefined && typeof event.loadBalancerEndpoint !== 'string') throw new TypeError('routing shutdown endpoint must be a string');
|
|
9
|
+
}
|
|
10
|
+
return event;
|
|
11
|
+
}
|
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
import { log as defaultLog } from '@eliware/common';
|
|
2
2
|
import { compareBundleVersions } from './bundle-version.mjs';
|
|
3
|
+
import { validateRoutingEvent } from './event-contract.mjs';
|
|
3
4
|
|
|
4
|
-
export function createRoutingStream({ endpoint, token, application = 'default', fetchBundle, WebSocketImpl = globalThis.WebSocket, onUpdate, onError, reconnectMs = 1000, maxReconnectMs = 30000, heartbeatMs = 45000, now = () => Date.now() } = {}) {
|
|
5
|
+
export function createRoutingStream({ endpoint, token, application = 'default', fetchBundle, WebSocketImpl = globalThis.WebSocket, onUpdate, onError, reconnectMs = 1000, maxReconnectMs = 30000, heartbeatMs = 45000, now = () => Date.now(), telemetry } = {}) {
|
|
5
6
|
if (!endpoint || typeof fetchBundle !== 'function') throw new TypeError('endpoint and fetchBundle are required');
|
|
6
|
-
let socket; let closed = false; let timer; let heartbeat; let expectedVersion = 0; let delay = reconnectMs; let updateHandler = onUpdate; let mode = 'disconnected';
|
|
7
|
+
let socket; let closed = false; let timer; let heartbeat; let expectedVersion = 0; let delay = reconnectMs; let updateHandler = onUpdate; let mode = 'disconnected'; let plannedReconnect = false; let lastReconnectWasPlanned = false; let disconnectedAt; let reconnectDeadlineAt;
|
|
7
8
|
const log = arguments[0]?.log ?? defaultLog;
|
|
8
9
|
const streamUrl = () => `${endpoint.replace(/^http/i, 'ws').replace(/\/$/, '')}/api/v1/routing/stream?application=${encodeURIComponent(application)}&token=${encodeURIComponent(token ?? '')}`;
|
|
9
10
|
async function fallback() { try { const bundle = await fetchBundle(application); if (closed) return; mode = 'rest'; updateHandler?.({ type: 'routing.resync', version: expectedVersion, bundle, receivedAt: now() }); } catch (error) { if (closed) return; mode = 'disconnected'; onError?.(error); log.warn?.('Routing REST fallback failed', { error }); } }
|
|
10
|
-
function schedule() { if (closed || timer) return; timer = setTimeout(() => { timer = undefined; void connect(); },
|
|
11
|
+
function schedule() { if (closed || timer || (reconnectDeadlineAt !== undefined && now() >= reconnectDeadlineAt)) return; const wait = Math.min(delay, Math.max(0, reconnectDeadlineAt === undefined ? delay : reconnectDeadlineAt - now())); timer = setTimeout(() => { timer = undefined; void connect(); }, wait); delay = Math.min(maxReconnectMs, delay * 2); }
|
|
11
12
|
async function connect() {
|
|
12
13
|
if (closed || typeof WebSocketImpl !== 'function') { await fallback(); schedule(); return; }
|
|
13
14
|
try {
|
|
14
15
|
socket = new WebSocketImpl(streamUrl());
|
|
15
|
-
socket.onopen = () => { mode = 'websocket'; delay = reconnectMs; heartbeat = setInterval(() => socket?.send?.(JSON.stringify({ type: 'heartbeat', sentAt: now() })), heartbeatMs); };
|
|
16
|
+
socket.onopen = () => { mode = 'websocket'; reconnectDeadlineAt = undefined; if (disconnectedAt !== undefined) { telemetry?.recordReconnect?.({ delayMs: Math.max(0, now() - disconnectedAt), failover: lastReconnectWasPlanned }); disconnectedAt = undefined; lastReconnectWasPlanned = false; } delay = reconnectMs; heartbeat = setInterval(() => socket?.send?.(JSON.stringify({ type: 'heartbeat', sentAt: now() })), heartbeatMs); };
|
|
16
17
|
socket.onmessage = async ({ data }) => {
|
|
17
18
|
try {
|
|
18
|
-
const event = JSON.parse(data);
|
|
19
|
+
const event = validateRoutingEvent(JSON.parse(data));
|
|
20
|
+
if (event.type === 'routing.shutdown') {
|
|
21
|
+
updateHandler?.(event);
|
|
22
|
+
if (typeof event.loadBalancerEndpoint === 'string' && event.loadBalancerEndpoint) endpoint = event.loadBalancerEndpoint;
|
|
23
|
+
reconnectDeadlineAt = Number.isFinite(Number(event.reconnectDeadlineMs)) ? now() + Math.max(0, Number(event.reconnectDeadlineMs)) : undefined;
|
|
24
|
+
plannedReconnect = true;
|
|
25
|
+
delay = reconnectMs;
|
|
26
|
+
await fallback();
|
|
27
|
+
socket?.close?.(1012, 'supervisor restarting');
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const version = event.version;
|
|
19
31
|
if (version !== undefined && expectedVersion !== 0 && compareBundleVersions(version, expectedVersion) <= 0) return;
|
|
20
32
|
const numericVersion = Number(version); const numericExpected = Number(expectedVersion);
|
|
21
33
|
if (Number.isInteger(numericVersion) && Number.isInteger(numericExpected) && numericExpected > 0 && numericVersion > numericExpected + 1) await fallback();
|
|
@@ -24,8 +36,8 @@ export function createRoutingStream({ endpoint, token, application = 'default',
|
|
|
24
36
|
} catch (error) { onError?.(error); }
|
|
25
37
|
};
|
|
26
38
|
socket.onerror = (error) => { onError?.(error); };
|
|
27
|
-
socket.onclose = () => { clearInterval(heartbeat); heartbeat = undefined; socket = undefined; mode = 'disconnected'; if (!closed) { void fallback(); schedule(); } };
|
|
39
|
+
socket.onclose = () => { clearInterval(heartbeat); heartbeat = undefined; socket = undefined; mode = 'disconnected'; if (!closed) { disconnectedAt = now(); lastReconnectWasPlanned = plannedReconnect; if (!plannedReconnect) void fallback(); plannedReconnect = false; schedule(); } };
|
|
28
40
|
} catch (error) { mode = 'disconnected'; onError?.(error); await fallback(); schedule(); }
|
|
29
41
|
}
|
|
30
|
-
return { connect, sendTelemetry: (payload) => { if (socket?.readyState === 1) socket.send(JSON.stringify(payload)); }, setOnUpdate: (handler) => { updateHandler = handler; }, close: () => { closed = true; mode = 'disconnected'; clearTimeout(timer); clearInterval(heartbeat); socket?.close?.(); }, state: () => ({ connected: socket?.readyState === 1, mode, expectedVersion }) };
|
|
42
|
+
return { connect, sendTelemetry: (payload) => { if (socket?.readyState === 1) socket.send(JSON.stringify(payload)); }, setOnUpdate: (handler) => { updateHandler = handler; }, setTelemetry: (value) => { telemetry = value; }, close: () => { closed = true; mode = 'disconnected'; clearTimeout(timer); clearInterval(heartbeat); socket?.close?.(); }, state: () => ({ connected: socket?.readyState === 1, mode, expectedVersion, endpoint, reconnectDeadlineAt }) };
|
|
31
43
|
}
|
package/src/telemetry.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export function createTelemetry({ application = 'default', intervalMs = 1000, now = () => Date.now(), setIntervalImpl = setInterval, clearIntervalImpl = clearInterval } = {}) {
|
|
2
|
-
const stats = { queries: 0, failures: 0, retries: 0, reconnects: 0, failoverCount: 0, inflight: 0, totalLatencyMs: 0, maxLatencyMs: 0 };
|
|
2
|
+
const stats = { queries: 0, failures: 0, retries: 0, reconnects: 0, failoverCount: 0, reconnectDelayMs: 0, inflight: 0, totalLatencyMs: 0, maxLatencyMs: 0 };
|
|
3
3
|
let timer;
|
|
4
4
|
const begin = () => { stats.inflight += 1; return now(); };
|
|
5
5
|
const record = ({ latencyMs = 0, failed = false, retry = false, reconnect = false, failover = false } = {}) => { stats.queries += 1; stats.inflight = Math.max(0, stats.inflight - 1); stats.totalLatencyMs += latencyMs; stats.maxLatencyMs = Math.max(stats.maxLatencyMs, latencyMs); if (failed) stats.failures += 1; if (retry) stats.retries += 1; if (reconnect) stats.reconnects += 1; if (failover) stats.failoverCount += 1; };
|
|
6
6
|
const snapshot = () => ({ type: 'client.telemetry', application, ...stats, avgLatencyMs: stats.queries ? stats.totalLatencyMs / stats.queries : 0, sentAt: new Date(now()).toISOString() });
|
|
7
|
-
|
|
7
|
+
const recordReconnect = ({ delayMs = 0, failover = false } = {}) => { stats.reconnects += 1; stats.reconnectDelayMs += Math.max(0, Number(delayMs) || 0); if (failover) stats.failoverCount += 1; };
|
|
8
|
+
return { begin, record, recordReconnect, snapshot, start(stream) { if (timer) return; timer = setIntervalImpl(() => stream.sendTelemetry?.(snapshot()), intervalMs); timer.unref?.(); }, stop() { if (timer) clearIntervalImpl(timer); timer = undefined; } };
|
|
8
9
|
}
|