Skip to content

Commit 4bf2fd4

Browse files
Merge pull request #8365 from sjkummer/fix-grpc-channel-options
Fix grpc channel options
2 parents d47fc51 + fdf11a5 commit 4bf2fd4

File tree

4 files changed

+37
-53
lines changed

4 files changed

+37
-53
lines changed

packages/microservices/client/client-grpc.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@ import { Logger } from '@nestjs/common/services/logger.service';
22
import { loadPackage } from '@nestjs/common/utils/load-package.util';
33
import { isFunction, isObject } from '@nestjs/common/utils/shared.utils';
44
import { Observable, Subscription } from 'rxjs';
5-
import {
6-
GRPC_DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH,
7-
GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH,
8-
GRPC_DEFAULT_PROTO_LOADER,
9-
GRPC_DEFAULT_URL,
10-
} from '../constants';
5+
import { GRPC_DEFAULT_PROTO_LOADER, GRPC_DEFAULT_URL } from '../constants';
116
import { InvalidGrpcPackageException } from '../errors/invalid-grpc-package.exception';
127
import { InvalidGrpcServiceException } from '../errors/invalid-grpc-service.exception';
138
import { InvalidProtoDefinitionException } from '../errors/invalid-proto-definition.exception';
149
import { ClientGrpc, GrpcOptions } from '../interfaces';
1510
import { ClientProxy } from './client-proxy';
1611
import { GRPC_CANCELLED } from './constants';
12+
import { ChannelOptions } from '../external/grpc-options.interface';
1713

1814
let grpcPackage: any = {};
1915
let grpcProtoLoaderPackage: any = {};
@@ -66,33 +62,25 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
6662
throw new InvalidGrpcServiceException();
6763
}
6864

