Skip to content

Commit

Permalink
PATCH V2
Browse files Browse the repository at this point in the history
  • Loading branch information
Adhityaa Chandrasekar committed Nov 19, 2019
1 parent 10ad7f1 commit ea18dad
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 29 deletions.
6 changes: 2 additions & 4 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ type baseCodec interface {
Unmarshal(data []byte, v interface{}) error
}

// A bufferedBaseCodec is exactly like a baseCodec, but also requires a
// A reusableBaseCodec is exactly like a baseCodec, but also requires a
// ReturnBuffer method to be implemented. Once a Marshal caller is done with
// the returned byte buffer, they can choose to return it back to the encoding
// library for re-use using this method.
//
// This API is EXPERIMENTAL.
type bufferedBaseCodec interface {
type reusableBaseCodec interface {
baseCodec
// If implemented in a codec, this function may be called with the byte
// buffer returned by Marshal after gRPC is done with the buffer.
Expand Down
23 changes: 5 additions & 18 deletions encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func GetCompressor(name string) Compressor {
// Codec defines the interface gRPC uses to encode and decode messages. Note
// that implementations of this interface must be thread safe; a Codec's
// methods can be called from concurrent goroutines.
//
// Optionally, if a ReturnBuffer(buf []byte) is implemented, it may be called
// to return the byte slice it received from the Marshal function after gRPC is
// done with it. The codec may reuse this byte slice in a future Marshal
// operation to reduce the application's memory footprint.
type Codec interface {
// Marshal returns the wire format of v.
Marshal(v interface{}) ([]byte, error)
Expand All @@ -86,24 +91,6 @@ type Codec interface {
Name() string
}

// A BufferedCodec is exactly like a Codec, but also requires a ReturnBuffer
// method to be implemented. Once a Marshal caller is done with the returned
// byte buffer, they can choose to return it back to the encoding library for
// re-use using this method.
//
// This API is EXPERIMENTAL.
type BufferedCodec interface {
Codec
// If implemented in a codec, this function may be called with the byte
// buffer returned by Marshal after gRPC is done with the buffer.
//
// gRPC will not call ReturnBuffer after it's done with the buffer if any of
// the following is true:
// 1. Stats handlers are used.
// 2. Binlogs are enabled.
ReturnBuffer(buf []byte)
}

var registeredCodecs = make(map[string]Codec)

// RegisterCodec registers the provided Codec for use with all gRPC clients and
Expand Down
4 changes: 2 additions & 2 deletions preloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (p *PreparedMsg) Encode(s Stream, msg interface{}) error {
return err
}
p.encodedData = data
if len(data) >= bufferReuseThreshold {
if bcodec, ok := rpcInfo.preloaderInfo.codec.(bufferedBaseCodec); ok {
if cap(data) >= bufferReuseThreshold {
if bcodec, ok := rpcInfo.preloaderInfo.codec.(reusableBaseCodec); ok {
p.returnBuffer = func() {
bcodec.ReturnBuffer(data)
}
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ func (s *Server) sendResponse(t transport.ServerTransport, stream *transport.Str

var f func()
if attemptBufferReuse && len(data) >= bufferReuseThreshold {
if bcodec, ok := codec.(bufferedBaseCodec); ok {
if bcodec, ok := codec.(reusableBaseCodec); ok {
f = func() {
bcodec.ReturnBuffer(data)
}
Expand Down
4 changes: 2 additions & 2 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -1623,8 +1623,8 @@ func prepareMsg(m interface{}, codec baseCodec, cp Compressor, comp encoding.Com
return nil, nil, nil, nil, err
}

if attemptBufferReuse && len(data) >= bufferReuseThreshold {
if bcodec, ok := codec.(bufferedBaseCodec); ok {
if attemptBufferReuse && cap(data) >= bufferReuseThreshold {
if bcodec, ok := codec.(reusableBaseCodec); ok {
returnBuffer = func() {
bcodec.ReturnBuffer(data)
}
Expand Down
4 changes: 2 additions & 2 deletions vet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ fi
# TODO(dfawley): don't use deprecated functions in examples or first-party
# plugins.
SC_OUT="$(mktemp)"
staticcheck -go 1.9 -checks 'inherit,-ST1015' ./... > "${SC_OUT}" || true
staticcheck -go 1.9 -checks 'inherit,-ST1015,-SA6002' ./... > "${SC_OUT}" || true
# Error if anything other than deprecation warnings are printed.
(! grep -Pv "\((SA1019)|(SA6002)\)$" "${SC_OUT}")
(! grep -v "is deprecated:.*SA1019" "${SC_OUT}")
# Only ignore the following deprecated types/fields/functions.
(! grep -Fv '.HandleResolvedAddrs
.HandleSubConnStateChange
Expand Down

0 comments on commit ea18dad

Please sign in to comment.