Skip to content

Commit e33be81

Browse files
Merge pull request #15975 from OlegStrokan/feat/keepalive-server-support
feat(microservices): add keepalive support for server side
2 parents 3723412 + 62242bc commit e33be81

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

packages/microservices/server/server-grpc.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,34 @@ export class ServerGrpc extends Server<never, never> {
129129
return services;
130130
}
131131

132+
public getKeepaliveOptions() {
133+
if (!isObject(this.options.keepalive)) {
134+
return {};
135+
}
136+
const keepaliveKeys: Record<string, string> = {
137+
keepaliveTimeMs: 'grpc.keepalive_time_ms',
138+
keepaliveTimeoutMs: 'grpc.keepalive_timeout_ms',
139+
keepalivePermitWithoutCalls: 'grpc.keepalive_permit_without_calls',
140+
http2MaxPingsWithoutData: 'grpc.http2.max_pings_without_data',
141+
http2MinTimeBetweenPingsMs: 'grpc.http2.min_time_between_pings_ms',
142+
http2MinPingIntervalWithoutDataMs:
143+
'grpc.http2.min_ping_interval_without_data_ms',
144+
http2MaxPingStrikes: 'grpc.http2.max_ping_strikes',
145+
};
146+
147+
const keepaliveOptions = {};
148+
for (const [optionKey, optionValue] of Object.entries(
149+
this.options.keepalive,
150+
)) {
151+
const key = keepaliveKeys[optionKey];
152+
if (key === undefined) {
153+
continue;
154+
}
155+
keepaliveOptions[key] = optionValue;
156+
}
157+
return keepaliveOptions;
158+
}
159+
132160
/**
133161
* Will create service mapping from gRPC generated Object to handlers
134162
* defined with @GrpcMethod or @GrpcStreamMethod annotations
@@ -568,7 +596,15 @@ export class ServerGrpc extends Server<never, never> {
568596
if (this.options && this.options.maxMetadataSize) {
569597
channelOptions['grpc.max_metadata_size'] = this.options.maxMetadataSize;
570598
}
571-
const server = new grpcPackage.Server(channelOptions);
599+
600+
const keepaliveOptions = this.getKeepaliveOptions();
601+
const options: Record<string, string | number> = {
602+
...channelOptions,
603+
...keepaliveOptions,
604+
};
605+
606+
// Use merged options instead of just channelOptions
607+
const server = new grpcPackage.Server(options);
572608
const credentials = this.getOptionsProp(this.options, 'credentials');
573609

574610
await new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)