diff --git a/dialoptions.go b/dialoptions.go index 025cca665243..69f2f52d114b 100644 --- a/dialoptions.go +++ b/dialoptions.go @@ -76,7 +76,7 @@ type dialOptions struct { defaultServiceConfig *ServiceConfig // defaultServiceConfig is parsed from defaultServiceConfigRawJSON. defaultServiceConfigRawJSON *string resolvers []resolver.Builder - useBytesPoolForParser bool + useSharedRecvBuffers bool } // DialOption configures how we set up the connection. @@ -637,7 +637,7 @@ func WithResolvers(rs ...resolver.Builder) DialOption { }) } -// WithUseBytesPoolForParser returns a DialOption that specifies whether to use +// WithSharedRecvBuffers returns a DialOption that specifies whether to use // a bytes pool for parsing. Setting this to true will reduce the memory allocation // in the parser. // @@ -645,8 +645,8 @@ func WithResolvers(rs ...resolver.Builder) DialOption { // // Notice: This API is EXPERIMENTAL and may be changed or removed in a // later release. -func WithUseBytesPoolForParser(useBytesPoolForParser bool) DialOption { +func WithSharedRecvBuffers(ok bool) DialOption { return newFuncDialOption(func(o *dialOptions) { - o.useBytesPoolForParser = useBytesPoolForParser + o.useSharedRecvBuffers = ok }) } diff --git a/rpc_util.go b/rpc_util.go index 821489ad1bb3..007c62a2d10f 100644 --- a/rpc_util.go +++ b/rpc_util.go @@ -542,8 +542,8 @@ type parser struct { // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md header [5]byte - // useBytesPool indicates whether to use bytes pool to allocate - useBytesPool bool + // useSharedRecvBuffers indicates whether to use shared receive buffers. + useSharedRecvBuffers bool } // recvMsg reads a complete gRPC message from the stream. @@ -577,7 +577,7 @@ func (p *parser) recvMsg(maxReceiveMessageSize int) (pf payloadFormat, msg []byt if int(length) > maxReceiveMessageSize { return 0, nil, status.Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", length, maxReceiveMessageSize) } - if p.useBytesPool { + if p.useSharedRecvBuffers { msg = pool.Get(int(length)) } else { msg = make([]byte, int(length)) @@ -768,7 +768,7 @@ func recv(p *parser, c baseCodec, s *transport.Stream, dc Decompressor, m interf return status.Errorf(codes.Internal, "grpc: failed to unmarshal the received message: %v", err) } if payInfo != nil { - if p.useBytesPool { + if p.useSharedRecvBuffers { if len(buf) != 0 { payInfo.uncompressedBytes = make([]byte, len(buf)) copy(payInfo.uncompressedBytes, buf) @@ -777,7 +777,7 @@ func recv(p *parser, c baseCodec, s *transport.Stream, dc Decompressor, m interf payInfo.uncompressedBytes = buf } } - if p.useBytesPool { + if p.useSharedRecvBuffers { pool.Put(&buf) } return nil diff --git a/server.go b/server.go index a5bd2dbfac7a..0f320070c4dc 100644 --- a/server.go +++ b/server.go @@ -174,7 +174,7 @@ type serverOptions struct { maxHeaderListSize *uint32 headerTableSize *uint32 numServerWorkers uint32 - useBytesPoolForParser bool + useSharedRecvBuffers bool } var defaultServerOptions = serverOptions{ @@ -553,16 +553,16 @@ func NumStreamWorkers(numServerWorkers uint32) ServerOption { }) } -// UseBytesPoolForParser returns a ServerOption that sets whether to use a bytes pool +// SharedRecvBuffers returns a ServerOption that sets whether to use a bytes pool // for the parser. Setting this to true will reduce the memory allocation in the parser. // // # Experimental // // Notice: This API is EXPERIMENTAL and may be changed or removed in a // later release. -func UseBytesPoolForParser(useBytesPoolForParser bool) ServerOption { +func SharedRecvBuffers(ok bool) ServerOption { return newFuncServerOption(func(o *serverOptions) { - o.useBytesPoolForParser = useBytesPoolForParser + o.useSharedRecvBuffers = ok }) } diff --git a/stream.go b/stream.go index 57c709e89aec..b46e48f2c8c2 100644 --- a/stream.go +++ b/stream.go @@ -490,7 +490,7 @@ func (a *csAttempt) newStream() error { return toRPCErr(nse.Err) } a.s = s - a.p = &parser{r: s, useBytesPool: a.cs.cc.dopts.useBytesPoolForParser} + a.p = &parser{r: s, useSharedRecvBuffers: a.cs.cc.dopts.useSharedRecvBuffers} return nil } @@ -1249,7 +1249,7 @@ func newNonRetryClientStream(ctx context.Context, desc *StreamDesc, method strin return nil, err } as.s = s - as.p = &parser{r: s, useBytesPool: ac.dopts.useBytesPoolForParser} + as.p = &parser{r: s, useSharedRecvBuffers: ac.dopts.useSharedRecvBuffers} ac.incrCallsStarted() if desc != unaryStreamDesc { // Listen on cc and stream contexts to cleanup when the user closes the