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/dist/browser/client.js
CHANGED
|
@@ -230,7 +230,7 @@ function noopNext() {
|
|
|
230
230
|
// package.json
|
|
231
231
|
var package_default = {
|
|
232
232
|
name: "rivetkit",
|
|
233
|
-
version: "0.0.0-sqlite-
|
|
233
|
+
version: "0.0.0-sqlite-sync-3-0-1-release.95f6e2d",
|
|
234
234
|
description: "Lightweight libraries for building stateful actors on edge platforms",
|
|
235
235
|
license: "Apache-2.0",
|
|
236
236
|
keywords: [
|
|
@@ -440,7 +440,6 @@ var package_default = {
|
|
|
440
440
|
dependencies: {
|
|
441
441
|
"@hono/zod-openapi": "^1.1.5",
|
|
442
442
|
"@opentelemetry/api": "^1.1.0",
|
|
443
|
-
"@rivet-dev/services": "^0.2.3",
|
|
444
443
|
"@rivetkit/bare-ts": "^0.6.2",
|
|
445
444
|
"@rivetkit/engine-api-full": "workspace:*",
|
|
446
445
|
"@rivetkit/engine-cli": "workspace:*",
|
|
@@ -1152,6 +1151,22 @@ function deserializeActorKey(keyString) {
|
|
|
1152
1151
|
}
|
|
1153
1152
|
return parts;
|
|
1154
1153
|
}
|
|
1154
|
+
var CANONICAL_SEPARATOR = ",";
|
|
1155
|
+
var CANONICAL_EMPTY_PART = "\\0";
|
|
1156
|
+
function encodeActorKey(key) {
|
|
1157
|
+
return key.map((part) => {
|
|
1158
|
+
if (part === "") {
|
|
1159
|
+
return CANONICAL_EMPTY_PART;
|
|
1160
|
+
}
|
|
1161
|
+
return part.replace(/\\/g, "\\\\").replace(/,/g, "\\,");
|
|
1162
|
+
}).join(CANONICAL_SEPARATOR);
|
|
1163
|
+
}
|
|
1164
|
+
function actorKeyToGatewayQuery(key) {
|
|
1165
|
+
if (key.length === 0) {
|
|
1166
|
+
return void 0;
|
|
1167
|
+
}
|
|
1168
|
+
return encodeActorKey(key);
|
|
1169
|
+
}
|
|
1155
1170
|
|
|
1156
1171
|
// src/client/token-provider.ts
|
|
1157
1172
|
function isInvalidToken(group, code) {
|
|
@@ -1464,6 +1479,14 @@ var ClientConfigSchemaBase = z3.object({
|
|
|
1464
1479
|
// This is required in the client config in order to support
|
|
1465
1480
|
// `proxyWebSocket`
|
|
1466
1481
|
getUpgradeWebSocket: z3.custom().optional(),
|
|
1482
|
+
/**
|
|
1483
|
+
* Bounds retries of refused requests: `percent` retries per 100 requests (default 10), with
|
|
1484
|
+
* up to `maxTokens` banked for bursts (default 100). A refusal past the budget is thrown.
|
|
1485
|
+
*/
|
|
1486
|
+
retryBudget: z3.object({
|
|
1487
|
+
percent: z3.number().min(0).max(100).optional(),
|
|
1488
|
+
maxTokens: z3.number().int().min(0).optional()
|
|
1489
|
+
}).optional(),
|
|
1467
1490
|
/** Replaces global fetch for actor HTTP gateway requests. */
|
|
1468
1491
|
fetch: z3.custom().optional(),
|
|
1469
1492
|
/** Whether to automatically perform health checks when the client is created. */
|
|
@@ -1780,8 +1803,9 @@ function buildActorQueryGatewayUrl(endpoint, namespace, query, token, path = "",
|
|
|
1780
1803
|
return combineUrlPath(endpoint, gatewayPath);
|
|
1781
1804
|
}
|
|
1782
1805
|
function pushKeyQueryParams(params, key) {
|
|
1783
|
-
|
|
1784
|
-
|
|
1806
|
+
const rvtKey = actorKeyToGatewayQuery(key);
|
|
1807
|
+
if (rvtKey !== void 0) {
|
|
1808
|
+
params.append("rvt-key", rvtKey);
|
|
1785
1809
|
}
|
|
1786
1810
|
}
|
|
1787
1811
|
function pushInputQueryParam(params, input, maxInputSize) {
|
|
@@ -3870,17 +3894,22 @@ function internalClientError(message, opts) {
|
|
|
3870
3894
|
}
|
|
3871
3895
|
|
|
3872
3896
|
// src/client/utils.ts
|
|
3897
|
+
var RETRY_AFTER_SUFFIX = /^retry_after_ms=(\d+)$/;
|
|
3873
3898
|
function parseWebSocketCloseReason(reason) {
|
|
3874
|
-
const [mainPart,
|
|
3899
|
+
const [mainPart, suffix] = reason.split("#");
|
|
3875
3900
|
const [group, code] = mainPart.split(".");
|
|
3876
3901
|
if (!group || !code) {
|
|
3877
3902
|
logger2().warn({ msg: "failed to parse close reason", reason });
|
|
3878
3903
|
return void 0;
|
|
3879
3904
|
}
|
|
3905
|
+
const retryAfter = suffix?.match(RETRY_AFTER_SUFFIX);
|
|
3906
|
+
if (retryAfter) {
|
|
3907
|
+
return { group, code, retryAfterMs: Number(retryAfter[1]) };
|
|
3908
|
+
}
|
|
3880
3909
|
return {
|
|
3881
3910
|
group,
|
|
3882
3911
|
code,
|
|
3883
|
-
rayId
|
|
3912
|
+
rayId: suffix
|
|
3884
3913
|
};
|
|
3885
3914
|
}
|
|
3886
3915
|
function messageLength(message) {
|
|
@@ -4459,7 +4488,7 @@ var RemoteEngineControlClient = class {
|
|
|
4459
4488
|
datacenter: region,
|
|
4460
4489
|
name,
|
|
4461
4490
|
key: serializeActorKey(key),
|
|
4462
|
-
|
|
4491
|
+
pool: poolName ?? this.#config.poolName,
|
|
4463
4492
|
input: actorInput ? uint8ArrayToBase64(
|
|
4464
4493
|
encodeCborCompat(actorInput)
|
|
4465
4494
|
) : void 0,
|
|
@@ -4512,7 +4541,7 @@ var RemoteEngineControlClient = class {
|
|
|
4512
4541
|
(config) => createActor(config, {
|
|
4513
4542
|
datacenter: region,
|
|
4514
4543
|
name,
|
|
4515
|
-
|
|
4544
|
+
pool: poolName ?? this.#config.poolName,
|
|
4516
4545
|
key: serializeActorKey(key),
|
|
4517
4546
|
input: input ? uint8ArrayToBase64(
|
|
4518
4547
|
encodeCborCompat(input)
|
|
@@ -4816,6 +4845,56 @@ async function checkForSchedulingError(group, code, actorId, query, driver, rayI
|
|
|
4816
4845
|
return null;
|
|
4817
4846
|
}
|
|
4818
4847
|
|
|
4848
|
+
// src/client/retry-budget.ts
|
|
4849
|
+
var DEFAULT_PERCENT = 10;
|
|
4850
|
+
var DEFAULT_MAX_TOKENS = 100;
|
|
4851
|
+
var RetryBudget = class {
|
|
4852
|
+
#tokens;
|
|
4853
|
+
#maxTokens;
|
|
4854
|
+
#deposit;
|
|
4855
|
+
constructor(options = {}) {
|
|
4856
|
+
const percent = options.percent ?? DEFAULT_PERCENT;
|
|
4857
|
+
const maxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
4858
|
+
if (!(percent >= 0 && percent <= 100)) {
|
|
4859
|
+
throw new Error("retryBudget.percent must be in 0..=100");
|
|
4860
|
+
}
|
|
4861
|
+
if (!(maxTokens >= 0)) {
|
|
4862
|
+
throw new Error("retryBudget.maxTokens must be at least 0");
|
|
4863
|
+
}
|
|
4864
|
+
this.#maxTokens = maxTokens;
|
|
4865
|
+
this.#tokens = maxTokens;
|
|
4866
|
+
this.#deposit = percent / 100;
|
|
4867
|
+
}
|
|
4868
|
+
/** Credits one request sent. */
|
|
4869
|
+
deposit() {
|
|
4870
|
+
this.#tokens = Math.min(this.#maxTokens, this.#tokens + this.#deposit);
|
|
4871
|
+
}
|
|
4872
|
+
/** Spends one token. Returns `false` when the budget is spent. */
|
|
4873
|
+
tryWithdraw() {
|
|
4874
|
+
if (this.#tokens + 1e-9 < 1) {
|
|
4875
|
+
return false;
|
|
4876
|
+
}
|
|
4877
|
+
this.#tokens = Math.max(0, this.#tokens - 1);
|
|
4878
|
+
return true;
|
|
4879
|
+
}
|
|
4880
|
+
};
|
|
4881
|
+
function retryAfterMs(error) {
|
|
4882
|
+
if (!(error instanceof RivetError)) {
|
|
4883
|
+
return void 0;
|
|
4884
|
+
}
|
|
4885
|
+
const metadata = error.metadata;
|
|
4886
|
+
if (metadata === null || typeof metadata !== "object") {
|
|
4887
|
+
return void 0;
|
|
4888
|
+
}
|
|
4889
|
+
const hint = metadata.retry_after_ms;
|
|
4890
|
+
return typeof hint === "number" && hint >= 0 ? hint : void 0;
|
|
4891
|
+
}
|
|
4892
|
+
function retryDelayMs(attempt, opts) {
|
|
4893
|
+
const backoff = Math.min(opts.initialDelayMs * 2 ** attempt, opts.maxDelayMs);
|
|
4894
|
+
const jittered = backoff / 2 + Math.random() * (backoff / 2);
|
|
4895
|
+
return Math.max(jittered, opts.hintMs ?? 0);
|
|
4896
|
+
}
|
|
4897
|
+
|
|
4819
4898
|
// src/client/lifecycle-errors.ts
|
|
4820
4899
|
function* walkErrorChain(error, maxDepth = 8) {
|
|
4821
4900
|
let current = error;
|
|
@@ -4844,7 +4923,7 @@ function classifyActorError(error) {
|
|
|
4844
4923
|
if (error.group === "actor" && error.code === "stopping" && error.message.includes("database accessed after actor stopped")) {
|
|
4845
4924
|
return void 0;
|
|
4846
4925
|
}
|
|
4847
|
-
if (error.group === "actor" && error.code === "restarting") {
|
|
4926
|
+
if (error.group === "actor" && (error.code === "restarting" || error.code === "migrating_from_v2")) {
|
|
4848
4927
|
return buildLifecycleBoundaryInfo(
|
|
4849
4928
|
"request_retry",
|
|
4850
4929
|
"actor_error",
|
|
@@ -4855,6 +4934,14 @@ function classifyActorError(error) {
|
|
|
4855
4934
|
}
|
|
4856
4935
|
);
|
|
4857
4936
|
}
|
|
4937
|
+
if (error.group === "ups" && error.code === "consumer_closed" || error.group === "depot" && error.code === "request_deadline_exceeded") {
|
|
4938
|
+
return buildLifecycleBoundaryInfo(
|
|
4939
|
+
"reconnect_only",
|
|
4940
|
+
"actor_error",
|
|
4941
|
+
error.message,
|
|
4942
|
+
{ group: error.group, code: error.code }
|
|
4943
|
+
);
|
|
4944
|
+
}
|
|
4858
4945
|
if (error.group === "guard" && isRetryableGuardGatewayHttpError(error.code)) {
|
|
4859
4946
|
return buildLifecycleBoundaryInfo(
|
|
4860
4947
|
"request_retry",
|
|
@@ -4993,6 +5080,7 @@ async function retryOnLifecycleBoundary(run, opts) {
|
|
|
4993
5080
|
const maxAttempts = opts?.maxAttempts ?? 5;
|
|
4994
5081
|
const initialDelayMs = opts?.initialDelayMs ?? 25;
|
|
4995
5082
|
const maxDelayMs = opts?.maxDelayMs ?? 200;
|
|
5083
|
+
opts?.budget?.deposit();
|
|
4996
5084
|
let lastError;
|
|
4997
5085
|
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
4998
5086
|
throwIfAborted(opts?.signal);
|
|
@@ -5006,7 +5094,14 @@ async function retryOnLifecycleBoundary(run, opts) {
|
|
|
5006
5094
|
if (attempt === maxAttempts - 1) {
|
|
5007
5095
|
break;
|
|
5008
5096
|
}
|
|
5009
|
-
|
|
5097
|
+
if (opts?.budget && !opts.budget.tryWithdraw()) {
|
|
5098
|
+
break;
|
|
5099
|
+
}
|
|
5100
|
+
const delayMs = retryDelayMs(attempt, {
|
|
5101
|
+
initialDelayMs,
|
|
5102
|
+
maxDelayMs,
|
|
5103
|
+
hintMs: retryAfterMs(error)
|
|
5104
|
+
});
|
|
5010
5105
|
await waitWithSignal(delayMs, opts?.signal);
|
|
5011
5106
|
}
|
|
5012
5107
|
}
|
|
@@ -5157,12 +5252,18 @@ function createQueueSender(senderOptions) {
|
|
|
5157
5252
|
}
|
|
5158
5253
|
|
|
5159
5254
|
// src/client/actor-conn.ts
|
|
5255
|
+
var CONNECT_TIMEOUT_MS = 2e4;
|
|
5256
|
+
function isConnectionOpenTimeout(error) {
|
|
5257
|
+
return error instanceof RivetError && error.group === "client" && error.code === "connection_open_timeout";
|
|
5258
|
+
}
|
|
5160
5259
|
var CONNECT_SYMBOL = /* @__PURE__ */ Symbol("connect");
|
|
5161
5260
|
var ActorConnRaw = class {
|
|
5162
5261
|
#disposed = false;
|
|
5163
5262
|
/* Will be aborted on dispose. */
|
|
5164
5263
|
#abortController = new AbortController();
|
|
5165
5264
|
#connStatus = "idle";
|
|
5265
|
+
/** The opening failure that stopped retries. Later actions must fail instead of queueing. */
|
|
5266
|
+
#connectionOpenError;
|
|
5166
5267
|
#actorId;
|
|
5167
5268
|
#connId;
|
|
5168
5269
|
#messageQueue = [];
|
|
@@ -5184,6 +5285,8 @@ var ActorConnRaw = class {
|
|
|
5184
5285
|
#keepNodeAliveInterval;
|
|
5185
5286
|
/** Promise used to indicate the socket has connected successfully. This will be rejected if the connection fails. */
|
|
5186
5287
|
#onOpenPromise;
|
|
5288
|
+
/** Sockets given up on by the connect timeout. Their late events are ignored. */
|
|
5289
|
+
#abandonedSockets = /* @__PURE__ */ new WeakSet();
|
|
5187
5290
|
#websocket;
|
|
5188
5291
|
#authRetryAvailable = true;
|
|
5189
5292
|
#client;
|
|
@@ -5279,6 +5382,8 @@ var ActorConnRaw = class {
|
|
|
5279
5382
|
`Invalid action call: expected an options object { name, args }, got ${typeof opts}. Use conn.actionName(...args) for the shorthand API.`
|
|
5280
5383
|
);
|
|
5281
5384
|
}
|
|
5385
|
+
if (this.#disposed) throw new ActorConnDisposed();
|
|
5386
|
+
if (this.#connectionOpenError) throw this.#connectionOpenError;
|
|
5282
5387
|
logger2().debug({ msg: "action", name: opts.name, args: opts.args });
|
|
5283
5388
|
const actionId = this.#actionIdCounter;
|
|
5284
5389
|
this.#actionIdCounter += 1;
|
|
@@ -5366,17 +5471,22 @@ var ActorConnRaw = class {
|
|
|
5366
5471
|
}
|
|
5367
5472
|
}
|
|
5368
5473
|
#connectWithRetry() {
|
|
5474
|
+
this.#connectionOpenError = void 0;
|
|
5369
5475
|
this.#setConnStatus("connecting");
|
|
5370
5476
|
pRetry2(this.#connectAndWait.bind(this), {
|
|
5371
5477
|
forever: true,
|
|
5372
5478
|
minTimeout: 250,
|
|
5373
5479
|
maxTimeout: 3e4,
|
|
5374
|
-
onFailedAttempt: (error) => {
|
|
5480
|
+
onFailedAttempt: async (error) => {
|
|
5375
5481
|
logger2().warn({
|
|
5376
5482
|
msg: "failed to reconnect",
|
|
5377
5483
|
attempt: error.attemptNumber,
|
|
5378
5484
|
error: stringifyError(error)
|
|
5379
5485
|
});
|
|
5486
|
+
const hintMs = retryAfterMs(error);
|
|
5487
|
+
if (hintMs !== void 0) {
|
|
5488
|
+
await new Promise((resolve) => setTimeout(resolve, hintMs));
|
|
5489
|
+
}
|
|
5380
5490
|
},
|
|
5381
5491
|
// Cancel retry if aborted
|
|
5382
5492
|
signal: this.#abortController.signal
|
|
@@ -5402,8 +5512,11 @@ var ActorConnRaw = class {
|
|
|
5402
5512
|
})
|
|
5403
5513
|
);
|
|
5404
5514
|
await this.#connectWebSocket();
|
|
5405
|
-
await this.#onOpenPromise.promise;
|
|
5515
|
+
await this.#waitForOpen(this.#onOpenPromise.promise);
|
|
5406
5516
|
} catch (error) {
|
|
5517
|
+
this.#abandonCurrentSocket(
|
|
5518
|
+
isConnectionOpenTimeout(error) ? "Connect timeout" : "Connection opening failed"
|
|
5519
|
+
);
|
|
5407
5520
|
if (this.#shouldRetryConnectionOpenError(error)) {
|
|
5408
5521
|
throw error;
|
|
5409
5522
|
}
|
|
@@ -5413,7 +5526,7 @@ var ActorConnRaw = class {
|
|
|
5413
5526
|
`Failed to open connection: ${stringifyError(error)}`,
|
|
5414
5527
|
{ error: stringifyError(error) }
|
|
5415
5528
|
);
|
|
5416
|
-
this.#
|
|
5529
|
+
this.#connectionOpenError = actorError;
|
|
5417
5530
|
this.#rejectPendingPromises(actorError, false);
|
|
5418
5531
|
this.#dispatchActorError(actorError);
|
|
5419
5532
|
this.#setConnStatus("idle");
|
|
@@ -5424,10 +5537,61 @@ var ActorConnRaw = class {
|
|
|
5424
5537
|
this.#onOpenPromise = void 0;
|
|
5425
5538
|
}
|
|
5426
5539
|
}
|
|
5540
|
+
/**
|
|
5541
|
+
* Waits for the actor's Init on the current socket, up to {@link CONNECT_TIMEOUT_MS}. A socket
|
|
5542
|
+
* can stay connecting with no open, error or close event, as when the engine holds the
|
|
5543
|
+
* upgrade or a refused connect reports only an error. On timeout the socket is abandoned
|
|
5544
|
+
* and closed, and the retryable `client.connection_open_timeout` starts the next attempt.
|
|
5545
|
+
*/
|
|
5546
|
+
async #waitForOpen(open) {
|
|
5547
|
+
let timer;
|
|
5548
|
+
const timedOut = new Promise((_, reject) => {
|
|
5549
|
+
timer = setTimeout(() => {
|
|
5550
|
+
reject(
|
|
5551
|
+
new RivetError(
|
|
5552
|
+
"client",
|
|
5553
|
+
"connection_open_timeout",
|
|
5554
|
+
`Connection did not open within ${CONNECT_TIMEOUT_MS} ms`
|
|
5555
|
+
)
|
|
5556
|
+
);
|
|
5557
|
+
}, CONNECT_TIMEOUT_MS);
|
|
5558
|
+
});
|
|
5559
|
+
try {
|
|
5560
|
+
await Promise.race([open, timedOut]);
|
|
5561
|
+
} finally {
|
|
5562
|
+
clearTimeout(timer);
|
|
5563
|
+
}
|
|
5564
|
+
}
|
|
5565
|
+
#abandonCurrentSocket(reason) {
|
|
5566
|
+
const ws = this.#websocket;
|
|
5567
|
+
if (!ws) return;
|
|
5568
|
+
this.#websocket = void 0;
|
|
5569
|
+
this.#abandonedSockets.add(ws);
|
|
5570
|
+
logger2().debug({
|
|
5571
|
+
msg: "closing failed connection attempt",
|
|
5572
|
+
connId: this.#connId,
|
|
5573
|
+
readyState: ws.readyState,
|
|
5574
|
+
reason
|
|
5575
|
+
});
|
|
5576
|
+
try {
|
|
5577
|
+
ws.close(1e3, reason);
|
|
5578
|
+
} catch (err) {
|
|
5579
|
+
logger2().debug({
|
|
5580
|
+
msg: "failed to close abandoned websocket",
|
|
5581
|
+
error: stringifyError(err)
|
|
5582
|
+
});
|
|
5583
|
+
}
|
|
5584
|
+
}
|
|
5427
5585
|
#shouldRetryConnectionOpenError(error) {
|
|
5428
5586
|
if (error instanceof ActorConnDisposed) {
|
|
5429
5587
|
return false;
|
|
5430
5588
|
}
|
|
5589
|
+
if (isConnectionOpenTimeout(error)) {
|
|
5590
|
+
return true;
|
|
5591
|
+
}
|
|
5592
|
+
if (error instanceof RivetError && error.group === "client" && error.code === "connection_open_interrupted") {
|
|
5593
|
+
return true;
|
|
5594
|
+
}
|
|
5431
5595
|
if (error instanceof RivetError && isInvalidToken(error.group, error.code) && this.#driver.refreshAuthToken && this.#authRetryAvailable) {
|
|
5432
5596
|
this.#authRetryAvailable = false;
|
|
5433
5597
|
return true;
|
|
@@ -5487,14 +5651,18 @@ var ActorConnRaw = class {
|
|
|
5487
5651
|
});
|
|
5488
5652
|
this.#websocket = ws;
|
|
5489
5653
|
ws.addEventListener("open", () => {
|
|
5654
|
+
if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
|
|
5655
|
+
return;
|
|
5490
5656
|
logger2().debug({
|
|
5491
5657
|
msg: "client websocket open",
|
|
5492
5658
|
connId: this.#connId
|
|
5493
5659
|
});
|
|
5494
5660
|
});
|
|
5495
5661
|
ws.addEventListener("message", async (ev) => {
|
|
5662
|
+
if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
|
|
5663
|
+
return;
|
|
5496
5664
|
try {
|
|
5497
|
-
await this.#handleOnMessage(ev.data);
|
|
5665
|
+
await this.#handleOnMessage(ev.data, ws);
|
|
5498
5666
|
} catch (err) {
|
|
5499
5667
|
logger2().error({
|
|
5500
5668
|
msg: "error in websocket message handler",
|
|
@@ -5503,6 +5671,8 @@ var ActorConnRaw = class {
|
|
|
5503
5671
|
}
|
|
5504
5672
|
});
|
|
5505
5673
|
ws.addEventListener("close", async (ev) => {
|
|
5674
|
+
if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
|
|
5675
|
+
return;
|
|
5506
5676
|
try {
|
|
5507
5677
|
await this.#handleOnClose(ev);
|
|
5508
5678
|
} catch (err) {
|
|
@@ -5513,6 +5683,8 @@ var ActorConnRaw = class {
|
|
|
5513
5683
|
}
|
|
5514
5684
|
});
|
|
5515
5685
|
ws.addEventListener("error", () => {
|
|
5686
|
+
if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
|
|
5687
|
+
return;
|
|
5516
5688
|
try {
|
|
5517
5689
|
this.#handleOnError();
|
|
5518
5690
|
} catch (err) {
|
|
@@ -5578,7 +5750,7 @@ var ActorConnRaw = class {
|
|
|
5578
5750
|
});
|
|
5579
5751
|
}
|
|
5580
5752
|
/** Called by the onmessage event from drivers. */
|
|
5581
|
-
async #handleOnMessage(data) {
|
|
5753
|
+
async #handleOnMessage(data, socket) {
|
|
5582
5754
|
logger2().trace({
|
|
5583
5755
|
msg: "received message",
|
|
5584
5756
|
dataType: typeof data,
|
|
@@ -5586,6 +5758,8 @@ var ActorConnRaw = class {
|
|
|
5586
5758
|
isArrayBuffer: data instanceof ArrayBuffer
|
|
5587
5759
|
});
|
|
5588
5760
|
const response = await this.#parseMessage(data);
|
|
5761
|
+
if (this.#websocket !== socket || this.#abandonedSockets.has(socket))
|
|
5762
|
+
return;
|
|
5589
5763
|
logger2().trace(
|
|
5590
5764
|
getLogMessage() ? {
|
|
5591
5765
|
msg: "parsed message",
|
|
@@ -5657,18 +5831,15 @@ var ActorConnRaw = class {
|
|
|
5657
5831
|
this.#actorResolutionState,
|
|
5658
5832
|
this.#driver
|
|
5659
5833
|
);
|
|
5834
|
+
if (this.#websocket !== socket || this.#abandonedSockets.has(socket)) {
|
|
5835
|
+
return;
|
|
5836
|
+
}
|
|
5660
5837
|
if (schedulingError) {
|
|
5661
5838
|
errorToThrow = schedulingError;
|
|
5662
5839
|
}
|
|
5663
5840
|
}
|
|
5664
|
-
if (this.#onOpenPromise) {
|
|
5665
|
-
this.#onOpenPromise.reject(errorToThrow);
|
|
5666
|
-
}
|
|
5667
5841
|
this.#invalidateActorIfStale(group, code);
|
|
5668
|
-
|
|
5669
|
-
inFlight.reject(errorToThrow);
|
|
5670
|
-
this.#actionsInFlight.delete(id);
|
|
5671
|
-
}
|
|
5842
|
+
this.#rejectPendingPromises(errorToThrow, false);
|
|
5672
5843
|
this.#dispatchActorError(errorToThrow);
|
|
5673
5844
|
}
|
|
5674
5845
|
} else if (response.body.tag === "ActionResponse") {
|
|
@@ -5719,7 +5890,8 @@ var ActorConnRaw = class {
|
|
|
5719
5890
|
const reason = closeEvent.reason || "";
|
|
5720
5891
|
const parsed = parseWebSocketCloseReason(reason);
|
|
5721
5892
|
if (parsed) {
|
|
5722
|
-
const { group, code, rayId } = parsed;
|
|
5893
|
+
const { group, code, rayId, retryAfterMs: retryAfterMs2 } = parsed;
|
|
5894
|
+
const closeOptions = retryAfterMs2 === void 0 ? { rayId } : { rayId, metadata: { retry_after_ms: retryAfterMs2 } };
|
|
5723
5895
|
if (isInvalidToken(group, code) && closeEvent.code === 1008 && failedSocket) {
|
|
5724
5896
|
try {
|
|
5725
5897
|
if (!await this.#driver.refreshAuthToken?.(
|
|
@@ -5741,7 +5913,7 @@ var ActorConnRaw = class {
|
|
|
5741
5913
|
group,
|
|
5742
5914
|
code,
|
|
5743
5915
|
`Connection closed: ${reason}`,
|
|
5744
|
-
|
|
5916
|
+
closeOptions
|
|
5745
5917
|
)
|
|
5746
5918
|
);
|
|
5747
5919
|
return;
|
|
@@ -5762,7 +5934,7 @@ var ActorConnRaw = class {
|
|
|
5762
5934
|
group,
|
|
5763
5935
|
code,
|
|
5764
5936
|
`Connection closed: ${reason}`,
|
|
5765
|
-
|
|
5937
|
+
closeOptions
|
|
5766
5938
|
);
|
|
5767
5939
|
}
|
|
5768
5940
|
} else {
|
|
@@ -5770,14 +5942,17 @@ var ActorConnRaw = class {
|
|
|
5770
5942
|
group,
|
|
5771
5943
|
code,
|
|
5772
5944
|
`Connection closed: ${reason}`,
|
|
5773
|
-
|
|
5945
|
+
closeOptions
|
|
5774
5946
|
);
|
|
5775
5947
|
}
|
|
5776
5948
|
this.#invalidateActorIfStale(group, code);
|
|
5777
5949
|
} else {
|
|
5778
|
-
|
|
5779
|
-
|
|
5780
|
-
|
|
5950
|
+
const message = `${wasClean ? "Connection closed" : "Connection lost"} (code: ${closeEvent.code}, reason: ${reason})`;
|
|
5951
|
+
error = !wasConnected && closeEvent.code === 1006 ? new RivetError(
|
|
5952
|
+
"client",
|
|
5953
|
+
"connection_open_interrupted",
|
|
5954
|
+
message
|
|
5955
|
+
) : new Error(message);
|
|
5781
5956
|
}
|
|
5782
5957
|
this.#rejectPendingPromises(error, false);
|
|
5783
5958
|
if (error instanceof RivetError) {
|
|
@@ -5793,6 +5968,7 @@ var ActorConnRaw = class {
|
|
|
5793
5968
|
}
|
|
5794
5969
|
}
|
|
5795
5970
|
#rejectPendingPromises(error, suppressUnhandled) {
|
|
5971
|
+
this.#clearQueuedMessages();
|
|
5796
5972
|
if (this.#onOpenPromise) {
|
|
5797
5973
|
if (suppressUnhandled) {
|
|
5798
5974
|
this.#onOpenPromise.promise.catch(() => {
|
|
@@ -6454,6 +6630,7 @@ var ActorHandleRaw = class {
|
|
|
6454
6630
|
#queueSendMutex = new AsyncMutex();
|
|
6455
6631
|
#currentActorInvocation;
|
|
6456
6632
|
#startCallSpan;
|
|
6633
|
+
#retryBudget;
|
|
6457
6634
|
/**
|
|
6458
6635
|
* Do not call this directly.
|
|
6459
6636
|
*
|
|
@@ -6461,7 +6638,7 @@ var ActorHandleRaw = class {
|
|
|
6461
6638
|
*
|
|
6462
6639
|
* @protected
|
|
6463
6640
|
*/
|
|
6464
|
-
constructor(client, driver, params, getParams, encoding, actorResolutionState, gatewayOptions = {}, signal, currentActorInvocation, startCallSpan) {
|
|
6641
|
+
constructor(client, driver, params, getParams, encoding, actorResolutionState, gatewayOptions = {}, signal, currentActorInvocation, startCallSpan, retryBudget = new RetryBudget()) {
|
|
6465
6642
|
this.#client = client;
|
|
6466
6643
|
this.#driver = driver;
|
|
6467
6644
|
this.#encoding = encoding;
|
|
@@ -6472,6 +6649,7 @@ var ActorHandleRaw = class {
|
|
|
6472
6649
|
this.#signal = signal;
|
|
6473
6650
|
this.#currentActorInvocation = currentActorInvocation;
|
|
6474
6651
|
this.#startCallSpan = startCallSpan;
|
|
6652
|
+
this.#retryBudget = retryBudget;
|
|
6475
6653
|
}
|
|
6476
6654
|
async #resolveConnectionParams() {
|
|
6477
6655
|
if (this.#getParams) {
|
|
@@ -6488,6 +6666,7 @@ var ActorHandleRaw = class {
|
|
|
6488
6666
|
let useQueryTarget = isDynamicActorQuery(
|
|
6489
6667
|
this.#actorResolutionState
|
|
6490
6668
|
);
|
|
6669
|
+
this.#retryBudget.deposit();
|
|
6491
6670
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
6492
6671
|
let actorId;
|
|
6493
6672
|
try {
|
|
@@ -6521,8 +6700,17 @@ var ActorHandleRaw = class {
|
|
|
6521
6700
|
metadata,
|
|
6522
6701
|
attempt,
|
|
6523
6702
|
maxAttempts
|
|
6524
|
-
)) {
|
|
6525
|
-
await
|
|
6703
|
+
) && this.#retryBudget.tryWithdraw()) {
|
|
6704
|
+
await new Promise(
|
|
6705
|
+
(resolve) => setTimeout(
|
|
6706
|
+
resolve,
|
|
6707
|
+
retryDelayMs(attempt, {
|
|
6708
|
+
initialDelayMs: 25,
|
|
6709
|
+
maxDelayMs: 1e3,
|
|
6710
|
+
hintMs: retryAfterMs(err)
|
|
6711
|
+
})
|
|
6712
|
+
)
|
|
6713
|
+
);
|
|
6526
6714
|
continue;
|
|
6527
6715
|
}
|
|
6528
6716
|
if (await this.#shouldRetrySchedulingError(
|
|
@@ -6597,7 +6785,10 @@ var ActorHandleRaw = class {
|
|
|
6597
6785
|
if (opts.name === "destroy") {
|
|
6598
6786
|
return await run();
|
|
6599
6787
|
}
|
|
6600
|
-
return await retryOnLifecycleBoundary(run, {
|
|
6788
|
+
return await retryOnLifecycleBoundary(run, {
|
|
6789
|
+
signal,
|
|
6790
|
+
budget: this.#retryBudget
|
|
6791
|
+
});
|
|
6601
6792
|
};
|
|
6602
6793
|
if (!call) {
|
|
6603
6794
|
return await send();
|
|
@@ -7137,6 +7328,7 @@ var ClientRaw = class {
|
|
|
7137
7328
|
#gatewayOptions;
|
|
7138
7329
|
#currentActorInvocation;
|
|
7139
7330
|
#startCallSpan;
|
|
7331
|
+
#retryBudget;
|
|
7140
7332
|
/**
|
|
7141
7333
|
* Creates an instance of Client.
|
|
7142
7334
|
*/
|
|
@@ -7149,6 +7341,7 @@ var ClientRaw = class {
|
|
|
7149
7341
|
this.#gatewayOptions = options.gateway ?? {};
|
|
7150
7342
|
this.#currentActorInvocation = options.currentActorInvocation;
|
|
7151
7343
|
this.#startCallSpan = options.startCallSpan;
|
|
7344
|
+
this.#retryBudget = new RetryBudget(options.retryBudget);
|
|
7152
7345
|
}
|
|
7153
7346
|
/**
|
|
7154
7347
|
* Gets a stateless handle to a actor by its ID.
|
|
@@ -7306,7 +7499,8 @@ var ClientRaw = class {
|
|
|
7306
7499
|
this.#gatewayOptions,
|
|
7307
7500
|
signal,
|
|
7308
7501
|
this.#currentActorInvocation,
|
|
7309
|
-
this.#startCallSpan
|
|
7502
|
+
this.#startCallSpan,
|
|
7503
|
+
this.#retryBudget
|
|
7310
7504
|
);
|
|
7311
7505
|
}
|
|
7312
7506
|
[CREATE_ACTOR_CONN_PROXY](conn) {
|