Skip to content

Commit 28af0d3

Browse files
bryfoxlukasIO
andauthored
Add optional timeouts to clients in server APIs (#560)
* Add timeout option to server clients * Rename * Share options type; remove ctor docs * Re-word * Format * License header * Create honest-apes-sparkle.md --------- Co-authored-by: lukasIO <[email protected]>
1 parent 7754b88 commit 28af0d3

File tree

8 files changed

+67
-13
lines changed

8 files changed

+67
-13
lines changed

.changeset/honest-apes-sparkle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"livekit-server-sdk": patch
3+
---
4+
5+
Add optional timeouts to clients in server APIs

packages/livekit-server-sdk/src/AgentDispatchClient.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ListAgentDispatchRequest,
99
ListAgentDispatchResponse,
1010
} from '@livekit/protocol';
11+
import type { ClientOptions } from './ClientOptions.js';
1112
import { ServiceBase } from './ServiceBase.js';
1213
import { type Rpc, TwirpRpc, livekitPackage } from './TwirpRPC.js';
1314

@@ -29,10 +30,14 @@ export class AgentDispatchClient extends ServiceBase {
2930
* @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'
3031
* @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY
3132
* @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET
33+
* @param options - client options
3234
*/
33-
constructor(host: string, apiKey?: string, secret?: string) {
35+
constructor(host: string, apiKey?: string, secret?: string, options?: ClientOptions) {
3436
super(apiKey, secret);
35-
this.rpc = new TwirpRpc(host, livekitPackage);
37+
const rpcOptions = options?.requestTimeout
38+
? { requestTimeout: options.requestTimeout }
39+
: undefined;
40+
this.rpc = new TwirpRpc(host, livekitPackage, rpcOptions);
3641
}
3742

3843
/**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
/**
6+
* Options common to all clients
7+
*/
8+
export type ClientOptions = {
9+
/**
10+
* Optional timeout, in seconds, for all server requests
11+
*/
12+
requestTimeout?: number;
13+
};

packages/livekit-server-sdk/src/EgressClient.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
UpdateStreamRequest,
2626
WebEgressRequest,
2727
} from '@livekit/protocol';
28+
import type { ClientOptions } from './ClientOptions.js';
2829
import { ServiceBase } from './ServiceBase.js';
2930
import type { Rpc } from './TwirpRPC.js';
3031
import { TwirpRpc, livekitPackage } from './TwirpRPC.js';
@@ -137,10 +138,14 @@ export class EgressClient extends ServiceBase {
137138
* @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'
138139
* @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY
139140
* @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET
141+
* @param options - client options
140142
*/
141-
constructor(host: string, apiKey?: string, secret?: string) {
143+
constructor(host: string, apiKey?: string, secret?: string, options?: ClientOptions) {
142144
super(apiKey, secret);
143-
this.rpc = new TwirpRpc(host, livekitPackage);
145+
const rpcOptions = options?.requestTimeout
146+
? { requestTimeout: options.requestTimeout }
147+
: undefined;
148+
this.rpc = new TwirpRpc(host, livekitPackage, rpcOptions);
144149
}
145150

146151
/**

packages/livekit-server-sdk/src/IngressClient.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ListIngressResponse,
1111
UpdateIngressRequest,
1212
} from '@livekit/protocol';
13+
import type { ClientOptions } from './ClientOptions.js';
1314
import { ServiceBase } from './ServiceBase.js';
1415
import type { Rpc } from './TwirpRPC.js';
1516
import { TwirpRpc, livekitPackage } from './TwirpRPC.js';
@@ -124,10 +125,14 @@ export class IngressClient extends ServiceBase {
124125
* @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'
125126
* @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY
126127
* @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET
128+
* @param options - client options
127129
*/
128-
constructor(host: string, apiKey?: string, secret?: string) {
130+
constructor(host: string, apiKey?: string, secret?: string, options?: ClientOptions) {
129131
super(apiKey, secret);
130-
this.rpc = new TwirpRpc(host, livekitPackage);
132+
const rpcOptions = options?.requestTimeout
133+
? { requestTimeout: options.requestTimeout }
134+
: undefined;
135+
this.rpc = new TwirpRpc(host, livekitPackage, rpcOptions);
131136
}
132137

133138
/**

packages/livekit-server-sdk/src/RoomServiceClient.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
UpdateRoomMetadataRequest,
2323
UpdateSubscriptionsRequest,
2424
} from '@livekit/protocol';
25+
import type { ClientOptions } from './ClientOptions.js';
2526
import { ServiceBase } from './ServiceBase.js';
2627
import type { Rpc } from './TwirpRPC.js';
2728
import { TwirpRpc, livekitPackage } from './TwirpRPC.js';
@@ -120,10 +121,14 @@ export class RoomServiceClient extends ServiceBase {
120121
* @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'
121122
* @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY
122123
* @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET
124+
* @param options - client options
123125
*/
124-
constructor(host: string, apiKey?: string, secret?: string) {
126+
constructor(host: string, apiKey?: string, secret?: string, options?: ClientOptions) {
125127
super(apiKey, secret);
126-
this.rpc = new TwirpRpc(host, livekitPackage);
128+
const rpcOptions = options?.requestTimeout
129+
? { requestTimeout: options.requestTimeout }
130+
: undefined;
131+
this.rpc = new TwirpRpc(host, livekitPackage, rpcOptions);
127132
}
128133

129134
/**

packages/livekit-server-sdk/src/SipClient.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
UpdateSIPInboundTrunkRequest,
4040
UpdateSIPOutboundTrunkRequest,
4141
} from '@livekit/protocol';
42+
import type { ClientOptions } from './ClientOptions.js';
4243
import { ServiceBase } from './ServiceBase.js';
4344
import type { Rpc } from './TwirpRPC.js';
4445
import { TwirpRpc, livekitPackage } from './TwirpRPC.js';
@@ -216,10 +217,14 @@ export class SipClient extends ServiceBase {
216217
* @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'
217218
* @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY
218219
* @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET
220+
* @param options - client options
219221
*/
220-
constructor(host: string, apiKey?: string, secret?: string) {
222+
constructor(host: string, apiKey?: string, secret?: string, options?: ClientOptions) {
221223
super(apiKey, secret);
222-
this.rpc = new TwirpRpc(host, livekitPackage);
224+
const rpcOptions = options?.requestTimeout
225+
? { requestTimeout: options.requestTimeout }
226+
: undefined;
227+
this.rpc = new TwirpRpc(host, livekitPackage, rpcOptions);
223228
}
224229

225230
/**

packages/livekit-server-sdk/src/TwirpRPC.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import type { JsonValue } from '@bufbuild/protobuf';
55

66
// twirp RPC adapter for client implementation
77

8+
type Options = {
9+
/** Prefix for the RPC requests */
10+
prefix?: string;
11+
/** Timeout for fetch requests, in seconds. Must be within the valid range for abort signal timeouts. */
12+
requestTimeout?: number;
13+
};
14+
815
const defaultPrefix = '/twirp';
16+
const defaultTimeoutSeconds = 60;
917

1018
export const livekitPackage = 'livekit';
1119
export interface Rpc {
@@ -48,21 +56,24 @@ export class TwirpRpc {
4856

4957
prefix: string;
5058

51-
constructor(host: string, pkg: string, prefix?: string) {
59+
requestTimeout: number;
60+
61+
constructor(host: string, pkg: string, options?: Options) {
5262
if (host.startsWith('ws')) {
5363
host = host.replace('ws', 'http');
5464
}
5565
this.host = host;
5666
this.pkg = pkg;
57-
this.prefix = prefix || defaultPrefix;
67+
this.requestTimeout = options?.requestTimeout ?? defaultTimeoutSeconds;
68+
this.prefix = options?.prefix || defaultPrefix;
5869
}
5970

6071
async request(
6172
service: string,
6273
method: string,
6374
data: any, // eslint-disable-line @typescript-eslint/no-explicit-any
6475
headers: any, // eslint-disable-line @typescript-eslint/no-explicit-any
65-
timeout = 60,
76+
timeout = this.requestTimeout,
6677
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6778
): Promise<any> {
6879
const path = `${this.prefix}/${this.pkg}.${service}/${method}`;

0 commit comments

Comments
 (0)