Fix default max message length values

This commit is contained in:
Michael Lumish 2020-04-09 10:45:57 -07:00
parent 9221fdea24
commit 4bb965d2d8
4 changed files with 24 additions and 24 deletions

View File

@ -40,3 +40,9 @@ export enum LogVerbosity {
INFO,
ERROR,
}
// -1 means unlimited
export const DEFAULT_MAX_SEND_MESSAGE_LENGTH = -1;
// 4 MB default
export const DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH = 4 * 1024 * 1024;

View File

@ -17,15 +17,12 @@
import { BaseFilter, Filter, FilterFactory } from "./filter";
import { Call, WriteObject } from "./call-stream";
import { Status } from "./constants";
import { Status, DEFAULT_MAX_SEND_MESSAGE_LENGTH, DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH } from "./constants";
import { ChannelOptions } from "./channel-options";
// The default max message size for sending or receiving is 4 MB
const DEFAULT_MAX_MESSAGE_SIZE = 4 * 1024 * 1024;
export class MaxMessageSizeFilter extends BaseFilter implements Filter {
private maxSendMessageSize: number = DEFAULT_MAX_MESSAGE_SIZE;
private maxReceiveMessageSize: number = DEFAULT_MAX_MESSAGE_SIZE;
private maxSendMessageSize: number = DEFAULT_MAX_SEND_MESSAGE_LENGTH;
private maxReceiveMessageSize: number = DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH;
constructor(
private readonly options: ChannelOptions,
private readonly callStream: Call

View File

@ -20,7 +20,7 @@ import * as http2 from 'http2';
import { Duplex, Readable, Writable } from 'stream';
import { StatusObject } from './call-stream';
import { Status } from './constants';
import { Status, DEFAULT_MAX_SEND_MESSAGE_LENGTH, DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH } from './constants';
import { Deserialize, Serialize } from './make-client';
import { Metadata } from './metadata';
import { StreamDecoder } from './stream-decoder';
@ -326,9 +326,6 @@ export type HandlerType = 'bidi' | 'clientStream' | 'serverStream' | 'unary';
const noopTimer: NodeJS.Timer = setTimeout(() => {}, 0);
// The default max message size for sending or receiving is 4 MB
const DEFAULT_MAX_MESSAGE_SIZE = 4 * 1024 * 1024;
// Internal class that wraps the HTTP2 request.
export class Http2ServerCallStream<
RequestType,
@ -342,8 +339,8 @@ export class Http2ServerCallStream<
private isPushPending = false;
private bufferedMessages: Array<Buffer | null> = [];
private messagesToPush: Array<RequestType | null> = [];
private maxSendMessageSize: number = DEFAULT_MAX_MESSAGE_SIZE;
private maxReceiveMessageSize: number = DEFAULT_MAX_MESSAGE_SIZE;
private maxSendMessageSize: number = DEFAULT_MAX_SEND_MESSAGE_LENGTH;
private maxReceiveMessageSize: number = DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH;
constructor(
private stream: http2.ServerHttp2Stream,

View File

@ -57,8 +57,12 @@ describe(`${anyGrpc.clientName} client -> ${anyGrpc.serverName} server`, functio
let client;
let port;
before(function(done) {
/* The default server has no limits on max message size to make those
* tests easier to write */
/* To make testing max message size enforcement easier, the we explicitly
* remove the limit on the size of messages the server can receive, and
* we expect that the size of messages it can send is unlimited by
* default. On the other side, we explicitly limit the size of messages
* the client can send to 4 MB, and we expect that the size of messages
* it can receive is limited to 4 MB by default */
interopServer.getServer(0, true, (err, serverObj) => {
if (err) {
done(err);
@ -71,13 +75,13 @@ describe(`${anyGrpc.clientName} client -> ${anyGrpc.serverName} server`, functio
const creds = grpc.credentials.createSsl(ca_data);
const options = {
'grpc.ssl_target_name_override': 'foo.test.google.fr',
'grpc.default_authority': 'foo.test.google.fr'
'grpc.default_authority': 'foo.test.google.fr',
'grpc.max_send_message_length': 4*1024*1024
};
client = new testProto.TestService(`localhost:${port}`, creds, options);
done();
}
}, {
'grpc.max_send_message_length': -1,
'grpc.max_receive_message_length': -1
});
});
@ -141,14 +145,12 @@ describe(`${anyGrpc.clientName} client -> ${anyGrpc.serverName} server`, functio
});
});
describe.only('max message size', function() {
// Note: the main server has these checks disabled
// A size that is larger than the default limit
const largeMessageSize = 32 * 1024 * 1024;
const largeMessageSize = 6 * 1024 * 1024;
const largeMessage = Buffer.alloc(largeMessageSize);
it('should get an error when sending a large message', function(done) {
done = multiDone(done, 2);
const unaryMessage = {payload: {body: largeMessage}}
console.log(client.unaryCall.requestSerialize(unaryMessage).length);
const unaryMessage = {payload: {body: largeMessage}};
client.unaryCall(unaryMessage, (error, result) => {
assert(error);
assert.strictEqual(error.code, grpc.status.RESOURCE_EXHAUSTED);
@ -228,7 +230,7 @@ describe(`${anyGrpc.clientName} client -> ${anyGrpc.serverName} server`, functio
});
});
});
describe('with a server with message size limits', function() {
describe('with a server with message size limits and a client without limits', function() {
let restrictedServer;
let restrictedServerClient;
before(function(done) {
@ -244,13 +246,12 @@ describe(`${anyGrpc.clientName} client -> ${anyGrpc.serverName} server`, functio
const options = {
'grpc.ssl_target_name_override': 'foo.test.google.fr',
'grpc.default_authority': 'foo.test.google.fr',
'grpc.max_send_message_length': -1,
'grpc.max_receive_message_length': -1
};
restrictedServerClient = new testProto.TestService(`localhost:${serverObj.port}`, creds, options);
done();
}
});
}, {'grpc.max_send_message_length': 4 * 1024 * 1024});
});
after(function() {
restrictedServer.forceShutdown();
@ -276,7 +277,6 @@ describe(`${anyGrpc.clientName} client -> ${anyGrpc.serverName} server`, functio
it('should get an error when requesting a large message', function(done) {
done = multiDone(done, 2);
restrictedServerClient.unaryCall({response_size: largeMessageSize}, (error, result) => {
console.log(result.payload.body.length);
assert(error);
assert.strictEqual(error.code, grpc.status.RESOURCE_EXHAUSTED);
done();