69-
const maxSendMessageLengthKey = 'grpc.max_send_message_length';
70-
const maxReceiveMessageLengthKey = 'grpc.max_receive_message_length';
71-
const maxMessageLengthOptions = {
72-
[maxSendMessageLengthKey]: this.getOptionsProp(
73-
this.options,
74-
'maxSendMessageLength',
75-
GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH,
76-
),
77-
[maxReceiveMessageLengthKey]: this.getOptionsProp(
78-
this.options,
79-
'maxReceiveMessageLength',
80-
GRPC_DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH,
81-
),
82-
};
83-
const maxMetadataSize = this.getOptionsProp(
84-
this.options,
85-
'maxMetadataSize',
86-
-1,
87-
);
88-
if (maxMetadataSize > 0) {
89-
maxMessageLengthOptions['grpc.max_metadata_size'] = maxMetadataSize;
65+
const channelOptions: ChannelOptions =
66+
this.options && this.options.channelOptions
67+
? this.options.channelOptions
68+
: {};
69+
if (this.options && this.options.maxSendMessageLength) {
70+
channelOptions['grpc.max_send_message_length'] =
71+
this.options.maxSendMessageLength;
72+
}
73+
if (this.options && this.options.maxReceiveMessageLength) {
74+
channelOptions['grpc.max_receive_message_length'] =
75+
this.options.maxReceiveMessageLength;
76+
}
77+
if (this.options && this.options.maxMetadataSize) {
78+
channelOptions['grpc.max_metadata_size'] = this.options.maxMetadataSize;
9079
}
9180

9281
const keepaliveOptions = this.getKeepaliveOptions();
9382
const options: Record<string, string | number> = {
94-
...(this.options.channelOptions || {}),
95-
...maxMessageLengthOptions,
83+
...channelOptions,
9684
...keepaliveOptions,
9785
};
9886

packages/microservices/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ export const GRPC_DEFAULT_PROTO_LOADER = '@grpc/proto-loader';
3939
export const NO_MESSAGE_HANDLER = `There is no matching message handler defined in the remote service.`;
4040
export const NO_EVENT_HANDLER = `There is no matching event handler defined in the remote service.`;
4141
export const DISCONNECTED_RMQ_MESSAGE = `Disconnected from RMQ. Trying to reconnect.`;
42-
export const GRPC_DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH = 4 * 1024 * 1024;
43-
export const GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH = 4 * 1024 * 1024;
4442

4543
export const KAFKA_DEFAULT_CLIENT = 'nestjs-consumer';
4644
export const KAFKA_DEFAULT_GROUP = 'nestjs-group';

packages/microservices/external/grpc-options.interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* This listing is incomplete. Full reference: https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
44
*/
55
export interface ChannelOptions {
6+
'grpc.max_send_message_length'?: number;
7+
'grpc.max_receive_message_length'?: number;
8+
'grpc.max_metadata_size'?: number;
69
'grpc.ssl_target_name_override'?: string;
710
'grpc.primary_user_agent'?: string;
811
'grpc.secondary_user_agent'?: string;
@@ -12,5 +15,6 @@ export interface ChannelOptions {
1215
'grpc.initial_reconnect_backoff_ms'?: number;
1316
'grpc.max_reconnect_backoff_ms'?: number;
1417
'grpc.use_local_subchannel_pool'?: number;
18+
'grpc-node.max_session_memory'?: number;
1519
[key: string]: string | number | undefined;
1620
}

packages/microservices/server/server-grpc.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { EMPTY, fromEvent, lastValueFrom, Subject } from 'rxjs';
77
import { catchError, takeUntil } from 'rxjs/operators';
88
import {
99
CANCEL_EVENT,
10-
GRPC_DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH,
11-
GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH,
1210
GRPC_DEFAULT_PROTO_LOADER,
1311
GRPC_DEFAULT_URL,
1412
} from '../constants';
@@ -19,6 +17,7 @@ import { InvalidProtoDefinitionException } from '../errors/invalid-proto-definit
1917
import { CustomTransportStrategy, MessageHandler } from '../interfaces';
2018
import { GrpcOptions } from '../interfaces/microservice-configuration.interface';
2119
import { Server } from './server';
20+
import { ChannelOptions } from '../external/grpc-options.interface';
2221

2322
let grpcPackage: any = {};
2423
let grpcProtoLoaderPackage: any = {};
@@ -333,27 +332,22 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
333332
}
334333

335334
public async createClient(): Promise<any> {
336-
const grpcOptions = {
337-
'grpc.max_send_message_length': this.getOptionsProp(
338-
this.options,
339-
'maxSendMessageLength',
340-
GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH,
341-
),
342-
'grpc.max_receive_message_length': this.getOptionsProp(
343-
this.options,
344-
'maxReceiveMessageLength',
345-
GRPC_DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH,
346-
),
347-
};
348-
const maxMetadataSize = this.getOptionsProp(
349-
this.options,
350-
'maxMetadataSize',
351-
-1,
352-
);
353-
if (maxMetadataSize > 0) {
354-
grpcOptions['grpc.max_metadata_size'] = maxMetadataSize;
335+
const channelOptions: ChannelOptions =
336+
this.options && this.options.channelOptions
337+
? this.options.channelOptions
338+
: {};
339+
if (this.options && this.options.maxSendMessageLength) {
340+
channelOptions['grpc.max_send_message_length'] =
341+
this.options.maxSendMessageLength;
342+
}
343+
if (this.options && this.options.maxReceiveMessageLength) {
344+
channelOptions['grpc.max_receive_message_length'] =
345+
this.options.maxReceiveMessageLength;
346+
}
347+
if (this.options && this.options.maxMetadataSize) {
348+
channelOptions['grpc.max_metadata_size'] = this.options.maxMetadataSize;
355349
}
356-
const server = new grpcPackage.Server(grpcOptions);
350+
const server = new grpcPackage.Server(channelOptions);
357351
const credentials = this.getOptionsProp(this.options, 'credentials');
358352

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

0 commit comments

Comments
 (0)