Skip to content

Commit

Permalink
Don't create buffer if compressor is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
iamqizhao committed Jan 25, 2016
1 parent 61d7c39 commit 4258b32
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
6 changes: 5 additions & 1 deletion call.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func sendRequest(ctx context.Context, codec Codec, compressor Compressor, callHd
}
}
}()
outBuf, err := encode(codec, args, compressor, new(bytes.Buffer))
var cbuf *bytes.Buffer
if compressor != nil {
cbuf = new(bytes.Buffer)
}
outBuf, err := encode(codec, args, compressor, cbuf)
if err != nil {
return nil, transport.StreamErrorf(codes.Internal, "grpc: %v", err)
}
Expand Down
9 changes: 8 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,11 @@ func (s *Server) Serve(lis net.Listener) error {
}

func (s *Server) sendResponse(t transport.ServerTransport, stream *transport.Stream, msg interface{}, cp Compressor, opts *transport.Options) error {
p, err := encode(s.opts.codec, msg, cp, new(bytes.Buffer))
var cbuf *bytes.Buffer
if cp != nil {
cbuf = new(bytes.Buffer)
}
p, err := encode(s.opts.codec, msg, cp, cbuf)
if err != nil {
// This typically indicates a fatal issue (e.g., memory
// corruption or hardware faults) the application program
Expand Down Expand Up @@ -457,6 +461,9 @@ func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transp
dg: s.opts.dg,
trInfo: trInfo,
}
if cp != nil {
ss.cbuf = new(bytes.Buffer)
}
if trInfo != nil {
trInfo.tr.LazyLog(&trInfo.firstLine, false)
defer func() {
Expand Down
24 changes: 18 additions & 6 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
dg: cc.dopts.dg,
tracing: EnableTracing,
}
if cp != nil {
callHdr.SendCompress = cp.Type()
cs.cbuf = new(bytes.Buffer)
}
if cs.tracing {
cs.trInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
cs.trInfo.firstLine.client = true
Expand Down Expand Up @@ -164,7 +168,7 @@ type clientStream struct {
desc *StreamDesc
codec Codec
cp Compressor
cbuf bytes.Buffer
cbuf *bytes.Buffer
dg DecompressorGenerator

tracing bool // set to EnableTracing when the clientStream is created.
Expand Down Expand Up @@ -211,8 +215,12 @@ func (cs *clientStream) SendMsg(m interface{}) (err error) {
}
err = toRPCErr(err)
}()
out, err := encode(cs.codec, m, cs.cp, &cs.cbuf)
defer cs.cbuf.Reset()
out, err := encode(cs.codec, m, cs.cp, cs.cbuf)
defer func() {
if cs.cbuf != nil {
cs.cbuf.Reset()
}
}()
if err != nil {
return transport.StreamErrorf(codes.Internal, "grpc: %v", err)
}
Expand Down Expand Up @@ -326,7 +334,7 @@ type serverStream struct {
codec Codec
cp Compressor
dg DecompressorGenerator
cbuf bytes.Buffer
cbuf *bytes.Buffer
statusCode codes.Code
statusDesc string
trInfo *traceInfo
Expand Down Expand Up @@ -365,8 +373,12 @@ func (ss *serverStream) SendMsg(m interface{}) (err error) {
ss.mu.Unlock()
}
}()
out, err := encode(ss.codec, m, ss.cp, &ss.cbuf)
defer ss.cbuf.Reset()
out, err := encode(ss.codec, m, ss.cp, ss.cbuf)
defer func() {
if ss.cbuf != nil {
ss.cbuf.Reset()
}
}()
if err != nil {
err = transport.StreamErrorf(codes.Internal, "grpc: %v", err)
return err
Expand Down

0 comments on commit 4258b32

Please sign in to comment.