Skip to content

Commit

Permalink
Add options in ingress client to accept timeout or signal
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Oct 21, 2024
1 parent d59dba7 commit 9c76bff
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/restate-sdk-clients/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,27 @@ export interface IngresCallOptions<I = unknown, O = unknown> {
input?: Serde<I>;

output?: Serde<O>;

/**
* Timeout to be used when executing the request. In milliseconds.
*
* Same as {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal#aborting_a_fetch_with_timeout_or_explicit_abort | AbortSignal.timeout()}.
*
* This field is exclusive with `signal`, and using both of them will result in a runtime failure.
*/
timeout?: number;

/**
* Signal to abort the underlying `fetch` operation. See {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}.
*
* This field is exclusive with `timeout`, and using both of them will result in a runtime failure.
*/
signal?: AbortSignal;
}

export interface IngresSendOptions<I> extends IngresCallOptions<I, void> {
/**
* If set, the invocation will be executed after the provided delay. In milliseconds.
* If set, the invocation will be enqueued now to be executed after the provided delay. In milliseconds.
*/
delay?: number;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/restate-sdk-clients/src/ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ const doComponentInvocation = async <I, O>(
headers[IDEMPOTENCY_KEY_HEADER] = idempotencyKey;
attachable = true;
}

// Abort signal, if any
let signal: AbortSignal | undefined;
if (
params.opts?.opts.signal !== undefined &&
params.opts?.opts.timeout !== undefined
) {
throw new Error(
"You can't specify both signal and timeout options at the same time"
);
} else if (params.opts?.opts.signal !== undefined) {
signal = params.opts?.opts.signal;
} else if (params.opts?.opts.timeout !== undefined) {
signal = AbortSignal.timeout(params.opts?.opts.timeout);
}

//
// make the call
//
Expand All @@ -179,6 +195,7 @@ const doComponentInvocation = async <I, O>(
method: params.method ?? "POST",
headers,
body,
signal,
});
if (!httpResponse.ok) {
const body = await httpResponse.text();
Expand Down
12 changes: 12 additions & 0 deletions packages/restate-sdk-examples/src/ingress_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ const idempotentCall = async (name: string, idempotencyKey: string) => {
console.log(greeting);
};

const callWithTimeout = async (name: string) => {
const greeter = ingress.serviceClient<Greeter>({ name: "greeter" });

const greeting = await greeter.greet(
name,
restate.rpc.opts({ timeout: 5 * 1000 })
);

console.log(greeting);
};

const customHeadersCall = async (name: string) => {
const greeter = ingress.serviceClient(Greeter);

Expand Down Expand Up @@ -178,6 +189,7 @@ Promise.resolve()
.then(() => objectCall("bob"))
.then(() => objectCall("mop"))
.then(() => simpleCall("bob"))
.then(() => callWithTimeout("lateBob"))
.then(() => sendAndCollectResultLater("boby"))
.then(() => workflow("boby"))
.then(() => idempotentCall("joe", "idemp-1"))
Expand Down

0 comments on commit 9c76bff

Please sign in to comment.