Skip to content
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
12 changes: 12 additions & 0 deletions docs/actors/content/docs/actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ The actor client includes type safety out of the box. When you use `createClient

</CodeGroup>

### Timeouts and Cancellation

Pass a `signal` to stop waiting for an action call, for example with `AbortSignal.timeout()`. Use `.action({ name, args, signal })` for this, since the shorthand `counterActor.increment(1)` doesn't take options. It works the same on handles and connections:

<CodeSnippet file="examples/docs/actors-actions/calling-with-signal.ts" title="client.ts" />

When the signal aborts, the call rejects with the signal's reason. If the action was already sent, it may still run on the actor; only the client stops waiting. On a connection, an action still queued while the connection reconnects is dropped and never sent.

On a handle, a `signal` passed to `get()` or `getOrCreate()` applies to every action call that doesn't pass its own.

To cancel work inside the actor itself, see [Canceling Long-Running Actions](#canceling-long-running-actions).

## Error Handling

Actors provide robust error handling out of the box for actions.
Expand Down
35 changes: 35 additions & 0 deletions examples/docs/actors-actions/calling-with-signal.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 81 additions & 34 deletions rivetkit-typescript/packages/rivetkit/src/client/actor-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,26 @@ export type ActorConnStatus =
| "connected"
| "disconnected";

interface QueuedMessage {
body:
| {
tag: "ActionRequest";
val: { id: bigint; name: string; args: unknown };
}
| {
tag: "SubscriptionRequest";
val: { eventName: string; subscribe: boolean };
};
}

function abortReason(signal: AbortSignal): unknown {
return signal.reason ?? new Error("Operation aborted");
}

interface ActionInFlight {
name: string;
resolve: (response: { id: bigint; output: unknown }) => void;
reject: (error: Error) => void;
reject: (error: unknown) => void;
}

interface EventSubscriptions<Args extends Array<unknown>> {
Expand Down Expand Up @@ -167,17 +183,7 @@ export class ActorConnRaw {
#actorId?: string;
#connId?: string;

#messageQueue: Array<{
body:
| {
tag: "ActionRequest";
val: { id: bigint; name: string; args: unknown };
}
| {
tag: "SubscriptionRequest";
val: { eventName: string; subscribe: boolean };
};
}> = [];
#messageQueue: QueuedMessage[] = [];
#actionsInFlight = new Map<number, ActionInFlight>();

#eventSubscriptions = new Map<string, Set<EventSubscriptions<any[]>>>();
Expand Down Expand Up @@ -350,6 +356,10 @@ export class ActorConnRaw {
* @template Response - The type of the response returned by the action function.
* @param {string} name - The name of the action function to call.
* @param {...Args} args - The arguments to pass to the action function.
* @param {AbortSignal} [signal] - Gives up on the call. While the connection is reopening the
* action waits in a queue; aborting then removes it, so it is never sent. Once sent, aborting
* only rejects this promise and the action may still run. Use `AbortSignal.timeout(ms)` for
* a deadline.
* @returns {Promise<Response>} - A promise that resolves to the response of the action function.
*/
async action<
Expand All @@ -372,6 +382,8 @@ export class ActorConnRaw {
}
if (this.#disposed) throw new errors.ActorConnDisposed();
if (this.#connectionOpenError) throw this.#connectionOpenError;
const { signal } = opts;
if (signal?.aborted) throw abortReason(signal);
logger().debug({ msg: "action", name: opts.name, args: opts.args });

// If we have an active connection, use the websockactionId
Expand All @@ -394,7 +406,7 @@ export class ActorConnRaw {
inFlightCount: this.#actionsInFlight.size,
});

this.#sendMessage({
const message: QueuedMessage = {
body: {
tag: "ActionRequest",
val: {
Expand All @@ -403,17 +415,65 @@ export class ActorConnRaw {
args: opts.args,
},
},
});
};
this.#sendMessage(message);

const onAbort = signal
? () => this.#abortAction(actionId, message, signal)
: undefined;
if (signal && onAbort) {
signal.addEventListener("abort", onAbort, { once: true });
}

// TODO: Throw error if disconnect is called

const { id: responseId, output } = await promise;
if (responseId !== BigInt(actionId))
throw new Error(
`Request ID ${actionId} does not match response ID ${responseId}`,
);
try {
const { id: responseId, output } = await promise;
if (responseId !== BigInt(actionId))
throw new Error(
`Request ID ${actionId} does not match response ID ${responseId}`,
);

return output as Response;
} finally {
if (signal && onAbort) {
signal.removeEventListener("abort", onAbort);
}
}
}

return output as Response;
/**
* Gives up on an action whose caller's signal aborted. A message still in the queue is
* removed so it is never sent after a reconnect. A message already sent may still run,
* so its in-flight entry stays to absorb the response; only the caller's promise rejects.
*/
#abortAction(
actionId: number,
message: QueuedMessage,
signal: AbortSignal,
) {
const inFlight = this.#actionsInFlight.get(actionId);
if (!inFlight) return;

const reason = abortReason(signal);
const queueIndex = this.#messageQueue.indexOf(message);
if (queueIndex !== -1) {
this.#messageQueue.splice(queueIndex, 1);
this.#actionsInFlight.delete(actionId);
logger().debug({
msg: "aborted queued action before send",
actionId,
actionName: inFlight.name,
queueLength: this.#messageQueue.length,
});
} else {
logger().debug({
msg: "aborted action after send, outcome unknown",
actionId,
actionName: inFlight.name,
});
}
inFlight.reject(reason);
}

/**
Expand Down Expand Up @@ -1391,20 +1451,7 @@ export class ActorConnRaw {
};
}

#sendMessage(
message: {
body:
| {
tag: "ActionRequest";
val: { id: bigint; name: string; args: unknown };
}
| {
tag: "SubscriptionRequest";
val: { eventName: string; subscribe: boolean };
};
},
opts?: SendHttpMessageOpts,
) {
#sendMessage(message: QueuedMessage, opts?: SendHttpMessageOpts) {
if (this.#disposed) {
if (opts?.ephemeral) {
return;
Expand Down