Skip to content

Commit

Permalink
The GrpcOptions attribute "channelOptions" was not used anymore (neve…
Browse files Browse the repository at this point in the history
…r read in the code). Therefore, declaring those ChannelOptions had no effect anymore and there was no way to configure specific gRPC settings & limits.

Also extended ChannelOptions with grpc-node.max_session_memory which is required when sending big messages (see grpc/grpc-node#1666).

Ensured backward compatibility by including size limits like grpc.max_send_message_length to the ChannelOptions
  • Loading branch information
sjkummer committed Oct 18, 2021
1 parent a96ceb4 commit 5ac3637
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
6 changes: 6 additions & 0 deletions packages/microservices/external/grpc-options.interface.ts
@@ -1,8 +1,13 @@
import {GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH} from "../constants";

/**
* An interface that contains options used when initializing a Channel instance.
* This listing is incomplete. Full reference: https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
*/
export interface ChannelOptions {
'grpc.max_send_message_length'?: number;
'grpc.max_receive_message_length'?: number;
'grpc.max_metadata_size'?: number;
'grpc.ssl_target_name_override'?: string;
'grpc.primary_user_agent'?: string;
'grpc.secondary_user_agent'?: string;
Expand All @@ -12,5 +17,6 @@ export interface ChannelOptions {
'grpc.initial_reconnect_backoff_ms'?: number;
'grpc.max_reconnect_backoff_ms'?: number;
'grpc.use_local_subchannel_pool'?: number;
'grpc-node.max_session_memory'?: number;
[key: string]: string | number | undefined;
}
31 changes: 11 additions & 20 deletions packages/microservices/server/server-grpc.ts
Expand Up @@ -19,6 +19,7 @@ import { InvalidProtoDefinitionException } from '../errors/invalid-proto-definit
import { CustomTransportStrategy, MessageHandler } from '../interfaces';
import { GrpcOptions } from '../interfaces/microservice-configuration.interface';
import { Server } from './server';
import {ChannelOptions} from "../external/grpc-options.interface";

let grpcPackage: any = {};
let grpcProtoLoaderPackage: any = {};
Expand Down Expand Up @@ -333,27 +334,17 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
}

public async createClient(): Promise<any> {
const grpcOptions = {
'grpc.max_send_message_length': this.getOptionsProp(
this.options,
'maxSendMessageLength',
GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH,
),
'grpc.max_receive_message_length': this.getOptionsProp(
this.options,
'maxReceiveMessageLength',
GRPC_DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH,
),
};
const maxMetadataSize = this.getOptionsProp(
this.options,
'maxMetadataSize',
-1,
);
if (maxMetadataSize > 0) {
grpcOptions['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 server = new grpcPackage.Server(grpcOptions);
const server = new grpcPackage.Server(channelOptions);
const credentials = this.getOptionsProp(this.options, 'credentials');

await new Promise((resolve, reject) => {
Expand Down

0 comments on commit 5ac3637

Please sign in to comment.