Skip to content

Commit 8e30a79

Browse files
authored
fix(client): Fix default gRPC keepalive settings not applied (#906)
1 parent f745212 commit 8e30a79

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

packages/client/src/connection.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ export interface ConnectionOptions {
3939
* GRPC Channel arguments
4040
*
4141
* @see option descriptions {@link https://grpc.github.io/grpc/core/group__grpc__arg__keys.html | here}
42+
*
43+
* By default the SDK sets the following keepalive arguments:
44+
*
45+
* ```
46+
* grpc.keepalive_permit_without_calls: 1
47+
* grpc.keepalive_time_ms: 30_000
48+
* grpc.keepalive_timeout_ms: 15_000
49+
* ```
50+
*
51+
* To opt-out of keepalive, override these keys with `undefined`.
4252
*/
4353
channelArgs?: grpc.ChannelOptions;
4454

@@ -75,18 +85,21 @@ export type ConnectionOptionsWithDefaults = Required<Omit<ConnectionOptions, 'tl
7585

7686
export const LOCAL_TARGET = '127.0.0.1:7233';
7787

78-
export function defaultConnectionOpts(): ConnectionOptionsWithDefaults {
88+
function addDefaults(options: ConnectionOptions): ConnectionOptionsWithDefaults {
89+
const { channelArgs, interceptors, ...rest } = options;
7990
return {
8091
address: LOCAL_TARGET,
8192
credentials: grpc.credentials.createInsecure(),
8293
channelArgs: {
8394
'grpc.keepalive_permit_without_calls': 1,
8495
'grpc.keepalive_time_ms': 30_000,
8596
'grpc.keepalive_timeout_ms': 15_000,
97+
...channelArgs,
8698
},
87-
interceptors: [makeGrpcRetryInterceptor(defaultGrpcRetryOptions())],
99+
interceptors: interceptors ?? [makeGrpcRetryInterceptor(defaultGrpcRetryOptions())],
88100
metadata: {},
89101
connectTimeoutMs: 10_000,
102+
...filterNullAndUndefined(rest),
90103
};
91104
}
92105

@@ -192,10 +205,7 @@ export class Connection {
192205
readonly callContextStorage: AsyncLocalStorage<CallContext>;
193206

194207
protected static createCtorOptions(options?: ConnectionOptions): ConnectionCtorOptions {
195-
const optionsWithDefaults = {
196-
...defaultConnectionOpts(),
197-
...filterNullAndUndefined(normalizeGRPCConfig(options)),
198-
};
208+
const optionsWithDefaults = addDefaults(normalizeGRPCConfig(options));
199209
// Allow overriding this
200210
optionsWithDefaults.metadata['client-name'] ??= 'temporal-typescript';
201211
optionsWithDefaults.metadata['client-version'] ??= pkg.version;

packages/test/src/test-client-connection.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,14 @@ test('grpc retry passes request and headers on retry, propagates responses', asy
161161
});
162162
t.is(attempt, 10);
163163
});
164+
165+
test('Default keepalive settings are set while maintaining user provided channelArgs', async (t) => {
166+
const conn = Connection.lazy({
167+
channelArgs: { 'grpc.enable_channelz': 1, 'grpc.keepalive_permit_without_calls': 0 },
168+
});
169+
const { channelArgs } = conn.options;
170+
t.is(channelArgs['grpc.keepalive_time_ms'], 30_000);
171+
t.is(channelArgs['grpc.enable_channelz'], 1);
172+
// User setting overrides default
173+
t.is(channelArgs['grpc.keepalive_permit_without_calls'], 0);
174+
});

0 commit comments

Comments
 (0)