Skip to content

Commit

Permalink
Add default request timeout (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikrsna-buf authored Oct 2, 2023
1 parent d07926a commit 908e0dd
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/connect-node/src/connect-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ type ConnectTransportOptions = NodeTransportOptions & {
* available, on side-effect free methods. Defaults to false.
*/
useHttpGet?: boolean;

/**
* The timeout in milliseconds to apply to all requests.
*
* This can be overridden on a per-request basis by passing a timeoutMs.
*/
defaultTimeoutMs?: number;
};

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/connect-node/src/grpc-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ type GrpcTransportOptions = NodeTransportOptions & {
* The default limit is the maximum supported value of ~4GiB.
*/
writeMaxBytes?: number;

/**
* The timeout in milliseconds to apply to all requests.
*
* This can be overridden on a per-request basis by passing a timeoutMs.
*/
defaultTimeoutMs?: number;
};

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/connect-node/src/grpc-web-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ type GrpcWebTransportOptions = NodeTransportOptions & {
* The default limit is the maximum supported value of ~4GiB.
*/
writeMaxBytes?: number;

/**
* The timeout in milliseconds to apply to all requests.
*
* This can be overridden on a per-request basis by passing a timeoutMs.
*/
defaultTimeoutMs?: number;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/connect-web-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ it like a web server would usually do.

| code generator | bundle size | minified | compressed |
|----------------|-------------------:|-----------------------:|---------------------:|
| connect | 113,448 b | 49,872 b | 13,465 b |
| connect | 113,658 b | 49,964 b | 13,487 b |
| grpc-web | 414,071 b | 300,352 b | 53,255 b |
19 changes: 19 additions & 0 deletions packages/connect-web/src/connect-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export interface ConnectTransportOptions {
* available, on side-effect free methods. Defaults to false.
*/
useHttpGet?: boolean;

/**
* The timeout in milliseconds to apply to all requests.
*
* This can be overridden on a per-request basis by passing a timeoutMs.
*/
defaultTimeoutMs?: number;
}

/**
Expand Down Expand Up @@ -142,6 +149,12 @@ export function createConnectTransport(
options.jsonOptions,
options.binaryOptions,
);
timeoutMs =
timeoutMs === undefined
? options.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return await runUnaryCall<I, O>({
interceptors: options.interceptors,
signal,
Expand Down Expand Up @@ -280,6 +293,12 @@ export function createConnectTransport(
return encodeEnvelope(0, serialize(r.value));
}

timeoutMs =
timeoutMs === undefined
? options.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return await runStreamingCall<I, O>({
interceptors: options.interceptors,
timeoutMs,
Expand Down
20 changes: 19 additions & 1 deletion packages/connect-web/src/grpc-web-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ export interface GrpcWebTransportOptions {
* Optional override of the fetch implementation used by the transport.
*/
fetch?: typeof globalThis.fetch;

/**
* The timeout in milliseconds to apply to all requests.
*
* This can be overridden on a per-request basis by passing a timeoutMs.
*/
defaultTimeoutMs?: number;
}

/**
Expand Down Expand Up @@ -137,6 +144,12 @@ export function createGrpcWebTransport(
options.jsonOptions,
options.binaryOptions,
);
timeoutMs =
timeoutMs === undefined
? options.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return await runUnaryCall<I, O>({
interceptors: options.interceptors,
signal,
Expand Down Expand Up @@ -288,7 +301,12 @@ export function createGrpcWebTransport(
}
return encodeEnvelope(0, serialize(r.value));
}

timeoutMs =
timeoutMs === undefined
? options.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return runStreamingCall<I, O>({
interceptors: options.interceptors,
signal,
Expand Down
2 changes: 2 additions & 0 deletions packages/connect/src/call-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
export interface CallOptions {
/**
* Timeout in milliseconds.
*
* Set to <= 0 to disable the default timeout.
*/
timeoutMs?: number;

Expand Down
12 changes: 12 additions & 0 deletions packages/connect/src/protocol-connect/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export function createTransport(opt: CommonTransportOptions): Transport {
opt.jsonOptions,
opt,
);
timeoutMs =
timeoutMs === undefined
? opt.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return await runUnaryCall<I, O>({
interceptors: opt.interceptors,
signal,
Expand Down Expand Up @@ -192,6 +198,12 @@ export function createTransport(opt: CommonTransportOptions): Transport {
const endStreamSerialization = createEndStreamSerialization(
opt.jsonOptions,
);
timeoutMs =
timeoutMs === undefined
? opt.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return runStreamingCall<I, O>({
interceptors: opt.interceptors,
signal,
Expand Down
12 changes: 12 additions & 0 deletions packages/connect/src/protocol-grpc-web/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export function createTransport(opt: CommonTransportOptions): Transport {
opt.jsonOptions,
opt,
);
timeoutMs =
timeoutMs === undefined
? opt.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return await runUnaryCall<I, O>({
interceptors: opt.interceptors,
signal,
Expand Down Expand Up @@ -193,6 +199,12 @@ export function createTransport(opt: CommonTransportOptions): Transport {
opt.jsonOptions,
opt,
);
timeoutMs =
timeoutMs === undefined
? opt.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return runStreamingCall<I, O>({
interceptors: opt.interceptors,
signal,
Expand Down
12 changes: 12 additions & 0 deletions packages/connect/src/protocol-grpc/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export function createTransport(opt: CommonTransportOptions): Transport {
opt.jsonOptions,
opt,
);
timeoutMs =
timeoutMs === undefined
? opt.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return await runUnaryCall<I, O>({
interceptors: opt.interceptors,
signal,
Expand Down Expand Up @@ -169,6 +175,12 @@ export function createTransport(opt: CommonTransportOptions): Transport {
opt.jsonOptions,
opt,
);
timeoutMs =
timeoutMs === undefined
? opt.defaultTimeoutMs
: timeoutMs <= 0
? undefined
: timeoutMs;
return runStreamingCall<I, O>({
interceptors: opt.interceptors,
signal,
Expand Down
7 changes: 7 additions & 0 deletions packages/connect/src/protocol/transport-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,11 @@ export interface CommonTransportOptions {
* available, on side-effect free methods. Defaults to false.
*/
useHttpGet?: boolean;

/**
* The timeout in milliseconds to apply to all requests.
*
* This can be overridden on a per-request basis by passing a timeoutMs.
*/
defaultTimeoutMs?: number;
}

0 comments on commit 908e0dd

Please sign in to comment.