rivetkit 0.0.0-sqlite-query-metrics-v2.f5d3605 → 0.0.0-sqlite-sync-3-0-1-release.95f6e2d
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/dist/browser/client.d.ts +99 -59
- package/dist/browser/client.js +228 -34
- package/dist/browser/client.js.map +1 -1
- package/dist/browser/inspector/client.js +1 -2
- package/dist/browser/inspector/client.js.map +1 -1
- package/dist/tsup/{chunk-3URZ4UV5.js → chunk-2HWY7FZU.js} +2 -2
- package/dist/tsup/{chunk-PJ5EXY6F.js → chunk-62WFU3BQ.js} +223 -42
- package/dist/tsup/chunk-62WFU3BQ.js.map +1 -0
- package/dist/tsup/{chunk-JI6GZ2C2.js → chunk-A2CF6PFH.js} +18 -1
- package/dist/tsup/chunk-A2CF6PFH.js.map +1 -0
- package/dist/tsup/{chunk-KDT56V3Y.cjs → chunk-AQWZWR2P.cjs} +10 -11
- package/dist/tsup/chunk-AQWZWR2P.cjs.map +1 -0
- package/dist/tsup/{chunk-L3Z3W44Q.cjs → chunk-E4KDQJCR.cjs} +3 -3
- package/dist/tsup/{chunk-L3Z3W44Q.cjs.map → chunk-E4KDQJCR.cjs.map} +1 -1
- package/dist/tsup/{chunk-DHL2M2E2.cjs → chunk-E6HKVCPA.cjs} +3 -3
- package/dist/tsup/{chunk-DHL2M2E2.cjs.map → chunk-E6HKVCPA.cjs.map} +1 -1
- package/dist/tsup/{chunk-DDS7OOVF.cjs → chunk-EQBUH25O.cjs} +362 -181
- package/dist/tsup/chunk-EQBUH25O.cjs.map +1 -0
- package/dist/tsup/{chunk-USKGKQZM.js → chunk-HQHTTJW5.js} +10 -11
- package/dist/tsup/chunk-HQHTTJW5.js.map +1 -0
- package/dist/tsup/{chunk-JW2WPE6Z.cjs → chunk-M5SQGPRJ.cjs} +4 -4
- package/dist/tsup/{chunk-JW2WPE6Z.cjs.map → chunk-M5SQGPRJ.cjs.map} +1 -1
- package/dist/tsup/{chunk-FYDB45YU.cjs → chunk-MYWNJP55.cjs} +10 -10
- package/dist/tsup/{chunk-FYDB45YU.cjs.map → chunk-MYWNJP55.cjs.map} +1 -1
- package/dist/tsup/{chunk-F7IGTKGM.js → chunk-OWZ76NSH.js} +2 -2
- package/dist/tsup/{chunk-KSP3NXGY.js → chunk-QE5MTZHB.js} +3 -3
- package/dist/tsup/{chunk-D4G35VTO.js → chunk-QFZY2CK3.js} +4 -4
- package/dist/tsup/{chunk-E2CUROR3.js → chunk-QIIIRYN7.js} +2 -2
- package/dist/tsup/{chunk-MYWZCDML.cjs → chunk-RTWH3AX3.cjs} +19 -2
- package/dist/tsup/chunk-RTWH3AX3.cjs.map +1 -0
- package/dist/tsup/{chunk-HO76AEDH.cjs → chunk-UHMB5FFB.cjs} +3 -3
- package/dist/tsup/{chunk-HO76AEDH.cjs.map → chunk-UHMB5FFB.cjs.map} +1 -1
- package/dist/tsup/{chunk-QR6EAWEC.cjs → chunk-VKXEBB3Z.cjs} +7 -7
- package/dist/tsup/{chunk-QR6EAWEC.cjs.map → chunk-VKXEBB3Z.cjs.map} +1 -1
- package/dist/tsup/{chunk-EPRA2TCT.js → chunk-ZMVYZ7ZI.js} +2 -2
- package/dist/tsup/client/mod.cjs +6 -6
- package/dist/tsup/client/mod.d.cts +15 -3
- package/dist/tsup/client/mod.d.ts +15 -3
- package/dist/tsup/client/mod.js +5 -5
- package/dist/tsup/common/log.cjs +2 -2
- package/dist/tsup/common/log.js +1 -1
- package/dist/tsup/common/websocket.cjs +3 -3
- package/dist/tsup/common/websocket.js +2 -2
- package/dist/tsup/{config-Cw_BwH_G.d.ts → config-B4erdavw.d.ts} +58 -12
- package/dist/tsup/{config-D4clT6mq.d.ts → config-BfG1qpYz.d.cts} +34 -52
- package/dist/tsup/{config-D4clT6mq.d.cts → config-BfG1qpYz.d.ts} +34 -52
- package/dist/tsup/{config-CkmVg2fG.d.cts → config-DK144OAw.d.cts} +58 -12
- package/dist/tsup/{context-_xIcgBnE.d.cts → context-Cvnf3BRe.d.cts} +2 -2
- package/dist/tsup/{context-BYOKTQJi.d.ts → context-Oxi6YWcG.d.ts} +2 -2
- package/dist/tsup/db/drizzle.cjs +2 -2
- package/dist/tsup/db/drizzle.d.cts +1 -1
- package/dist/tsup/db/drizzle.d.ts +1 -1
- package/dist/tsup/db/drizzle.js +1 -1
- package/dist/tsup/db/mod.cjs +3 -3
- package/dist/tsup/db/mod.d.cts +2 -2
- package/dist/tsup/db/mod.d.ts +2 -2
- package/dist/tsup/db/mod.js +2 -2
- package/dist/tsup/dynamic/mod.d.cts +2 -2
- package/dist/tsup/dynamic/mod.d.ts +2 -2
- package/dist/tsup/inspector/mod.cjs +5 -5
- package/dist/tsup/inspector/mod.js +4 -4
- package/dist/tsup/inspector/workflow.cjs +4 -4
- package/dist/tsup/inspector/workflow.js +3 -3
- package/dist/tsup/inspector-tab/mod.d.cts +3 -3
- package/dist/tsup/inspector-tab/mod.d.ts +3 -3
- package/dist/tsup/mod.cjs +553 -202
- package/dist/tsup/mod.cjs.map +1 -1
- package/dist/tsup/mod.d.cts +4 -4
- package/dist/tsup/mod.d.ts +4 -4
- package/dist/tsup/mod.js +467 -116
- package/dist/tsup/mod.js.map +1 -1
- package/dist/tsup/test/mod.cjs +7 -7
- package/dist/tsup/test/mod.d.cts +2 -2
- package/dist/tsup/test/mod.d.ts +2 -2
- package/dist/tsup/test/mod.js +5 -5
- package/dist/tsup/unstable/migrations.d.cts +1 -1
- package/dist/tsup/unstable/migrations.d.ts +1 -1
- package/dist/tsup/utils.cjs +2 -2
- package/dist/tsup/utils.js +1 -1
- package/dist/tsup/workflow/mod.cjs +22 -19
- package/dist/tsup/workflow/mod.cjs.map +1 -1
- package/dist/tsup/workflow/mod.d.cts +4 -4
- package/dist/tsup/workflow/mod.d.ts +4 -4
- package/dist/tsup/workflow/mod.js +10 -7
- package/dist/tsup/workflow/mod.js.map +1 -1
- package/package.json +9 -10
- package/src/actor/keys.ts +86 -0
- package/src/client/actor-conn.ts +136 -24
- package/src/client/actor-handle.ts +21 -3
- package/src/client/client.ts +6 -0
- package/src/client/config.ts +12 -1
- package/src/client/lifecycle-errors.ts +35 -2
- package/src/client/retry-budget.ts +78 -0
- package/src/client/utils.ts +13 -2
- package/src/common/database/config.ts +46 -52
- package/src/common/database/native-database.ts +247 -27
- package/src/common/router.ts +1 -1
- package/src/db/mod.ts +0 -1
- package/src/engine-api/actors.ts +3 -3
- package/src/engine-client/actor-websocket-client.ts +4 -2
- package/src/engine-client/api-endpoints.ts +44 -14
- package/src/engine-client/mod.ts +2 -2
- package/src/registry/config/envoy.ts +1 -1
- package/src/registry/config/index.ts +29 -31
- package/src/registry/index.ts +66 -21
- package/src/registry/napi-runtime.ts +82 -3
- package/src/registry/native.ts +184 -52
- package/src/registry/runtime.ts +79 -10
- package/src/registry/wasm-runtime.ts +94 -3
- package/src/utils/env-vars.ts +15 -10
- package/src/workflow/driver.ts +5 -3
- package/dist/tsup/chunk-DDS7OOVF.cjs.map +0 -1
- package/dist/tsup/chunk-JI6GZ2C2.js.map +0 -1
- package/dist/tsup/chunk-KDT56V3Y.cjs.map +0 -1
- package/dist/tsup/chunk-MYWZCDML.cjs.map +0 -1
- package/dist/tsup/chunk-PJ5EXY6F.js.map +0 -1
- package/dist/tsup/chunk-USKGKQZM.js.map +0 -1
- package/src/actor/config.test.ts +0 -78
- package/src/actor/keys.test.ts +0 -275
- package/src/actor/utils.test.ts +0 -48
- package/src/client/test.ts +0 -44
- package/src/client/token-provider.test.ts +0 -86
- package/src/common/database/mod.test.ts +0 -364
- package/src/common/database/native-database.test.ts +0 -220
- package/src/common/inline-websocket-adapter.test.ts +0 -51
- package/src/db/drizzle.test.ts +0 -164
- package/src/registry/config/index.test.ts +0 -25
- package/src/registry/run-handler-coordinator.test.ts +0 -252
- package/src/registry/runtime.test.ts +0 -124
- package/src/unstable/migrations.test.ts +0 -170
- package/src/utils/endpoint-parser.test.ts +0 -206
- package/src/utils.test.ts +0 -34
- package/src/workflow/driver.test.ts +0 -62
- /package/dist/tsup/{chunk-3URZ4UV5.js.map → chunk-2HWY7FZU.js.map} +0 -0
- /package/dist/tsup/{chunk-F7IGTKGM.js.map → chunk-OWZ76NSH.js.map} +0 -0
- /package/dist/tsup/{chunk-KSP3NXGY.js.map → chunk-QE5MTZHB.js.map} +0 -0
- /package/dist/tsup/{chunk-D4G35VTO.js.map → chunk-QFZY2CK3.js.map} +0 -0
- /package/dist/tsup/{chunk-E2CUROR3.js.map → chunk-QIIIRYN7.js.map} +0 -0
- /package/dist/tsup/{chunk-EPRA2TCT.js.map → chunk-ZMVYZ7ZI.js.map} +0 -0
package/src/client/actor-conn.ts
CHANGED
|
@@ -51,7 +51,6 @@ import { ACTOR_CONNS_SYMBOL, type ClientRaw } from "./client";
|
|
|
51
51
|
import * as errors from "./errors";
|
|
52
52
|
import { isRetryableLifecycleReconnectSignal } from "./lifecycle-errors";
|
|
53
53
|
import { logger } from "./log";
|
|
54
|
-
import { isInvalidToken } from "./token-provider";
|
|
55
54
|
import { outboundTelemetryHeaders } from "./outbound-telemetry";
|
|
56
55
|
import {
|
|
57
56
|
createQueueSender,
|
|
@@ -60,6 +59,8 @@ import {
|
|
|
60
59
|
type QueueSendResult,
|
|
61
60
|
type QueueSendWaitOptions,
|
|
62
61
|
} from "./queue";
|
|
62
|
+
import { retryAfterMs } from "./retry-budget";
|
|
63
|
+
import { isInvalidToken } from "./token-provider";
|
|
63
64
|
import {
|
|
64
65
|
type WebSocketMessage as ConnMessage,
|
|
65
66
|
messageLength,
|
|
@@ -104,6 +105,21 @@ interface EventSubscriptions<Args extends Array<unknown>> {
|
|
|
104
105
|
*/
|
|
105
106
|
export type EventUnsubscribe = () => void;
|
|
106
107
|
|
|
108
|
+
/**
|
|
109
|
+
* How long a connection attempt may wait for the actor's Init before it is abandoned and
|
|
110
|
+
* retried. Longer than the engine's own WebSocket open deadline (15 s by default), so an engine
|
|
111
|
+
* that answers with a typed close wins, while a socket that never settles is still retried.
|
|
112
|
+
*/
|
|
113
|
+
const CONNECT_TIMEOUT_MS = 20_000;
|
|
114
|
+
|
|
115
|
+
function isConnectionOpenTimeout(error: unknown): boolean {
|
|
116
|
+
return (
|
|
117
|
+
error instanceof errors.ActorError &&
|
|
118
|
+
error.group === "client" &&
|
|
119
|
+
error.code === "connection_open_timeout"
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
107
123
|
/**
|
|
108
124
|
* A function that handles connection errors.
|
|
109
125
|
*
|
|
@@ -144,6 +160,8 @@ export class ActorConnRaw {
|
|
|
144
160
|
#abortController = new AbortController();
|
|
145
161
|
|
|
146
162
|
#connStatus: ActorConnStatus = "idle";
|
|
163
|
+
/** The opening failure that stopped retries. Later actions must fail instead of queueing. */
|
|
164
|
+
#connectionOpenError?: errors.ActorError;
|
|
147
165
|
|
|
148
166
|
#actorId?: string;
|
|
149
167
|
#connId?: string;
|
|
@@ -182,6 +200,8 @@ export class ActorConnRaw {
|
|
|
182
200
|
|
|
183
201
|
/** Promise used to indicate the socket has connected successfully. This will be rejected if the connection fails. */
|
|
184
202
|
#onOpenPromise?: ReturnType<typeof promiseWithResolvers<undefined>>;
|
|
203
|
+
/** Sockets given up on by the connect timeout. Their late events are ignored. */
|
|
204
|
+
#abandonedSockets = new WeakSet<UniversalWebSocket>();
|
|
185
205
|
|
|
186
206
|
#websocket?: UniversalWebSocket;
|
|
187
207
|
#authRetryAvailable = true;
|
|
@@ -338,6 +358,8 @@ export class ActorConnRaw {
|
|
|
338
358
|
`Invalid action call: expected an options object { name, args }, got ${typeof opts}. Use conn.actionName(...args) for the shorthand API.`,
|
|
339
359
|
);
|
|
340
360
|
}
|
|
361
|
+
if (this.#disposed) throw new errors.ActorConnDisposed();
|
|
362
|
+
if (this.#connectionOpenError) throw this.#connectionOpenError;
|
|
341
363
|
logger().debug({ msg: "action", name: opts.name, args: opts.args });
|
|
342
364
|
|
|
343
365
|
// If we have an active connection, use the websockactionId
|
|
@@ -448,6 +470,7 @@ export class ActorConnRaw {
|
|
|
448
470
|
}
|
|
449
471
|
|
|
450
472
|
#connectWithRetry() {
|
|
473
|
+
this.#connectionOpenError = undefined;
|
|
451
474
|
this.#setConnStatus("connecting");
|
|
452
475
|
|
|
453
476
|
// Attempt to reconnect indefinitely
|
|
@@ -457,12 +480,17 @@ export class ActorConnRaw {
|
|
|
457
480
|
minTimeout: 250,
|
|
458
481
|
maxTimeout: 30_000,
|
|
459
482
|
|
|
460
|
-
onFailedAttempt: (error) => {
|
|
483
|
+
onFailedAttempt: async (error) => {
|
|
461
484
|
logger().warn({
|
|
462
485
|
msg: "failed to reconnect",
|
|
463
486
|
attempt: error.attemptNumber,
|
|
464
487
|
error: stringifyError(error),
|
|
465
488
|
});
|
|
489
|
+
// A close that carried the service's retry hint waits at least that long.
|
|
490
|
+
const hintMs = retryAfterMs(error);
|
|
491
|
+
if (hintMs !== undefined) {
|
|
492
|
+
await new Promise((resolve) => setTimeout(resolve, hintMs));
|
|
493
|
+
}
|
|
466
494
|
},
|
|
467
495
|
|
|
468
496
|
// Cancel retry if aborted
|
|
@@ -498,8 +526,13 @@ export class ActorConnRaw {
|
|
|
498
526
|
await this.#connectWebSocket();
|
|
499
527
|
|
|
500
528
|
// Wait for result
|
|
501
|
-
await this.#onOpenPromise.promise;
|
|
529
|
+
await this.#waitForOpen(this.#onOpenPromise.promise);
|
|
502
530
|
} catch (error) {
|
|
531
|
+
this.#abandonCurrentSocket(
|
|
532
|
+
isConnectionOpenTimeout(error)
|
|
533
|
+
? "Connect timeout"
|
|
534
|
+
: "Connection opening failed",
|
|
535
|
+
);
|
|
503
536
|
if (this.#shouldRetryConnectionOpenError(error)) {
|
|
504
537
|
throw error;
|
|
505
538
|
}
|
|
@@ -514,7 +547,7 @@ export class ActorConnRaw {
|
|
|
514
547
|
{ error: stringifyError(error) },
|
|
515
548
|
);
|
|
516
549
|
|
|
517
|
-
this.#
|
|
550
|
+
this.#connectionOpenError = actorError;
|
|
518
551
|
this.#rejectPendingPromises(actorError, false);
|
|
519
552
|
this.#dispatchActorError(actorError);
|
|
520
553
|
this.#setConnStatus("idle");
|
|
@@ -529,10 +562,67 @@ export class ActorConnRaw {
|
|
|
529
562
|
}
|
|
530
563
|
}
|
|
531
564
|
|
|
565
|
+
/**
|
|
566
|
+
* Waits for the actor's Init on the current socket, up to {@link CONNECT_TIMEOUT_MS}. A socket
|
|
567
|
+
* can stay connecting with no open, error or close event, as when the engine holds the
|
|
568
|
+
* upgrade or a refused connect reports only an error. On timeout the socket is abandoned
|
|
569
|
+
* and closed, and the retryable `client.connection_open_timeout` starts the next attempt.
|
|
570
|
+
*/
|
|
571
|
+
async #waitForOpen(open: Promise<undefined>) {
|
|
572
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
573
|
+
const timedOut = new Promise<never>((_, reject) => {
|
|
574
|
+
timer = setTimeout(() => {
|
|
575
|
+
reject(
|
|
576
|
+
new errors.ActorError(
|
|
577
|
+
"client",
|
|
578
|
+
"connection_open_timeout",
|
|
579
|
+
`Connection did not open within ${CONNECT_TIMEOUT_MS} ms`,
|
|
580
|
+
),
|
|
581
|
+
);
|
|
582
|
+
}, CONNECT_TIMEOUT_MS);
|
|
583
|
+
});
|
|
584
|
+
try {
|
|
585
|
+
await Promise.race([open, timedOut]);
|
|
586
|
+
} finally {
|
|
587
|
+
clearTimeout(timer);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
#abandonCurrentSocket(reason: string) {
|
|
592
|
+
const ws = this.#websocket;
|
|
593
|
+
if (!ws) return;
|
|
594
|
+
this.#websocket = undefined;
|
|
595
|
+
this.#abandonedSockets.add(ws);
|
|
596
|
+
logger().debug({
|
|
597
|
+
msg: "closing failed connection attempt",
|
|
598
|
+
connId: this.#connId,
|
|
599
|
+
readyState: ws.readyState,
|
|
600
|
+
reason,
|
|
601
|
+
});
|
|
602
|
+
try {
|
|
603
|
+
ws.close(1000, reason);
|
|
604
|
+
} catch (err) {
|
|
605
|
+
logger().debug({
|
|
606
|
+
msg: "failed to close abandoned websocket",
|
|
607
|
+
error: stringifyError(err),
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
532
612
|
#shouldRetryConnectionOpenError(error: unknown): boolean {
|
|
533
613
|
if (error instanceof errors.ActorConnDisposed) {
|
|
534
614
|
return false;
|
|
535
615
|
}
|
|
616
|
+
if (isConnectionOpenTimeout(error)) {
|
|
617
|
+
return true;
|
|
618
|
+
}
|
|
619
|
+
if (
|
|
620
|
+
error instanceof errors.ActorError &&
|
|
621
|
+
error.group === "client" &&
|
|
622
|
+
error.code === "connection_open_interrupted"
|
|
623
|
+
) {
|
|
624
|
+
return true;
|
|
625
|
+
}
|
|
536
626
|
if (
|
|
537
627
|
error instanceof errors.ActorError &&
|
|
538
628
|
isInvalidToken(error.group, error.code) &&
|
|
@@ -614,14 +704,18 @@ export class ActorConnRaw {
|
|
|
614
704
|
});
|
|
615
705
|
this.#websocket = ws;
|
|
616
706
|
ws.addEventListener("open", () => {
|
|
707
|
+
if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
|
|
708
|
+
return;
|
|
617
709
|
logger().debug({
|
|
618
710
|
msg: "client websocket open",
|
|
619
711
|
connId: this.#connId,
|
|
620
712
|
});
|
|
621
713
|
});
|
|
622
714
|
ws.addEventListener("message", async (ev: { data: ConnMessage }) => {
|
|
715
|
+
if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
|
|
716
|
+
return;
|
|
623
717
|
try {
|
|
624
|
-
await this.#handleOnMessage(ev.data);
|
|
718
|
+
await this.#handleOnMessage(ev.data, ws);
|
|
625
719
|
} catch (err) {
|
|
626
720
|
logger().error({
|
|
627
721
|
msg: "error in websocket message handler",
|
|
@@ -630,6 +724,8 @@ export class ActorConnRaw {
|
|
|
630
724
|
}
|
|
631
725
|
});
|
|
632
726
|
ws.addEventListener("close", async (ev: Event | CloseEventLike) => {
|
|
727
|
+
if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
|
|
728
|
+
return;
|
|
633
729
|
try {
|
|
634
730
|
await this.#handleOnClose(ev);
|
|
635
731
|
} catch (err) {
|
|
@@ -640,6 +736,8 @@ export class ActorConnRaw {
|
|
|
640
736
|
}
|
|
641
737
|
});
|
|
642
738
|
ws.addEventListener("error", () => {
|
|
739
|
+
if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
|
|
740
|
+
return;
|
|
643
741
|
try {
|
|
644
742
|
this.#handleOnError();
|
|
645
743
|
} catch (err) {
|
|
@@ -721,7 +819,7 @@ export class ActorConnRaw {
|
|
|
721
819
|
}
|
|
722
820
|
|
|
723
821
|
/** Called by the onmessage event from drivers. */
|
|
724
|
-
async #handleOnMessage(data: any) {
|
|
822
|
+
async #handleOnMessage(data: any, socket: UniversalWebSocket) {
|
|
725
823
|
logger().trace({
|
|
726
824
|
msg: "received message",
|
|
727
825
|
dataType: typeof data,
|
|
@@ -730,6 +828,10 @@ export class ActorConnRaw {
|
|
|
730
828
|
});
|
|
731
829
|
|
|
732
830
|
const response = await this.#parseMessage(data as ConnMessage);
|
|
831
|
+
// Decoding can yield while this socket closes and another connects. Apply
|
|
832
|
+
// the message only while its socket still owns the connection.
|
|
833
|
+
if (this.#websocket !== socket || this.#abandonedSockets.has(socket))
|
|
834
|
+
return;
|
|
733
835
|
logger().trace(
|
|
734
836
|
getLogMessage()
|
|
735
837
|
? {
|
|
@@ -815,23 +917,19 @@ export class ActorConnRaw {
|
|
|
815
917
|
this.#actorResolutionState,
|
|
816
918
|
this.#driver,
|
|
817
919
|
);
|
|
920
|
+
if (
|
|
921
|
+
this.#websocket !== socket ||
|
|
922
|
+
this.#abandonedSockets.has(socket)
|
|
923
|
+
) {
|
|
924
|
+
return;
|
|
925
|
+
}
|
|
818
926
|
if (schedulingError) {
|
|
819
927
|
errorToThrow = schedulingError;
|
|
820
928
|
}
|
|
821
929
|
}
|
|
822
930
|
|
|
823
|
-
// If we have an onOpenPromise, reject it with the error
|
|
824
|
-
if (this.#onOpenPromise) {
|
|
825
|
-
this.#onOpenPromise.reject(errorToThrow);
|
|
826
|
-
}
|
|
827
|
-
|
|
828
931
|
this.#invalidateActorIfStale(group, code);
|
|
829
|
-
|
|
830
|
-
// Reject any in-flight requests
|
|
831
|
-
for (const [id, inFlight] of this.#actionsInFlight.entries()) {
|
|
832
|
-
inFlight.reject(errorToThrow);
|
|
833
|
-
this.#actionsInFlight.delete(id);
|
|
834
|
-
}
|
|
932
|
+
this.#rejectPendingPromises(errorToThrow, false);
|
|
835
933
|
|
|
836
934
|
this.#dispatchActorError(errorToThrow);
|
|
837
935
|
}
|
|
@@ -894,7 +992,11 @@ export class ActorConnRaw {
|
|
|
894
992
|
const parsed = parseWebSocketCloseReason(reason);
|
|
895
993
|
|
|
896
994
|
if (parsed) {
|
|
897
|
-
const { group, code, rayId } = parsed;
|
|
995
|
+
const { group, code, rayId, retryAfterMs } = parsed;
|
|
996
|
+
const closeOptions =
|
|
997
|
+
retryAfterMs === undefined
|
|
998
|
+
? { rayId }
|
|
999
|
+
: { rayId, metadata: { retry_after_ms: retryAfterMs } };
|
|
898
1000
|
if (
|
|
899
1001
|
isInvalidToken(group, code) &&
|
|
900
1002
|
closeEvent.code === 1008 &&
|
|
@@ -923,7 +1025,7 @@ export class ActorConnRaw {
|
|
|
923
1025
|
group,
|
|
924
1026
|
code,
|
|
925
1027
|
`Connection closed: ${reason}`,
|
|
926
|
-
|
|
1028
|
+
closeOptions,
|
|
927
1029
|
),
|
|
928
1030
|
);
|
|
929
1031
|
return;
|
|
@@ -946,7 +1048,7 @@ export class ActorConnRaw {
|
|
|
946
1048
|
group,
|
|
947
1049
|
code,
|
|
948
1050
|
`Connection closed: ${reason}`,
|
|
949
|
-
|
|
1051
|
+
closeOptions,
|
|
950
1052
|
);
|
|
951
1053
|
}
|
|
952
1054
|
} else {
|
|
@@ -954,16 +1056,24 @@ export class ActorConnRaw {
|
|
|
954
1056
|
group,
|
|
955
1057
|
code,
|
|
956
1058
|
`Connection closed: ${reason}`,
|
|
957
|
-
|
|
1059
|
+
closeOptions,
|
|
958
1060
|
);
|
|
959
1061
|
}
|
|
960
1062
|
|
|
961
1063
|
this.#invalidateActorIfStale(group, code);
|
|
962
1064
|
} else {
|
|
963
1065
|
// Default error for non-structured close reasons
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
1066
|
+
const message = `${wasClean ? "Connection closed" : "Connection lost"} (code: ${closeEvent.code}, reason: ${reason})`;
|
|
1067
|
+
// A transport loss during the handshake can outlast an Engine
|
|
1068
|
+
// restart. Retry opening without replaying actions with unknown outcomes.
|
|
1069
|
+
error =
|
|
1070
|
+
!wasConnected && closeEvent.code === 1006
|
|
1071
|
+
? new errors.ActorError(
|
|
1072
|
+
"client",
|
|
1073
|
+
"connection_open_interrupted",
|
|
1074
|
+
message,
|
|
1075
|
+
)
|
|
1076
|
+
: new Error(message);
|
|
967
1077
|
}
|
|
968
1078
|
|
|
969
1079
|
this.#rejectPendingPromises(error, false);
|
|
@@ -985,6 +1095,8 @@ export class ActorConnRaw {
|
|
|
985
1095
|
}
|
|
986
1096
|
|
|
987
1097
|
#rejectPendingPromises(error: Error, suppressUnhandled: boolean) {
|
|
1098
|
+
// Rejected actions must not survive in the queue and execute after a reconnect.
|
|
1099
|
+
this.#clearQueuedMessages();
|
|
988
1100
|
if (this.#onOpenPromise) {
|
|
989
1101
|
if (suppressUnhandled) {
|
|
990
1102
|
this.#onOpenPromise.promise.catch(() => {});
|
|
@@ -61,6 +61,7 @@ import {
|
|
|
61
61
|
import { type ClientRaw, CREATE_ACTOR_CONN_PROXY } from "./client";
|
|
62
62
|
import { ActorError, isSchedulingError } from "./errors";
|
|
63
63
|
import { retryOnLifecycleBoundary } from "./lifecycle-errors";
|
|
64
|
+
import { RetryBudget, retryAfterMs, retryDelayMs } from "./retry-budget";
|
|
64
65
|
import { logger } from "./log";
|
|
65
66
|
import { outboundTelemetryHeaders } from "./outbound-telemetry";
|
|
66
67
|
import {
|
|
@@ -94,6 +95,7 @@ export class ActorHandleRaw {
|
|
|
94
95
|
#queueSendMutex = new AsyncMutex();
|
|
95
96
|
#currentActorInvocation?: CurrentActorInvocation;
|
|
96
97
|
#startCallSpan?: StartCallSpan;
|
|
98
|
+
#retryBudget: RetryBudget;
|
|
97
99
|
|
|
98
100
|
/**
|
|
99
101
|
* Do not call this directly.
|
|
@@ -113,6 +115,7 @@ export class ActorHandleRaw {
|
|
|
113
115
|
signal?: AbortSignal,
|
|
114
116
|
currentActorInvocation?: CurrentActorInvocation,
|
|
115
117
|
startCallSpan?: StartCallSpan,
|
|
118
|
+
retryBudget: RetryBudget = new RetryBudget(),
|
|
116
119
|
) {
|
|
117
120
|
this.#client = client;
|
|
118
121
|
this.#driver = driver;
|
|
@@ -124,6 +127,7 @@ export class ActorHandleRaw {
|
|
|
124
127
|
this.#signal = signal;
|
|
125
128
|
this.#currentActorInvocation = currentActorInvocation;
|
|
126
129
|
this.#startCallSpan = startCallSpan;
|
|
130
|
+
this.#retryBudget = retryBudget;
|
|
127
131
|
}
|
|
128
132
|
|
|
129
133
|
async #resolveConnectionParams(): Promise<unknown> {
|
|
@@ -166,6 +170,7 @@ export class ActorHandleRaw {
|
|
|
166
170
|
this.#actorResolutionState,
|
|
167
171
|
);
|
|
168
172
|
|
|
173
|
+
this.#retryBudget.deposit();
|
|
169
174
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
170
175
|
let actorId: string | undefined;
|
|
171
176
|
try {
|
|
@@ -205,9 +210,19 @@ export class ActorHandleRaw {
|
|
|
205
210
|
metadata,
|
|
206
211
|
attempt,
|
|
207
212
|
maxAttempts,
|
|
208
|
-
)
|
|
213
|
+
) &&
|
|
214
|
+
this.#retryBudget.tryWithdraw()
|
|
209
215
|
) {
|
|
210
|
-
await
|
|
216
|
+
await new Promise((resolve) =>
|
|
217
|
+
setTimeout(
|
|
218
|
+
resolve,
|
|
219
|
+
retryDelayMs(attempt, {
|
|
220
|
+
initialDelayMs: 25,
|
|
221
|
+
maxDelayMs: 1_000,
|
|
222
|
+
hintMs: retryAfterMs(err),
|
|
223
|
+
}),
|
|
224
|
+
),
|
|
225
|
+
);
|
|
211
226
|
continue;
|
|
212
227
|
}
|
|
213
228
|
|
|
@@ -309,7 +324,10 @@ export class ActorHandleRaw {
|
|
|
309
324
|
if (opts.name === "destroy") {
|
|
310
325
|
return await run();
|
|
311
326
|
}
|
|
312
|
-
return await retryOnLifecycleBoundary(run, {
|
|
327
|
+
return await retryOnLifecycleBoundary(run, {
|
|
328
|
+
signal,
|
|
329
|
+
budget: this.#retryBudget,
|
|
330
|
+
});
|
|
313
331
|
};
|
|
314
332
|
if (!call) {
|
|
315
333
|
return await send();
|
package/src/client/client.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
import { type ActorHandle, ActorHandleRaw } from "./actor-handle";
|
|
15
15
|
import { logger } from "./log";
|
|
16
16
|
import { resolveGatewayTarget } from "./resolve-gateway-target";
|
|
17
|
+
import { type RetryBudgetOptions, RetryBudget } from "./retry-budget";
|
|
17
18
|
|
|
18
19
|
export type { ClientConfig, ClientConfigInput } from "./config";
|
|
19
20
|
|
|
@@ -181,6 +182,8 @@ export const CREATE_ACTOR_CONN_PROXY = Symbol("createActorConnProxy");
|
|
|
181
182
|
export interface ClientRawOptions {
|
|
182
183
|
encoding?: Encoding;
|
|
183
184
|
gateway?: ActorGatewayOptions;
|
|
185
|
+
/** Bounds retries of refused requests across every handle of this client. */
|
|
186
|
+
retryBudget?: RetryBudgetOptions;
|
|
184
187
|
}
|
|
185
188
|
|
|
186
189
|
/** Runtime-supplied wiring for clients created inside an actor. Set only by the native runtime. */
|
|
@@ -211,6 +214,7 @@ export class ClientRaw {
|
|
|
211
214
|
#gatewayOptions: ActorGatewayOptions;
|
|
212
215
|
#currentActorInvocation?: CurrentActorInvocation;
|
|
213
216
|
#startCallSpan?: StartCallSpan;
|
|
217
|
+
#retryBudget: RetryBudget;
|
|
214
218
|
|
|
215
219
|
/**
|
|
216
220
|
* Creates an instance of Client.
|
|
@@ -228,6 +232,7 @@ export class ClientRaw {
|
|
|
228
232
|
this.#gatewayOptions = options.gateway ?? {};
|
|
229
233
|
this.#currentActorInvocation = options.currentActorInvocation;
|
|
230
234
|
this.#startCallSpan = options.startCallSpan;
|
|
235
|
+
this.#retryBudget = new RetryBudget(options.retryBudget);
|
|
231
236
|
}
|
|
232
237
|
|
|
233
238
|
/**
|
|
@@ -431,6 +436,7 @@ export class ClientRaw {
|
|
|
431
436
|
signal,
|
|
432
437
|
this.#currentActorInvocation,
|
|
433
438
|
this.#startCallSpan,
|
|
439
|
+
this.#retryBudget,
|
|
434
440
|
);
|
|
435
441
|
}
|
|
436
442
|
|
package/src/client/config.ts
CHANGED
|
@@ -88,6 +88,17 @@ export const ClientConfigSchemaBase = z.object({
|
|
|
88
88
|
// `proxyWebSocket`
|
|
89
89
|
getUpgradeWebSocket: z.custom<GetUpgradeWebSocket>().optional(),
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Bounds retries of refused requests: `percent` retries per 100 requests (default 10), with
|
|
93
|
+
* up to `maxTokens` banked for bursts (default 100). A refusal past the budget is thrown.
|
|
94
|
+
*/
|
|
95
|
+
retryBudget: z
|
|
96
|
+
.object({
|
|
97
|
+
percent: z.number().min(0).max(100).optional(),
|
|
98
|
+
maxTokens: z.number().int().min(0).optional(),
|
|
99
|
+
})
|
|
100
|
+
.optional(),
|
|
101
|
+
|
|
91
102
|
/** Replaces global fetch for actor HTTP gateway requests. */
|
|
92
103
|
fetch: z.custom<typeof globalThis.fetch>().optional(),
|
|
93
104
|
|
|
@@ -166,7 +177,7 @@ export function convertRegistryConfigToClientConfig(
|
|
|
166
177
|
token: config.token,
|
|
167
178
|
getToken: undefined,
|
|
168
179
|
namespace: config.namespace,
|
|
169
|
-
poolName: config.
|
|
180
|
+
poolName: config.node.poolName,
|
|
170
181
|
headers: config.headers,
|
|
171
182
|
gateway: { skipReadyWait: false },
|
|
172
183
|
encoding: "bare",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ActorError, HttpRequestError } from "./errors";
|
|
2
|
+
import { type RetryBudget, retryAfterMs, retryDelayMs } from "./retry-budget";
|
|
2
3
|
|
|
3
4
|
export interface LifecycleBoundaryInfo {
|
|
4
5
|
kind: "request_retry" | "reconnect_only";
|
|
@@ -64,7 +65,12 @@ function classifyActorError(
|
|
|
64
65
|
return undefined;
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
|
|
68
|
+
// `actor.migrating_from_v2` is a refusal while the actor moves in from a V2 cluster; it
|
|
69
|
+
// carries a `retry_after_ms` hint.
|
|
70
|
+
if (
|
|
71
|
+
error.group === "actor" &&
|
|
72
|
+
(error.code === "restarting" || error.code === "migrating_from_v2")
|
|
73
|
+
) {
|
|
68
74
|
return buildLifecycleBoundaryInfo(
|
|
69
75
|
"request_retry",
|
|
70
76
|
"actor_error",
|
|
@@ -76,6 +82,19 @@ function classifyActorError(
|
|
|
76
82
|
);
|
|
77
83
|
}
|
|
78
84
|
|
|
85
|
+
if (
|
|
86
|
+
(error.group === "ups" && error.code === "consumer_closed") ||
|
|
87
|
+
(error.group === "depot" && error.code === "request_deadline_exceeded")
|
|
88
|
+
) {
|
|
89
|
+
// The connection can recover, but an interrupted action may already have run.
|
|
90
|
+
return buildLifecycleBoundaryInfo(
|
|
91
|
+
"reconnect_only",
|
|
92
|
+
"actor_error",
|
|
93
|
+
error.message,
|
|
94
|
+
{ group: error.group, code: error.code },
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
79
98
|
if (
|
|
80
99
|
error.group === "guard" &&
|
|
81
100
|
isRetryableGuardGatewayHttpError(error.code)
|
|
@@ -271,6 +290,11 @@ async function waitWithSignal(ms: number, signal?: AbortSignal) {
|
|
|
271
290
|
});
|
|
272
291
|
}
|
|
273
292
|
|
|
293
|
+
/**
|
|
294
|
+
* Runs `run`, retrying retryable lifecycle and refusal errors with jittered exponential backoff
|
|
295
|
+
* that honors the service's `retry_after_ms` hint. With a `budget`, the call credits it once and
|
|
296
|
+
* every retry spends a token; a spent budget returns the error instead of retrying.
|
|
297
|
+
*/
|
|
274
298
|
export async function retryOnLifecycleBoundary<T>(
|
|
275
299
|
run: () => Promise<T>,
|
|
276
300
|
opts?: {
|
|
@@ -278,11 +302,13 @@ export async function retryOnLifecycleBoundary<T>(
|
|
|
278
302
|
initialDelayMs?: number;
|
|
279
303
|
maxDelayMs?: number;
|
|
280
304
|
signal?: AbortSignal;
|
|
305
|
+
budget?: RetryBudget;
|
|
281
306
|
},
|
|
282
307
|
): Promise<T> {
|
|
283
308
|
const maxAttempts = opts?.maxAttempts ?? 5;
|
|
284
309
|
const initialDelayMs = opts?.initialDelayMs ?? 25;
|
|
285
310
|
const maxDelayMs = opts?.maxDelayMs ?? 200;
|
|
311
|
+
opts?.budget?.deposit();
|
|
286
312
|
|
|
287
313
|
let lastError: unknown;
|
|
288
314
|
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
@@ -299,8 +325,15 @@ export async function retryOnLifecycleBoundary<T>(
|
|
|
299
325
|
if (attempt === maxAttempts - 1) {
|
|
300
326
|
break;
|
|
301
327
|
}
|
|
328
|
+
if (opts?.budget && !opts.budget.tryWithdraw()) {
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
302
331
|
|
|
303
|
-
const delayMs =
|
|
332
|
+
const delayMs = retryDelayMs(attempt, {
|
|
333
|
+
initialDelayMs,
|
|
334
|
+
maxDelayMs,
|
|
335
|
+
hintMs: retryAfterMs(error),
|
|
336
|
+
});
|
|
304
337
|
await waitWithSignal(delayMs, opts?.signal);
|
|
305
338
|
}
|
|
306
339
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { ActorError } from "./errors";
|
|
2
|
+
|
|
3
|
+
/** How many retries a client may spend, as a share of the requests it sends. */
|
|
4
|
+
export interface RetryBudgetOptions {
|
|
5
|
+
/** Retries allowed per 100 requests (default 10). */
|
|
6
|
+
percent?: number;
|
|
7
|
+
/** Retries the client can bank for a burst of refusals; the budget starts full (default 100). */
|
|
8
|
+
maxTokens?: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const DEFAULT_PERCENT = 10;
|
|
12
|
+
const DEFAULT_MAX_TOKENS = 100;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Bounds the client's retries of refused requests. Each request adds a fraction of a token and
|
|
16
|
+
* each retry spends one, so retries stay a fixed share of traffic while the service sheds load
|
|
17
|
+
* instead of multiplying it.
|
|
18
|
+
*/
|
|
19
|
+
export class RetryBudget {
|
|
20
|
+
#tokens: number;
|
|
21
|
+
readonly #maxTokens: number;
|
|
22
|
+
readonly #deposit: number;
|
|
23
|
+
|
|
24
|
+
constructor(options: RetryBudgetOptions = {}) {
|
|
25
|
+
const percent = options.percent ?? DEFAULT_PERCENT;
|
|
26
|
+
const maxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
27
|
+
if (!(percent >= 0 && percent <= 100)) {
|
|
28
|
+
throw new Error("retryBudget.percent must be in 0..=100");
|
|
29
|
+
}
|
|
30
|
+
if (!(maxTokens >= 0)) {
|
|
31
|
+
throw new Error("retryBudget.maxTokens must be at least 0");
|
|
32
|
+
}
|
|
33
|
+
this.#maxTokens = maxTokens;
|
|
34
|
+
this.#tokens = maxTokens;
|
|
35
|
+
this.#deposit = percent / 100;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Credits one request sent. */
|
|
39
|
+
deposit(): void {
|
|
40
|
+
this.#tokens = Math.min(this.#maxTokens, this.#tokens + this.#deposit);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Spends one token. Returns `false` when the budget is spent. */
|
|
44
|
+
tryWithdraw(): boolean {
|
|
45
|
+
// Rounding guards against float drift: ten 0.1 deposits must make one token.
|
|
46
|
+
if (this.#tokens + 1e-9 < 1) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
this.#tokens = Math.max(0, this.#tokens - 1);
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The service's retry hint for a refused request, in milliseconds, when it sent one. */
|
|
55
|
+
export function retryAfterMs(error: unknown): number | undefined {
|
|
56
|
+
if (!(error instanceof ActorError)) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
const metadata = error.metadata;
|
|
60
|
+
if (metadata === null || typeof metadata !== "object") {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const hint = (metadata as { retry_after_ms?: unknown }).retry_after_ms;
|
|
64
|
+
return typeof hint === "number" && hint >= 0 ? hint : undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The wait before retry `attempt` (0-based): jittered exponential backoff over
|
|
69
|
+
* `[initialDelayMs, maxDelayMs]`, never shorter than the service's hint.
|
|
70
|
+
*/
|
|
71
|
+
export function retryDelayMs(
|
|
72
|
+
attempt: number,
|
|
73
|
+
opts: { initialDelayMs: number; maxDelayMs: number; hintMs?: number },
|
|
74
|
+
): number {
|
|
75
|
+
const backoff = Math.min(opts.initialDelayMs * 2 ** attempt, opts.maxDelayMs);
|
|
76
|
+
const jittered = backoff / 2 + Math.random() * (backoff / 2);
|
|
77
|
+
return Math.max(jittered, opts.hintMs ?? 0);
|
|
78
|
+
}
|
package/src/client/utils.ts
CHANGED
|
@@ -19,21 +19,27 @@ export interface ParsedCloseReason {
|
|
|
19
19
|
group: string;
|
|
20
20
|
code: string;
|
|
21
21
|
rayId?: string;
|
|
22
|
+
/** The service's retry hint, from a `#retry_after_ms=<ms>` suffix. */
|
|
23
|
+
retryAfterMs?: number;
|
|
22
24
|
}
|
|
23
25
|
|
|
26
|
+
const RETRY_AFTER_SUFFIX = /^retry_after_ms=(\d+)$/;
|
|
27
|
+
|
|
24
28
|
/**
|
|
25
29
|
* Parses WebSocket close reason string into structured data.
|
|
26
30
|
*
|
|
27
31
|
* Expected format examples:
|
|
28
32
|
* - "guard.actor_runner_failed#t1s80so6h3irenp8ymzltfoittcl00"
|
|
29
33
|
* - "ws.client_closed"
|
|
34
|
+
* - "guard.service_unavailable#retry_after_ms=1000", a refusal with its retry hint. The hint
|
|
35
|
+
* rides after `#` so older clients still read the retryable `<group>.<code>`.
|
|
30
36
|
*
|
|
31
37
|
* Returns undefined if the format is invalid
|
|
32
38
|
*/
|
|
33
39
|
export function parseWebSocketCloseReason(
|
|
34
40
|
reason: string,
|
|
35
41
|
): ParsedCloseReason | undefined {
|
|
36
|
-
const [mainPart,
|
|
42
|
+
const [mainPart, suffix] = reason.split("#");
|
|
37
43
|
const [group, code] = mainPart.split(".");
|
|
38
44
|
|
|
39
45
|
if (!group || !code) {
|
|
@@ -41,10 +47,15 @@ export function parseWebSocketCloseReason(
|
|
|
41
47
|
return undefined;
|
|
42
48
|
}
|
|
43
49
|
|
|
50
|
+
const retryAfter = suffix?.match(RETRY_AFTER_SUFFIX);
|
|
51
|
+
if (retryAfter) {
|
|
52
|
+
return { group, code, retryAfterMs: Number(retryAfter[1]) };
|
|
53
|
+
}
|
|
54
|
+
|
|
44
55
|
return {
|
|
45
56
|
group,
|
|
46
57
|
code,
|
|
47
|
-
rayId,
|
|
58
|
+
rayId: suffix,
|
|
48
59
|
};
|
|
49
60
|
}
|
|
50
61
|
|