Merge pull request #8365 from sjkummer/fix-grpc-channel-options

Fix grpc channel options
This commit is contained in:
Kamil Mysliwiec
2021-11-08 09:28:16 +01:00
committed by GitHub
4 changed files with 37 additions and 53 deletions

View File

@@ -2,18 +2,14 @@ import { Logger } from '@nestjs/common/services/logger.service';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { isFunction, isObject } from '@nestjs/common/utils/shared.utils';
import { Observable, Subscription } from 'rxjs';
import {
GRPC_DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH,
GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH,
GRPC_DEFAULT_PROTO_LOADER,
GRPC_DEFAULT_URL,
} from '../constants';
import { GRPC_DEFAULT_PROTO_LOADER, GRPC_DEFAULT_URL } from '../constants';
import { InvalidGrpcPackageException } from '../errors/invalid-grpc-package.exception';
import { InvalidGrpcServiceException } from '../errors/invalid-grpc-service.exception';
import { InvalidProtoDefinitionException } from '../errors/invalid-proto-definition.exception';
import { ClientGrpc, GrpcOptions } from '../interfaces';
import { ClientProxy } from './client-proxy';
import { GRPC_CANCELLED } from './constants';
import { ChannelOptions } from '../external/grpc-options.interface';
let grpcPackage: any = {};
let grpcProtoLoaderPackage: any = {};
@@ -66,33 +62,25 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
throw new InvalidGrpcServiceException();
}
const maxSendMessageLengthKey = 'grpc.max_send_message_length';
const maxReceiveMessageLengthKey = 'grpc.max_receive_message_length';
const maxMessageLengthOptions = {
[maxSendMessageLengthKey]: this.getOptionsProp(
this.options,
'maxSendMessageLength',
GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH,
),
[maxReceiveMessageLengthKey]: this.getOptionsProp(
this.options,
'maxReceiveMessageLength',
GRPC_DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH,
),
};
const maxMetadataSize = this.getOptionsProp(
this.options,
'maxMetadataSize',
-1,
);
if (maxMetadataSize > 0) {
maxMessageLengthOptions['grpc.max_metadata_size'] = maxMetadataSize;
const channelOptions: ChannelOptions =
this.options && this.options.channelOptions
? this.options.channelOptions
: {};
if (this.options && this.options.maxSendMessageLength) {
channelOptions['grpc.max_send_message_length'] =
this.options.maxSendMessageLength;
}
if (this.options && this.options.maxReceiveMessageLength) {
channelOptions['grpc.max_receive_message_length'] =
this.options.maxReceiveMessageLength;
}
if (this.options && this.options.maxMetadataSize) {
channelOptions['grpc.max_metadata_size'] = this.options.maxMetadataSize;
}
const keepaliveOptions = this.getKeepaliveOptions();
const options: Record<string, string | number> = {
...(this.options.channelOptions || {}),
...maxMessageLengthOptions,
...channelOptions,
...keepaliveOptions,
};