Skip to content

Commit

Permalink
rpc_util: Rename the option to useSharedRecvBuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
hueypark committed Feb 25, 2023
1 parent f9730dd commit e746250
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions dialoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -637,16 +637,16 @@ 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.
//
// # Experimental
//
// 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
})
}
10 changes: 5 additions & 5 deletions rpc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ type serverOptions struct {
maxHeaderListSize *uint32
headerTableSize *uint32
numServerWorkers uint32
useBytesPoolForParser bool
useSharedRecvBuffers bool
}

var defaultServerOptions = serverOptions{
Expand Down Expand Up @@ -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
})
}

Expand Down
4 changes: 2 additions & 2 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e746250

Please sign in to comment.