diff --git a/call.go b/call.go index 73f1e7f7..f6c63994 100644 --- a/call.go +++ b/call.go @@ -127,7 +127,7 @@ func sendRequest(ctx context.Context, dopts dialOptions, compressor Compressor, return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)") } if len(outBuf) > *c.maxSendMessageSize { - return Errorf(codes.ResourceExhausted, "Sent message larger than max (%d vs. %d)", len(outBuf), *c.maxSendMessageSize) + return Errorf(codes.ResourceExhausted, "grpc: trying to send message larger than max (%d vs. %d)", len(outBuf), *c.maxSendMessageSize) } err = t.Write(stream, outBuf, opts) if err == nil && outPayload != nil { diff --git a/clientconn.go b/clientconn.go index a502d338..9359366d 100644 --- a/clientconn.go +++ b/clientconn.go @@ -126,9 +126,9 @@ func WithInitialConnWindowSize(s int32) DialOption { } } -// WithMaxMsgSize returns a DialOption which sets the maximum message size the client can receive. Deprecated: use WithMaxReceiveMessageSize instead. +// WithMaxMsgSize returns a DialOption which sets the maximum message size the client can receive. Deprecated: use WithDefaultCallOptions(MaxCallRecvMsgSize(s)) instead. func WithMaxMsgSize(s int) DialOption { - return WithDefaultCallOptions(WithMaxReceiveMessageSize(s)) + return WithDefaultCallOptions(MaxCallRecvMsgSize(s)) } // WithDefaultCallOptions returns a DialOption which sets the default CallOptions for calls over the connection. diff --git a/rpc_util.go b/rpc_util.go index 4a4ca409..00b08337 100644 --- a/rpc_util.go +++ b/rpc_util.go @@ -219,16 +219,16 @@ func FailFast(failFast bool) CallOption { }) } -// WithMaxReceiveMessageSize returns a CallOption which sets the maximum message size the client can receive. -func WithMaxReceiveMessageSize(s int) CallOption { +// MaxCallRecvMsgSize returns a CallOption which sets the maximum message size the client can receive. +func MaxCallRecvMsgSize(s int) CallOption { return beforeCall(func(o *callInfo) error { o.maxReceiveMessageSize = &s return nil }) } -// WithMaxSendMessageSize returns a CallOption which sets the maximum message size the client can send. -func WithMaxSendMessageSize(s int) CallOption { +// MaxCallSendMsgSize returns a CallOption which sets the maximum message size the client can send. +func MaxCallSendMsgSize(s int) CallOption { return beforeCall(func(o *callInfo) error { o.maxSendMessageSize = &s return nil @@ -289,7 +289,7 @@ func (p *parser) recvMsg(maxReceiveMessageSize int) (pf payloadFormat, msg []byt return pf, nil, nil } if length > uint32(maxReceiveMessageSize) { - return 0, nil, Errorf(codes.ResourceExhausted, "grpc: Received message larger than max (%d vs. %d)", length, maxReceiveMessageSize) + return 0, nil, Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", length, maxReceiveMessageSize) } // TODO(bradfitz,zhaoq): garbage. reuse buffer after proto decoding instead // of making it for each message: @@ -393,7 +393,7 @@ func recv(p *parser, c Codec, s *transport.Stream, dc Decompressor, m interface{ if len(d) > maxReceiveMessageSize { // TODO: Revisit the error code. Currently keep it consistent with java // implementation. - return Errorf(codes.ResourceExhausted, "grpc: Received message larger than max (%d vs. %d)", len(d), maxReceiveMessageSize) + return Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", len(d), maxReceiveMessageSize) } if err := c.Unmarshal(d, m); err != nil { return Errorf(codes.Internal, "grpc: failed to unmarshal the received message %v", err) diff --git a/server.go b/server.go index 5807daee..62f4149a 100644 --- a/server.go +++ b/server.go @@ -131,7 +131,10 @@ type options struct { initialConnWindowSize int32 } -var defaultServerOptions = options{maxReceiveMessageSize: defaultServerMaxReceiveMessageSize, maxSendMessageSize: defaultServerMaxSendMessageSize} +var defaultServerOptions = options{ + maxReceiveMessageSize: defaultServerMaxReceiveMessageSize, + maxSendMessageSize: defaultServerMaxSendMessageSize, +} // A ServerOption sets options such as credentials, codec and keepalive parameters, etc. type ServerOption func(*options) @@ -187,23 +190,23 @@ func RPCDecompressor(dc Decompressor) ServerOption { } } -// MaxMsgSize returns a ServerOption to set the max message size in bytes for inbound mesages. -// If this is not set, gRPC uses the default limit. Deprecated: use MaxReceiveMessageSize instead. +// MaxMsgSize returns a ServerOption to set the max message size in bytes the server can receive. +// If this is not set, gRPC uses the default limit. Deprecated: use MaxRecvMsgSize instead. func MaxMsgSize(m int) ServerOption { - return MaxReceiveMessageSize(m) + return MaxRecvMsgSize(m) } -// MaxReceiveMessageSize returns a ServerOption to set the max message size in bytes for inbound mesages. +// MaxRecvMsgSize returns a ServerOption to set the max message size in bytes the server can receive. // If this is not set, gRPC uses the default 4MB. -func MaxReceiveMessageSize(m int) ServerOption { +func MaxRecvMsgSize(m int) ServerOption { return func(o *options) { o.maxReceiveMessageSize = m } } -// MaxSendMessageSize returns a ServerOption to set the max message size in bytes for outbound mesages. +// MaxSendMsgSize returns a ServerOption to set the max message size in bytes the server can send. // If this is not set, gRPC uses the default 4MB. -func MaxSendMessageSize(m int) ServerOption { +func MaxSendMsgSize(m int) ServerOption { return func(o *options) { o.maxSendMessageSize = m } @@ -669,7 +672,7 @@ func (s *Server) sendResponse(t transport.ServerTransport, stream *transport.Str grpclog.Fatalf("grpc: Server failed to encode response %v", err) } if len(p) > s.opts.maxSendMessageSize { - return status.Errorf(codes.ResourceExhausted, "Sent message larger than max (%d vs. %d)", len(p), s.opts.maxSendMessageSize) + return status.Errorf(codes.ResourceExhausted, "grpc: trying to send message larger than max (%d vs. %d)", len(p), s.opts.maxSendMessageSize) } err = t.Write(stream, p, opts) if err == nil && outPayload != nil { @@ -773,7 +776,7 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport. if len(req) > s.opts.maxReceiveMessageSize { // TODO: Revisit the error code. Currently keep it consistent with // java implementation. - return status.Errorf(codes.ResourceExhausted, "Received message larger than max (%d vs. %d)", len(req), s.opts.maxReceiveMessageSize) + return status.Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", len(req), s.opts.maxReceiveMessageSize) } if err := s.opts.codec.Unmarshal(req, v); err != nil { return status.Errorf(codes.Internal, "grpc: error unmarshalling request: %v", err) diff --git a/stream.go b/stream.go index 38e03a51..a999037b 100644 --- a/stream.go +++ b/stream.go @@ -371,7 +371,7 @@ func (cs *clientStream) SendMsg(m interface{}) (err error) { return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)") } if len(out) > *cs.c.maxSendMessageSize { - return Errorf(codes.ResourceExhausted, "Sent message larger than max (%d vs. %d)", len(out), *cs.c.maxSendMessageSize) + return Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", len(out), *cs.c.maxSendMessageSize) } err = cs.t.Write(cs.s, out, &transport.Options{Last: false}) if err == nil && outPayload != nil { @@ -613,7 +613,7 @@ func (ss *serverStream) SendMsg(m interface{}) (err error) { return err } if len(out) > ss.maxSendMessageSize { - return Errorf(codes.ResourceExhausted, "Sent message larger than max (%d vs. %d)", len(out), ss.maxSendMessageSize) + return Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", len(out), ss.maxSendMessageSize) } if err := ss.t.Write(ss.s, out, &transport.Options{Last: false}); err != nil { return toRPCErr(err) diff --git a/test/end2end_test.go b/test/end2end_test.go index 5f72c79f..61ad8ee5 100644 --- a/test/end2end_test.go +++ b/test/end2end_test.go @@ -504,10 +504,10 @@ func (te *test) startServer(ts testpb.TestServiceServer) { sopts = append(sopts, grpc.MaxMsgSize(*te.maxMsgSize)) } if te.maxServerReceiveMsgSize != nil { - sopts = append(sopts, grpc.MaxReceiveMessageSize(*te.maxServerReceiveMsgSize)) + sopts = append(sopts, grpc.MaxRecvMsgSize(*te.maxServerReceiveMsgSize)) } if te.maxServerSendMsgSize != nil { - sopts = append(sopts, grpc.MaxSendMessageSize(*te.maxServerSendMsgSize)) + sopts = append(sopts, grpc.MaxSendMsgSize(*te.maxServerSendMsgSize)) } if te.tapHandle != nil { sopts = append(sopts, grpc.InTapHandle(te.tapHandle)) @@ -610,10 +610,10 @@ func (te *test) clientConn() *grpc.ClientConn { opts = append(opts, grpc.WithMaxMsgSize(*te.maxMsgSize)) } if te.maxClientReceiveMsgSize != nil { - opts = append(opts, grpc.WithDefaultCallOptions(grpc.WithMaxReceiveMessageSize(*te.maxClientReceiveMsgSize))) + opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(*te.maxClientReceiveMsgSize))) } if te.maxClientSendMsgSize != nil { - opts = append(opts, grpc.WithDefaultCallOptions(grpc.WithMaxSendMessageSize(*te.maxClientSendMsgSize))) + opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(*te.maxClientSendMsgSize))) } switch te.e.security { case "tls":