Skip to content

chore: throw error if attempting to create actor multiple times #960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
10 changes: 10 additions & 0 deletions packages/actor-core/src/actor/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ export class ActorNotFound extends ActorError {
}
}

export class ActorAlreadyExists extends ActorError {
constructor(name: string, key: string[]) {
super(
"actor_already_exists",
`Actor already exists with name "${name}" and key ${JSON.stringify(key)}`,
{ public: true }
);
}
}

export class ProxyError extends ActorError {
constructor(operation: string, error?: unknown) {
super(
Expand Down
8 changes: 8 additions & 0 deletions packages/actor-core/src/test/driver/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import type {
GetWithKeyInput,
ManagerDriver,
} from "@/driver-helpers/mod";
import { ActorAlreadyExists } from "@/actor/errors";
import type { TestGlobalState } from "./global-state";
import * as crypto from "node:crypto";
import { ManagerInspector } from "@/inspector/manager";
import type { ActorCoreApp } from "@/app/mod";

Expand Down Expand Up @@ -117,6 +119,12 @@ export class TestManagerDriver implements ManagerDriver {
name,
key,
}: CreateActorInput): Promise<CreateActorOutput> {
// Check if actor with the same name and key already exists
const existingActor = await this.getWithKey({ name, key });
if (existingActor) {
throw new ActorAlreadyExists(name, key);
}

const actorId = crypto.randomUUID();
this.#state.createActor(actorId, name, key);

Expand Down
7 changes: 7 additions & 0 deletions packages/drivers/file-system/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
GetWithKeyInput,
ManagerDriver,
} from "actor-core/driver-helpers";
import { ActorAlreadyExists } from "actor-core/actor/errors";
import { logger } from "./log";
import type { FileSystemGlobalState } from "./global-state";
import type { ActorCoreApp } from "actor-core";
Expand Down Expand Up @@ -95,6 +96,12 @@ export class FileSystemManagerDriver implements ManagerDriver {
name,
key,
}: CreateActorInput): Promise<CreateActorOutput> {
// Check if actor with the same name and key already exists
const existingActor = await this.getWithKey({ name, key });
if (existingActor) {
throw new ActorAlreadyExists(name, key);
}

const actorId = crypto.randomUUID();
await this.#state.createActor(actorId, name, key);

Expand Down
8 changes: 8 additions & 0 deletions packages/drivers/memory/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import type {
GetWithKeyInput,
ManagerDriver,
} from "actor-core/driver-helpers";
import { ActorAlreadyExists } from "actor-core/actor/errors";
import type { MemoryGlobalState } from "./global-state";
import * as crypto from "node:crypto";
import { ManagerInspector } from "actor-core/inspector";
import type { ActorCoreApp } from "actor-core";

Expand Down Expand Up @@ -86,6 +88,12 @@ export class MemoryManagerDriver implements ManagerDriver {
name,
key,
}: CreateActorInput): Promise<CreateActorOutput> {
// Check if actor with the same name and key already exists
const existingActor = await this.getWithKey({ name, key });
if (existingActor) {
throw new ActorAlreadyExists(name, key);
}

const actorId = crypto.randomUUID();
this.#state.createActor(actorId, name, key);

Expand Down
8 changes: 8 additions & 0 deletions packages/drivers/redis/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import type {
GetWithKeyInput,
ManagerDriver,
} from "actor-core/driver-helpers";
import { ActorAlreadyExists } from "actor-core/actor/errors";
import type Redis from "ioredis";
import * as crypto from "node:crypto";
import { KEYS } from "./keys";
import { ManagerInspector } from "actor-core/inspector";
import type { ActorCoreApp } from "actor-core";
Expand Down Expand Up @@ -93,6 +95,12 @@ export class RedisManagerDriver implements ManagerDriver {
name,
key,
}: CreateActorInput): Promise<CreateActorOutput> {
// Check if actor with the same name and key already exists
const existingActor = await this.getWithKey({ name, key });
if (existingActor) {
throw new ActorAlreadyExists(name, key);
}

const actorId = crypto.randomUUID();
const actorKeyRedisKey = this.#generateActorKeyRedisKey(name, key);

Expand Down
7 changes: 7 additions & 0 deletions packages/platforms/cloudflare-workers/src/manager-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
CreateActorInput,
GetActorOutput,
} from "actor-core/driver-helpers";
import { ActorAlreadyExists } from "actor-core/actor/errors";
import { Bindings } from "./mod";
import { logger } from "./log";
import { serializeNameAndKey, serializeKey } from "./util";
Expand Down Expand Up @@ -112,6 +113,12 @@ export class CloudflareWorkersManagerDriver implements ManagerDriver {
if (!c) throw new Error("Missing Hono context");
const log = logger();

// Check if actor with the same name and key already exists
const existingActor = await this.getWithKey({ c, name, key });
if (existingActor) {
throw new ActorAlreadyExists(name, key);
}

// Create a deterministic ID from the actor name and key
// This ensures that actors with the same name and key will have the same ID
const nameKeyString = serializeNameAndKey(name, key);
Expand Down
7 changes: 7 additions & 0 deletions packages/platforms/rivet/src/manager-driver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertUnreachable } from "actor-core/utils";
import { ActorAlreadyExists } from "actor-core/actor/errors";
import type {
ManagerDriver,
GetForIdInput,
Expand Down Expand Up @@ -121,6 +122,12 @@ export class RivetManagerDriver implements ManagerDriver {
key,
region,
}: CreateActorInput): Promise<GetActorOutput> {
// Check if actor with the same name and key already exists
const existingActor = await this.getWithKey({ name, key });
if (existingActor) {
throw new ActorAlreadyExists(name, key);
}

// Create the actor request
let actorLogLevel: string | undefined = undefined;
if (typeof Deno !== "undefined") {
Expand Down
Loading