Skip to content

Commit 3e7e7f5

Browse files
Add options in ingress client to accept timeout or signal (#451)
1 parent d59dba7 commit 3e7e7f5

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

packages/restate-sdk-clients/src/api.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,27 @@ export interface IngresCallOptions<I = unknown, O = unknown> {
101101
input?: Serde<I>;
102102

103103
output?: Serde<O>;
104+
105+
/**
106+
* Timeout to be used when executing the request. In milliseconds.
107+
*
108+
* Same as {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal#aborting_a_fetch_with_timeout_or_explicit_abort | AbortSignal.timeout()}.
109+
*
110+
* This field is exclusive with `signal`, and using both of them will result in a runtime failure.
111+
*/
112+
timeout?: number;
113+
114+
/**
115+
* Signal to abort the underlying `fetch` operation. See {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}.
116+
*
117+
* This field is exclusive with `timeout`, and using both of them will result in a runtime failure.
118+
*/
119+
signal?: AbortSignal;
104120
}
105121

106122
export interface IngresSendOptions<I> extends IngresCallOptions<I, void> {
107123
/**
108-
* If set, the invocation will be executed after the provided delay. In milliseconds.
124+
* If set, the invocation will be enqueued now to be executed after the provided delay. In milliseconds.
109125
*/
110126
delay?: number;
111127
}

packages/restate-sdk-clients/src/ingress.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@ const doComponentInvocation = async <I, O>(
171171
headers[IDEMPOTENCY_KEY_HEADER] = idempotencyKey;
172172
attachable = true;
173173
}
174+
175+
// Abort signal, if any
176+
let signal: AbortSignal | undefined;
177+
if (
178+
params.opts?.opts.signal !== undefined &&
179+
params.opts?.opts.timeout !== undefined
180+
) {
181+
throw new Error(
182+
"You can't specify both signal and timeout options at the same time"
183+
);
184+
} else if (params.opts?.opts.signal !== undefined) {
185+
signal = params.opts?.opts.signal;
186+
} else if (params.opts?.opts.timeout !== undefined) {
187+
signal = AbortSignal.timeout(params.opts?.opts.timeout);
188+
}
189+
174190
//
175191
// make the call
176192
//
@@ -179,6 +195,7 @@ const doComponentInvocation = async <I, O>(
179195
method: params.method ?? "POST",
180196
headers,
181197
body,
198+
signal,
182199
});
183200
if (!httpResponse.ok) {
184201
const body = await httpResponse.text();

packages/restate-sdk-examples/src/ingress_client.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ const idempotentCall = async (name: string, idempotencyKey: string) => {
5151
console.log(greeting);
5252
};
5353

54+
const callWithTimeout = async (name: string) => {
55+
const greeter = ingress.serviceClient<Greeter>({ name: "greeter" });
56+
57+
const greeting = await greeter.greet(
58+
name,
59+
restate.rpc.opts({ timeout: 5 * 1000 })
60+
);
61+
62+
console.log(greeting);
63+
};
64+
5465
const customHeadersCall = async (name: string) => {
5566
const greeter = ingress.serviceClient(Greeter);
5667

@@ -178,6 +189,7 @@ Promise.resolve()
178189
.then(() => objectCall("bob"))
179190
.then(() => objectCall("mop"))
180191
.then(() => simpleCall("bob"))
192+
.then(() => callWithTimeout("lateBob"))
181193
.then(() => sendAndCollectResultLater("boby"))
182194
.then(() => workflow("boby"))
183195
.then(() => idempotentCall("joe", "idemp-1"))

0 commit comments

Comments
 (0)