Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions rivetkit-typescript/packages/rivetkit/src/client/actor-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from "@/schemas/client-protocol-zod/mod";
import { deserializeWithEncoding, serializeWithEncoding } from "@/serde";
import { bufferToArrayBuffer, promiseWithResolvers } from "@/utils";
import { getLogMessage } from "@/utils/env-vars";
import { getLogMessage, isLocalDev } from "@/utils/env-vars";
import type { ActorDefinitionActions } from "./actor-common";
import { checkForSchedulingError, queryActor } from "./actor-query";
import { ACTOR_CONNS_SYMBOL, type ClientRaw } from "./client";
Expand Down Expand Up @@ -304,12 +304,23 @@ enc
#connectWithRetry() {
this.#setConnStatus("connecting");

const retryTimeouts = isLocalDev()
? {
minTimeout: 1_000,
maxTimeout: 1_000,
factor: 1,
randomize: false,
}
: {
minTimeout: 250,
maxTimeout: 30_000,
};

// Attempt to reconnect indefinitely
// This is intentionally not awaited - connection happens in background
pRetry(this.#connectAndWait.bind(this), {
forever: true,
minTimeout: 250,
maxTimeout: 30_000,
...retryTimeouts,

onFailedAttempt: (error) => {
logger().warn({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import pRetry from "p-retry";
import type { ClientConfig } from "@/client/client";
import type { MetadataResponse } from "@/common/router";
import { stringifyError } from "@/common/utils";
import { isLocalDev } from "@/utils/env-vars";
import { getMetadata } from "./api-endpoints";
import { getEndpoint } from "./api-utils";
import { logger } from "./log";
Expand All @@ -21,6 +22,17 @@ export async function lookupMetadataCached(
}

// Create and store the promise immediately to prevent racing requests
const retryTimeouts = isLocalDev()
? {
minTimeout: 1_000,
maxTimeout: 1_000,
factor: 1,
randomize: false,
}
: {
minTimeout: 500,
maxTimeout: 15_000,
};
const metadataLookupPromise = pRetry(
async () => {
logger().debug({
Expand All @@ -40,8 +52,7 @@ export async function lookupMetadataCached(
},
{
forever: true,
minTimeout: 500,
maxTimeout: 15_000,
...retryTimeouts,
onFailedAttempt: (error) => {
// Skip logging warning on first attempt since this attempt
// fails if called immediately on startup. This is because the
Expand Down
15 changes: 15 additions & 0 deletions rivetkit-typescript/packages/rivetkit/src/utils/env-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,18 @@ export const getNodeEnv = (): string | undefined => getEnvUniversal("NODE_ENV");
export const getNextPhase = (): string | undefined =>
getEnvUniversal("NEXT_PHASE");
export const isDev = (): boolean => getNodeEnv() !== "production";
export const isLocalDev = (): boolean => {
if (typeof window !== "undefined") {
const hostname = window?.location?.hostname;
if (hostname) {
return (
hostname === "localhost" ||
hostname === "127.0.0.1" ||
hostname === "::1" ||
hostname === "[::1]"
);
}
}

return isDev();
};
Loading