rivetkit 0.0.0-sqlite-sync-3x-release.a00b07f → 0.0.0-sqlite-query-metrics-v2.f5d3605
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 +59 -99
- package/dist/browser/client.js +20 -193
- package/dist/browser/client.js.map +1 -1
- package/dist/browser/inspector/client.js +2 -1
- package/dist/browser/inspector/client.js.map +1 -1
- package/dist/tsup/{chunk-D5WUPE47.js → chunk-3URZ4UV5.js} +2 -2
- package/dist/tsup/{chunk-TSQTI6H6.js → chunk-D4G35VTO.js} +4 -4
- package/dist/tsup/{chunk-OTVJYZ2L.cjs → chunk-DDS7OOVF.cjs} +166 -326
- package/dist/tsup/chunk-DDS7OOVF.cjs.map +1 -0
- package/dist/tsup/{chunk-6KXVLIXL.cjs → chunk-DHL2M2E2.cjs} +3 -3
- package/dist/tsup/{chunk-6KXVLIXL.cjs.map → chunk-DHL2M2E2.cjs.map} +1 -1
- package/dist/tsup/{chunk-ZYK4B5WV.js → chunk-E2CUROR3.js} +2 -2
- package/dist/tsup/{chunk-JUWMP5O3.js → chunk-EPRA2TCT.js} +2 -2
- package/dist/tsup/{chunk-3DONDAMG.js → chunk-F7IGTKGM.js} +2 -2
- package/dist/tsup/{chunk-BTSDFFDC.cjs → chunk-FYDB45YU.cjs} +10 -10
- package/dist/tsup/{chunk-BTSDFFDC.cjs.map → chunk-FYDB45YU.cjs.map} +1 -1
- package/dist/tsup/{chunk-73RNTXHB.cjs → chunk-HO76AEDH.cjs} +3 -3
- package/dist/tsup/{chunk-73RNTXHB.cjs.map → chunk-HO76AEDH.cjs.map} +1 -1
- package/dist/tsup/{chunk-A2CF6PFH.js → chunk-JI6GZ2C2.js} +1 -18
- package/dist/tsup/chunk-JI6GZ2C2.js.map +1 -0
- package/dist/tsup/{chunk-Z7E2G3JZ.cjs → chunk-JW2WPE6Z.cjs} +4 -4
- package/dist/tsup/{chunk-Z7E2G3JZ.cjs.map → chunk-JW2WPE6Z.cjs.map} +1 -1
- package/dist/tsup/{chunk-2MOLY6AM.cjs → chunk-KDT56V3Y.cjs} +11 -10
- package/dist/tsup/chunk-KDT56V3Y.cjs.map +1 -0
- package/dist/tsup/{chunk-DJUBJQEQ.js → chunk-KSP3NXGY.js} +3 -3
- package/dist/tsup/{chunk-JUHXV3HF.cjs → chunk-L3Z3W44Q.cjs} +3 -3
- package/dist/tsup/{chunk-JUHXV3HF.cjs.map → chunk-L3Z3W44Q.cjs.map} +1 -1
- package/dist/tsup/{chunk-RTWH3AX3.cjs → chunk-MYWZCDML.cjs} +2 -19
- package/dist/tsup/chunk-MYWZCDML.cjs.map +1 -0
- package/dist/tsup/{chunk-TRUHDXOU.js → chunk-PJ5EXY6F.js} +25 -185
- package/dist/tsup/chunk-PJ5EXY6F.js.map +1 -0
- package/dist/tsup/{chunk-TM76Z7XT.cjs → chunk-QR6EAWEC.cjs} +7 -7
- package/dist/tsup/{chunk-TM76Z7XT.cjs.map → chunk-QR6EAWEC.cjs.map} +1 -1
- package/dist/tsup/{chunk-BRH4PQS5.js → chunk-USKGKQZM.js} +11 -10
- package/dist/tsup/chunk-USKGKQZM.js.map +1 -0
- package/dist/tsup/client/mod.cjs +6 -6
- package/dist/tsup/client/mod.d.cts +3 -15
- package/dist/tsup/client/mod.d.ts +3 -15
- 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-DK144OAw.d.cts → config-CkmVg2fG.d.cts} +12 -58
- package/dist/tsup/{config-B4erdavw.d.ts → config-Cw_BwH_G.d.ts} +12 -58
- package/dist/tsup/{config-BfG1qpYz.d.cts → config-D4clT6mq.d.cts} +52 -34
- package/dist/tsup/{config-BfG1qpYz.d.ts → config-D4clT6mq.d.ts} +52 -34
- package/dist/tsup/{context-Oxi6YWcG.d.ts → context-BYOKTQJi.d.ts} +2 -2
- package/dist/tsup/{context-Cvnf3BRe.d.cts → context-_xIcgBnE.d.cts} +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 +201 -548
- 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 +116 -463
- 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 +17 -17
- 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 +5 -5
- package/package.json +10 -9
- package/src/actor/config.test.ts +78 -0
- package/src/actor/keys.test.ts +275 -0
- package/src/actor/keys.ts +0 -86
- package/src/actor/utils.test.ts +48 -0
- package/src/client/actor-conn.ts +6 -92
- package/src/client/actor-handle.ts +3 -21
- package/src/client/client.ts +0 -6
- package/src/client/config.ts +1 -12
- package/src/client/lifecycle-errors.ts +1 -16
- package/src/client/test.ts +44 -0
- package/src/client/token-provider.test.ts +86 -0
- package/src/client/utils.ts +2 -13
- package/src/common/database/config.ts +52 -46
- package/src/common/database/mod.test.ts +364 -0
- package/src/common/database/native-database.test.ts +220 -0
- package/src/common/database/native-database.ts +27 -247
- package/src/common/inline-websocket-adapter.test.ts +51 -0
- package/src/common/router.ts +1 -1
- package/src/db/drizzle.test.ts +164 -0
- package/src/db/mod.ts +1 -0
- package/src/engine-api/actors.ts +3 -3
- package/src/engine-client/actor-websocket-client.ts +2 -4
- package/src/engine-client/api-endpoints.ts +14 -44
- package/src/engine-client/mod.ts +2 -2
- package/src/registry/config/envoy.ts +1 -1
- package/src/registry/config/index.test.ts +25 -0
- package/src/registry/config/index.ts +31 -28
- package/src/registry/index.ts +19 -51
- package/src/registry/napi-runtime.ts +2 -81
- package/src/registry/native.ts +51 -183
- package/src/registry/run-handler-coordinator.test.ts +252 -0
- package/src/registry/runtime.test.ts +124 -0
- package/src/registry/runtime.ts +10 -79
- package/src/registry/wasm-runtime.ts +3 -94
- package/src/unstable/migrations.test.ts +170 -0
- package/src/utils/endpoint-parser.test.ts +206 -0
- package/src/utils/env-vars.ts +10 -15
- package/src/utils.test.ts +34 -0
- package/src/workflow/driver.test.ts +62 -0
- package/dist/tsup/chunk-2MOLY6AM.cjs.map +0 -1
- package/dist/tsup/chunk-A2CF6PFH.js.map +0 -1
- package/dist/tsup/chunk-BRH4PQS5.js.map +0 -1
- package/dist/tsup/chunk-OTVJYZ2L.cjs.map +0 -1
- package/dist/tsup/chunk-RTWH3AX3.cjs.map +0 -1
- package/dist/tsup/chunk-TRUHDXOU.js.map +0 -1
- package/src/client/retry-budget.ts +0 -78
- /package/dist/tsup/{chunk-D5WUPE47.js.map → chunk-3URZ4UV5.js.map} +0 -0
- /package/dist/tsup/{chunk-TSQTI6H6.js.map → chunk-D4G35VTO.js.map} +0 -0
- /package/dist/tsup/{chunk-ZYK4B5WV.js.map → chunk-E2CUROR3.js.map} +0 -0
- /package/dist/tsup/{chunk-JUWMP5O3.js.map → chunk-EPRA2TCT.js.map} +0 -0
- /package/dist/tsup/{chunk-3DONDAMG.js.map → chunk-F7IGTKGM.js.map} +0 -0
- /package/dist/tsup/{chunk-DJUBJQEQ.js.map → chunk-KSP3NXGY.js.map} +0 -0
package/src/client/actor-conn.ts
CHANGED
|
@@ -50,7 +50,6 @@ import {
|
|
|
50
50
|
import { ACTOR_CONNS_SYMBOL, type ClientRaw } from "./client";
|
|
51
51
|
import * as errors from "./errors";
|
|
52
52
|
import { isRetryableLifecycleReconnectSignal } from "./lifecycle-errors";
|
|
53
|
-
import { retryAfterMs } from "./retry-budget";
|
|
54
53
|
import { logger } from "./log";
|
|
55
54
|
import { isInvalidToken } from "./token-provider";
|
|
56
55
|
import { outboundTelemetryHeaders } from "./outbound-telemetry";
|
|
@@ -105,21 +104,6 @@ interface EventSubscriptions<Args extends Array<unknown>> {
|
|
|
105
104
|
*/
|
|
106
105
|
export type EventUnsubscribe = () => void;
|
|
107
106
|
|
|
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
|
-
|
|
123
107
|
/**
|
|
124
108
|
* A function that handles connection errors.
|
|
125
109
|
*
|
|
@@ -198,8 +182,6 @@ export class ActorConnRaw {
|
|
|
198
182
|
|
|
199
183
|
/** Promise used to indicate the socket has connected successfully. This will be rejected if the connection fails. */
|
|
200
184
|
#onOpenPromise?: ReturnType<typeof promiseWithResolvers<undefined>>;
|
|
201
|
-
/** Sockets given up on by the connect timeout. Their late events are ignored. */
|
|
202
|
-
#abandonedSockets = new WeakSet<UniversalWebSocket>();
|
|
203
185
|
|
|
204
186
|
#websocket?: UniversalWebSocket;
|
|
205
187
|
#authRetryAvailable = true;
|
|
@@ -475,17 +457,12 @@ export class ActorConnRaw {
|
|
|
475
457
|
minTimeout: 250,
|
|
476
458
|
maxTimeout: 30_000,
|
|
477
459
|
|
|
478
|
-
onFailedAttempt:
|
|
460
|
+
onFailedAttempt: (error) => {
|
|
479
461
|
logger().warn({
|
|
480
462
|
msg: "failed to reconnect",
|
|
481
463
|
attempt: error.attemptNumber,
|
|
482
464
|
error: stringifyError(error),
|
|
483
465
|
});
|
|
484
|
-
// A close that carried the service's retry hint waits at least that long.
|
|
485
|
-
const hintMs = retryAfterMs(error);
|
|
486
|
-
if (hintMs !== undefined) {
|
|
487
|
-
await new Promise((resolve) => setTimeout(resolve, hintMs));
|
|
488
|
-
}
|
|
489
466
|
},
|
|
490
467
|
|
|
491
468
|
// Cancel retry if aborted
|
|
@@ -521,7 +498,7 @@ export class ActorConnRaw {
|
|
|
521
498
|
await this.#connectWebSocket();
|
|
522
499
|
|
|
523
500
|
// Wait for result
|
|
524
|
-
await this.#
|
|
501
|
+
await this.#onOpenPromise.promise;
|
|
525
502
|
} catch (error) {
|
|
526
503
|
if (this.#shouldRetryConnectionOpenError(error)) {
|
|
527
504
|
throw error;
|
|
@@ -552,65 +529,10 @@ export class ActorConnRaw {
|
|
|
552
529
|
}
|
|
553
530
|
}
|
|
554
531
|
|
|
555
|
-
/**
|
|
556
|
-
* Waits for the actor's Init on the current socket, up to {@link CONNECT_TIMEOUT_MS}. A socket
|
|
557
|
-
* can stay connecting with no open, error or close event, as when the engine holds the
|
|
558
|
-
* upgrade or a refused connect reports only an error. On timeout the socket is abandoned
|
|
559
|
-
* and closed, and the retryable `client.connection_open_timeout` starts the next attempt.
|
|
560
|
-
*/
|
|
561
|
-
async #waitForOpen(open: Promise<undefined>) {
|
|
562
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
563
|
-
const timedOut = new Promise<never>((_, reject) => {
|
|
564
|
-
timer = setTimeout(() => {
|
|
565
|
-
reject(
|
|
566
|
-
new errors.ActorError(
|
|
567
|
-
"client",
|
|
568
|
-
"connection_open_timeout",
|
|
569
|
-
`Connection did not open within ${CONNECT_TIMEOUT_MS} ms`,
|
|
570
|
-
),
|
|
571
|
-
);
|
|
572
|
-
}, CONNECT_TIMEOUT_MS);
|
|
573
|
-
});
|
|
574
|
-
try {
|
|
575
|
-
await Promise.race([open, timedOut]);
|
|
576
|
-
} catch (error) {
|
|
577
|
-
if (isConnectionOpenTimeout(error)) {
|
|
578
|
-
this.#abandonCurrentSocket();
|
|
579
|
-
}
|
|
580
|
-
throw error;
|
|
581
|
-
} finally {
|
|
582
|
-
clearTimeout(timer);
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
#abandonCurrentSocket() {
|
|
587
|
-
const ws = this.#websocket;
|
|
588
|
-
if (!ws) return;
|
|
589
|
-
this.#websocket = undefined;
|
|
590
|
-
this.#abandonedSockets.add(ws);
|
|
591
|
-
logger().warn({
|
|
592
|
-
msg: "connection did not open in time, retrying",
|
|
593
|
-
connId: this.#connId,
|
|
594
|
-
readyState: ws.readyState,
|
|
595
|
-
timeoutMs: CONNECT_TIMEOUT_MS,
|
|
596
|
-
});
|
|
597
|
-
try {
|
|
598
|
-
ws.close(1000, "Connect timeout");
|
|
599
|
-
} catch (err) {
|
|
600
|
-
logger().debug({
|
|
601
|
-
msg: "failed to close abandoned websocket",
|
|
602
|
-
error: stringifyError(err),
|
|
603
|
-
});
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
|
|
607
532
|
#shouldRetryConnectionOpenError(error: unknown): boolean {
|
|
608
533
|
if (error instanceof errors.ActorConnDisposed) {
|
|
609
534
|
return false;
|
|
610
535
|
}
|
|
611
|
-
if (isConnectionOpenTimeout(error)) {
|
|
612
|
-
return true;
|
|
613
|
-
}
|
|
614
536
|
if (
|
|
615
537
|
error instanceof errors.ActorError &&
|
|
616
538
|
isInvalidToken(error.group, error.code) &&
|
|
@@ -692,14 +614,12 @@ export class ActorConnRaw {
|
|
|
692
614
|
});
|
|
693
615
|
this.#websocket = ws;
|
|
694
616
|
ws.addEventListener("open", () => {
|
|
695
|
-
if (this.#abandonedSockets.has(ws)) return;
|
|
696
617
|
logger().debug({
|
|
697
618
|
msg: "client websocket open",
|
|
698
619
|
connId: this.#connId,
|
|
699
620
|
});
|
|
700
621
|
});
|
|
701
622
|
ws.addEventListener("message", async (ev: { data: ConnMessage }) => {
|
|
702
|
-
if (this.#abandonedSockets.has(ws)) return;
|
|
703
623
|
try {
|
|
704
624
|
await this.#handleOnMessage(ev.data);
|
|
705
625
|
} catch (err) {
|
|
@@ -710,7 +630,6 @@ export class ActorConnRaw {
|
|
|
710
630
|
}
|
|
711
631
|
});
|
|
712
632
|
ws.addEventListener("close", async (ev: Event | CloseEventLike) => {
|
|
713
|
-
if (this.#abandonedSockets.has(ws)) return;
|
|
714
633
|
try {
|
|
715
634
|
await this.#handleOnClose(ev);
|
|
716
635
|
} catch (err) {
|
|
@@ -721,7 +640,6 @@ export class ActorConnRaw {
|
|
|
721
640
|
}
|
|
722
641
|
});
|
|
723
642
|
ws.addEventListener("error", () => {
|
|
724
|
-
if (this.#abandonedSockets.has(ws)) return;
|
|
725
643
|
try {
|
|
726
644
|
this.#handleOnError();
|
|
727
645
|
} catch (err) {
|
|
@@ -976,11 +894,7 @@ export class ActorConnRaw {
|
|
|
976
894
|
const parsed = parseWebSocketCloseReason(reason);
|
|
977
895
|
|
|
978
896
|
if (parsed) {
|
|
979
|
-
const { group, code, rayId
|
|
980
|
-
const closeOptions =
|
|
981
|
-
retryAfterMs === undefined
|
|
982
|
-
? { rayId }
|
|
983
|
-
: { rayId, metadata: { retry_after_ms: retryAfterMs } };
|
|
897
|
+
const { group, code, rayId } = parsed;
|
|
984
898
|
if (
|
|
985
899
|
isInvalidToken(group, code) &&
|
|
986
900
|
closeEvent.code === 1008 &&
|
|
@@ -1009,7 +923,7 @@ export class ActorConnRaw {
|
|
|
1009
923
|
group,
|
|
1010
924
|
code,
|
|
1011
925
|
`Connection closed: ${reason}`,
|
|
1012
|
-
|
|
926
|
+
{ rayId },
|
|
1013
927
|
),
|
|
1014
928
|
);
|
|
1015
929
|
return;
|
|
@@ -1032,7 +946,7 @@ export class ActorConnRaw {
|
|
|
1032
946
|
group,
|
|
1033
947
|
code,
|
|
1034
948
|
`Connection closed: ${reason}`,
|
|
1035
|
-
|
|
949
|
+
{ rayId },
|
|
1036
950
|
);
|
|
1037
951
|
}
|
|
1038
952
|
} else {
|
|
@@ -1040,7 +954,7 @@ export class ActorConnRaw {
|
|
|
1040
954
|
group,
|
|
1041
955
|
code,
|
|
1042
956
|
`Connection closed: ${reason}`,
|
|
1043
|
-
|
|
957
|
+
{ rayId },
|
|
1044
958
|
);
|
|
1045
959
|
}
|
|
1046
960
|
|
|
@@ -61,7 +61,6 @@ 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";
|
|
65
64
|
import { logger } from "./log";
|
|
66
65
|
import { outboundTelemetryHeaders } from "./outbound-telemetry";
|
|
67
66
|
import {
|
|
@@ -95,7 +94,6 @@ export class ActorHandleRaw {
|
|
|
95
94
|
#queueSendMutex = new AsyncMutex();
|
|
96
95
|
#currentActorInvocation?: CurrentActorInvocation;
|
|
97
96
|
#startCallSpan?: StartCallSpan;
|
|
98
|
-
#retryBudget: RetryBudget;
|
|
99
97
|
|
|
100
98
|
/**
|
|
101
99
|
* Do not call this directly.
|
|
@@ -115,7 +113,6 @@ export class ActorHandleRaw {
|
|
|
115
113
|
signal?: AbortSignal,
|
|
116
114
|
currentActorInvocation?: CurrentActorInvocation,
|
|
117
115
|
startCallSpan?: StartCallSpan,
|
|
118
|
-
retryBudget: RetryBudget = new RetryBudget(),
|
|
119
116
|
) {
|
|
120
117
|
this.#client = client;
|
|
121
118
|
this.#driver = driver;
|
|
@@ -127,7 +124,6 @@ export class ActorHandleRaw {
|
|
|
127
124
|
this.#signal = signal;
|
|
128
125
|
this.#currentActorInvocation = currentActorInvocation;
|
|
129
126
|
this.#startCallSpan = startCallSpan;
|
|
130
|
-
this.#retryBudget = retryBudget;
|
|
131
127
|
}
|
|
132
128
|
|
|
133
129
|
async #resolveConnectionParams(): Promise<unknown> {
|
|
@@ -170,7 +166,6 @@ export class ActorHandleRaw {
|
|
|
170
166
|
this.#actorResolutionState,
|
|
171
167
|
);
|
|
172
168
|
|
|
173
|
-
this.#retryBudget.deposit();
|
|
174
169
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
175
170
|
let actorId: string | undefined;
|
|
176
171
|
try {
|
|
@@ -210,19 +205,9 @@ export class ActorHandleRaw {
|
|
|
210
205
|
metadata,
|
|
211
206
|
attempt,
|
|
212
207
|
maxAttempts,
|
|
213
|
-
)
|
|
214
|
-
this.#retryBudget.tryWithdraw()
|
|
208
|
+
)
|
|
215
209
|
) {
|
|
216
|
-
await
|
|
217
|
-
setTimeout(
|
|
218
|
-
resolve,
|
|
219
|
-
retryDelayMs(attempt, {
|
|
220
|
-
initialDelayMs: 25,
|
|
221
|
-
maxDelayMs: 1_000,
|
|
222
|
-
hintMs: retryAfterMs(err),
|
|
223
|
-
}),
|
|
224
|
-
),
|
|
225
|
-
);
|
|
210
|
+
await this.#waitForRetryWindow();
|
|
226
211
|
continue;
|
|
227
212
|
}
|
|
228
213
|
|
|
@@ -324,10 +309,7 @@ export class ActorHandleRaw {
|
|
|
324
309
|
if (opts.name === "destroy") {
|
|
325
310
|
return await run();
|
|
326
311
|
}
|
|
327
|
-
return await retryOnLifecycleBoundary(run, {
|
|
328
|
-
signal,
|
|
329
|
-
budget: this.#retryBudget,
|
|
330
|
-
});
|
|
312
|
+
return await retryOnLifecycleBoundary(run, { signal });
|
|
331
313
|
};
|
|
332
314
|
if (!call) {
|
|
333
315
|
return await send();
|
package/src/client/client.ts
CHANGED
|
@@ -14,7 +14,6 @@ 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";
|
|
18
17
|
|
|
19
18
|
export type { ClientConfig, ClientConfigInput } from "./config";
|
|
20
19
|
|
|
@@ -182,8 +181,6 @@ export const CREATE_ACTOR_CONN_PROXY = Symbol("createActorConnProxy");
|
|
|
182
181
|
export interface ClientRawOptions {
|
|
183
182
|
encoding?: Encoding;
|
|
184
183
|
gateway?: ActorGatewayOptions;
|
|
185
|
-
/** Bounds retries of refused requests across every handle of this client. */
|
|
186
|
-
retryBudget?: RetryBudgetOptions;
|
|
187
184
|
}
|
|
188
185
|
|
|
189
186
|
/** Runtime-supplied wiring for clients created inside an actor. Set only by the native runtime. */
|
|
@@ -214,7 +211,6 @@ export class ClientRaw {
|
|
|
214
211
|
#gatewayOptions: ActorGatewayOptions;
|
|
215
212
|
#currentActorInvocation?: CurrentActorInvocation;
|
|
216
213
|
#startCallSpan?: StartCallSpan;
|
|
217
|
-
#retryBudget: RetryBudget;
|
|
218
214
|
|
|
219
215
|
/**
|
|
220
216
|
* Creates an instance of Client.
|
|
@@ -232,7 +228,6 @@ export class ClientRaw {
|
|
|
232
228
|
this.#gatewayOptions = options.gateway ?? {};
|
|
233
229
|
this.#currentActorInvocation = options.currentActorInvocation;
|
|
234
230
|
this.#startCallSpan = options.startCallSpan;
|
|
235
|
-
this.#retryBudget = new RetryBudget(options.retryBudget);
|
|
236
231
|
}
|
|
237
232
|
|
|
238
233
|
/**
|
|
@@ -436,7 +431,6 @@ export class ClientRaw {
|
|
|
436
431
|
signal,
|
|
437
432
|
this.#currentActorInvocation,
|
|
438
433
|
this.#startCallSpan,
|
|
439
|
-
this.#retryBudget,
|
|
440
434
|
);
|
|
441
435
|
}
|
|
442
436
|
|
package/src/client/config.ts
CHANGED
|
@@ -88,17 +88,6 @@ 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
|
-
|
|
102
91
|
/** Replaces global fetch for actor HTTP gateway requests. */
|
|
103
92
|
fetch: z.custom<typeof globalThis.fetch>().optional(),
|
|
104
93
|
|
|
@@ -177,7 +166,7 @@ export function convertRegistryConfigToClientConfig(
|
|
|
177
166
|
token: config.token,
|
|
178
167
|
getToken: undefined,
|
|
179
168
|
namespace: config.namespace,
|
|
180
|
-
poolName: config.
|
|
169
|
+
poolName: config.envoy.poolName,
|
|
181
170
|
headers: config.headers,
|
|
182
171
|
gateway: { skipReadyWait: false },
|
|
183
172
|
encoding: "bare",
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ActorError, HttpRequestError } from "./errors";
|
|
2
|
-
import { type RetryBudget, retryAfterMs, retryDelayMs } from "./retry-budget";
|
|
3
2
|
|
|
4
3
|
export interface LifecycleBoundaryInfo {
|
|
5
4
|
kind: "request_retry" | "reconnect_only";
|
|
@@ -272,11 +271,6 @@ async function waitWithSignal(ms: number, signal?: AbortSignal) {
|
|
|
272
271
|
});
|
|
273
272
|
}
|
|
274
273
|
|
|
275
|
-
/**
|
|
276
|
-
* Runs `run`, retrying retryable lifecycle and refusal errors with jittered exponential backoff
|
|
277
|
-
* that honors the service's `retry_after_ms` hint. With a `budget`, the call credits it once and
|
|
278
|
-
* every retry spends a token; a spent budget returns the error instead of retrying.
|
|
279
|
-
*/
|
|
280
274
|
export async function retryOnLifecycleBoundary<T>(
|
|
281
275
|
run: () => Promise<T>,
|
|
282
276
|
opts?: {
|
|
@@ -284,13 +278,11 @@ export async function retryOnLifecycleBoundary<T>(
|
|
|
284
278
|
initialDelayMs?: number;
|
|
285
279
|
maxDelayMs?: number;
|
|
286
280
|
signal?: AbortSignal;
|
|
287
|
-
budget?: RetryBudget;
|
|
288
281
|
},
|
|
289
282
|
): Promise<T> {
|
|
290
283
|
const maxAttempts = opts?.maxAttempts ?? 5;
|
|
291
284
|
const initialDelayMs = opts?.initialDelayMs ?? 25;
|
|
292
285
|
const maxDelayMs = opts?.maxDelayMs ?? 200;
|
|
293
|
-
opts?.budget?.deposit();
|
|
294
286
|
|
|
295
287
|
let lastError: unknown;
|
|
296
288
|
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
@@ -307,15 +299,8 @@ export async function retryOnLifecycleBoundary<T>(
|
|
|
307
299
|
if (attempt === maxAttempts - 1) {
|
|
308
300
|
break;
|
|
309
301
|
}
|
|
310
|
-
if (opts?.budget && !opts.budget.tryWithdraw()) {
|
|
311
|
-
break;
|
|
312
|
-
}
|
|
313
302
|
|
|
314
|
-
const delayMs =
|
|
315
|
-
initialDelayMs,
|
|
316
|
-
maxDelayMs,
|
|
317
|
-
hintMs: retryAfterMs(error),
|
|
318
|
-
});
|
|
303
|
+
const delayMs = Math.min(initialDelayMs * 2 ** attempt, maxDelayMs);
|
|
319
304
|
await waitWithSignal(delayMs, opts?.signal);
|
|
320
305
|
}
|
|
321
306
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//import { exec as execCallback } from "node:child_process";
|
|
2
|
+
//import { setupLogging } from "@/common//log";
|
|
3
|
+
//import type { ClientOptions } from "./client";
|
|
4
|
+
//import { InternalError } from "./errors";
|
|
5
|
+
//import { Client } from "./mod.ts";
|
|
6
|
+
//
|
|
7
|
+
///**
|
|
8
|
+
// * Uses the Rivet CLI to read the manager endpoint to connect to. This allows
|
|
9
|
+
// * for writing tests that run locally without hardcoding the manager endpoint.
|
|
10
|
+
// */
|
|
11
|
+
//export async function readEndpointFromCli(): Promise<string> {
|
|
12
|
+
// // Read endpoint
|
|
13
|
+
// const cliPath = process.env.RIVET_CLI_PATH ?? "rivet";
|
|
14
|
+
//
|
|
15
|
+
// try {
|
|
16
|
+
// const { stdout, stderr } = await new Promise<{
|
|
17
|
+
// stdout: string;
|
|
18
|
+
// stderr: string;
|
|
19
|
+
// }>((resolve, reject) => {
|
|
20
|
+
// execCallback(`${cliPath} manager endpoint`, (error, stdout, stderr) => {
|
|
21
|
+
// if (error) reject(error);
|
|
22
|
+
// else resolve({ stdout, stderr });
|
|
23
|
+
// });
|
|
24
|
+
// });
|
|
25
|
+
//
|
|
26
|
+
// if (stderr) {
|
|
27
|
+
// throw new Error(stderr);
|
|
28
|
+
// }
|
|
29
|
+
//
|
|
30
|
+
// // Decode output
|
|
31
|
+
// return stdout.trim();
|
|
32
|
+
// } catch (error) {
|
|
33
|
+
// throw new InternalError(`Read endpoint failed: ${error}`);
|
|
34
|
+
// }
|
|
35
|
+
//}
|
|
36
|
+
//
|
|
37
|
+
//export class TestClient extends Client {
|
|
38
|
+
// public constructor(opts?: ClientOptions) {
|
|
39
|
+
// // Setup logging automatically
|
|
40
|
+
// setupLogging();
|
|
41
|
+
//
|
|
42
|
+
// super(readEndpointFromCli(), opts);
|
|
43
|
+
// }
|
|
44
|
+
//}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
isInvalidToken,
|
|
4
|
+
isInvalidTokenResponse,
|
|
5
|
+
TokenProvider,
|
|
6
|
+
} from "./token-provider";
|
|
7
|
+
|
|
8
|
+
describe("renewable client tokens", () => {
|
|
9
|
+
test("single-flights initial issuance and concurrent rejection refreshes", async () => {
|
|
10
|
+
const getToken = vi.fn(
|
|
11
|
+
async ({ forceRefresh }: { forceRefresh: boolean }) =>
|
|
12
|
+
forceRefresh ? "renewed" : "initial",
|
|
13
|
+
);
|
|
14
|
+
const provider = new TokenProvider(getToken);
|
|
15
|
+
expect(
|
|
16
|
+
await Promise.all([provider.current(), provider.current()]),
|
|
17
|
+
).toEqual(["initial", "initial"]);
|
|
18
|
+
expect(getToken).toHaveBeenCalledTimes(1);
|
|
19
|
+
expect(
|
|
20
|
+
await Promise.all([
|
|
21
|
+
provider.refreshIfCurrent("initial"),
|
|
22
|
+
provider.refreshIfCurrent("initial"),
|
|
23
|
+
]),
|
|
24
|
+
).toEqual(["renewed", "renewed"]);
|
|
25
|
+
expect(getToken).toHaveBeenCalledTimes(2);
|
|
26
|
+
expect(await provider.refreshIfCurrent("initial")).toBe("renewed");
|
|
27
|
+
expect(getToken).toHaveBeenCalledTimes(2);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("retries failed issuance and refuses empty credentials", async () => {
|
|
31
|
+
const getToken = vi
|
|
32
|
+
.fn()
|
|
33
|
+
.mockRejectedValueOnce(new Error("issuer down"))
|
|
34
|
+
.mockResolvedValueOnce("ok");
|
|
35
|
+
const provider = new TokenProvider(getToken);
|
|
36
|
+
await expect(provider.current()).rejects.toThrow("issuer down");
|
|
37
|
+
expect(await provider.current()).toBe("ok");
|
|
38
|
+
await expect(
|
|
39
|
+
new TokenProvider(async () => "").current(),
|
|
40
|
+
).rejects.toThrow("nonempty");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("refreshes before a known JWT expiry and treats opaque tokens as unexpiring hints", async () => {
|
|
44
|
+
const payload = btoa(
|
|
45
|
+
JSON.stringify({ exp: Math.floor(Date.now() / 1_000) + 1 }),
|
|
46
|
+
).replaceAll("=", "");
|
|
47
|
+
const getToken = vi.fn(async () => `header.${payload}.signature`);
|
|
48
|
+
const provider = new TokenProvider(getToken);
|
|
49
|
+
await provider.current();
|
|
50
|
+
await provider.current();
|
|
51
|
+
expect(getToken).toHaveBeenCalledTimes(2);
|
|
52
|
+
const opaque = vi.fn(async () => "opaque");
|
|
53
|
+
const opaqueProvider = new TokenProvider(opaque);
|
|
54
|
+
await opaqueProvider.current();
|
|
55
|
+
await opaqueProvider.current();
|
|
56
|
+
expect(opaque).toHaveBeenCalledTimes(1);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("only confirmed 401 invalid/expired errors permit renewal", () => {
|
|
60
|
+
expect(isInvalidToken("auth", "invalid_token")).toBe(true);
|
|
61
|
+
expect(isInvalidToken("auth", "token_expired")).toBe(true);
|
|
62
|
+
for (const code of [
|
|
63
|
+
"insufficient_permissions",
|
|
64
|
+
"verification_unavailable",
|
|
65
|
+
"issuance_unavailable",
|
|
66
|
+
]) {
|
|
67
|
+
expect(isInvalidToken("auth", code)).toBe(false);
|
|
68
|
+
}
|
|
69
|
+
expect(
|
|
70
|
+
isInvalidTokenResponse(
|
|
71
|
+
new Response(null, {
|
|
72
|
+
status: 401,
|
|
73
|
+
headers: { "x-rivet-error": "auth.token_expired" },
|
|
74
|
+
}),
|
|
75
|
+
),
|
|
76
|
+
).toBe(true);
|
|
77
|
+
expect(
|
|
78
|
+
isInvalidTokenResponse(
|
|
79
|
+
new Response(null, {
|
|
80
|
+
status: 503,
|
|
81
|
+
headers: { "x-rivet-error": "auth.token_expired" },
|
|
82
|
+
}),
|
|
83
|
+
),
|
|
84
|
+
).toBe(false);
|
|
85
|
+
});
|
|
86
|
+
});
|
package/src/client/utils.ts
CHANGED
|
@@ -19,27 +19,21 @@ 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;
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
const RETRY_AFTER_SUFFIX = /^retry_after_ms=(\d+)$/;
|
|
27
|
-
|
|
28
24
|
/**
|
|
29
25
|
* Parses WebSocket close reason string into structured data.
|
|
30
26
|
*
|
|
31
27
|
* Expected format examples:
|
|
32
28
|
* - "guard.actor_runner_failed#t1s80so6h3irenp8ymzltfoittcl00"
|
|
33
29
|
* - "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>`.
|
|
36
30
|
*
|
|
37
31
|
* Returns undefined if the format is invalid
|
|
38
32
|
*/
|
|
39
33
|
export function parseWebSocketCloseReason(
|
|
40
34
|
reason: string,
|
|
41
35
|
): ParsedCloseReason | undefined {
|
|
42
|
-
const [mainPart,
|
|
36
|
+
const [mainPart, rayId] = reason.split("#");
|
|
43
37
|
const [group, code] = mainPart.split(".");
|
|
44
38
|
|
|
45
39
|
if (!group || !code) {
|
|
@@ -47,15 +41,10 @@ export function parseWebSocketCloseReason(
|
|
|
47
41
|
return undefined;
|
|
48
42
|
}
|
|
49
43
|
|
|
50
|
-
const retryAfter = suffix?.match(RETRY_AFTER_SUFFIX);
|
|
51
|
-
if (retryAfter) {
|
|
52
|
-
return { group, code, retryAfterMs: Number(retryAfter[1]) };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
44
|
return {
|
|
56
45
|
group,
|
|
57
46
|
code,
|
|
58
|
-
rayId
|
|
47
|
+
rayId,
|
|
59
48
|
};
|
|
60
49
|
}
|
|
61
50
|
|