-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Implement new Codec that uses mem.BufferSlice
instead of []byte
#7356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
100 commits
Select commit
Hold shift + click to select a range
12bf91d
Implement new Codec that uses [][]byte instead of []byte
PapaCharlie 5575dbc
Disable full materialization if the BufferSlice only has one buffer i…
PapaCharlie 3807e4b
Fix tests
PapaCharlie d9f1aa7
Add copyright
PapaCharlie abb4993
Complete migration by reading off wire in *Buffers
PapaCharlie eb442ed
More fixes
PapaCharlie 3578382
transport package mostly fixed!
PapaCharlie cf49cd1
Nuke `BufferProvider` interface
PapaCharlie 28f5651
Move to new `mem` package, wire in the BufferPool all the way down to…
PapaCharlie 842d3fc
Complete!
PapaCharlie d3863ec
Address vet
PapaCharlie 1ba7894
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie b4b467f
Always fully materialize the buffers for the binlog
PapaCharlie 466a0ce
First pass at attempting getting the leakcheck package at checking bu…
PapaCharlie afcd86a
Implement buffer leak check
PapaCharlie 710ee3d
More frees
PapaCharlie 80eab7c
Make referencing a buffer actually create a copy
PapaCharlie af0a194
Clear unread buffers on stream close
PapaCharlie e9deef0
Add ref in SendMsg
PapaCharlie 74ffdf4
Fix retries
PapaCharlie 527b644
Fix the retries?
PapaCharlie 5a1e22e
Drain stream to pass test
PapaCharlie 13efcff
Cleanups
PapaCharlie 3e7f3d4
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie ab24c18
Remove dial
PapaCharlie af4644b
Disable goroutine leak check, but never disable buffer leak check
PapaCharlie df282ce
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 3ef511e
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 017b72f
Add docs and address comments
PapaCharlie 978d448
Getting closer
PapaCharlie 9be25eb
Disable leakcheck
PapaCharlie 23975fc
Add TODO on leakcheck
PapaCharlie 25adf8a
Address comments
PapaCharlie 9962795
Address more comments
PapaCharlie 5ee8565
Clean up buffers when initial call succeeds
PapaCharlie c5ed951
More touchups
PapaCharlie a2a6add
Address comments
PapaCharlie b6894dd
Allow specific tests to skip leakcheck
PapaCharlie 37f1c30
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 9a877dd
Add ForceCodecV2 option
PapaCharlie be4d4ab
Introduce `mem` package
PapaCharlie 5624e37
Address comments
PapaCharlie 169406f
add a few unit tests for Buffer; fix docstring for NopBufferPool
easwars c029ea2
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 0889be4
Make BufferSlice.Reader take reference
PapaCharlie dace88f
Minor tweaks to BufferSlice.Reader
PapaCharlie 14c288c
Improve test
PapaCharlie ae308d4
more tests and minor touchups
easwars 01ac2c6
fix a bug in BufferSlice.Reader
easwars 1261d3e
add prefix to test names to match type
easwars 50bda24
handle review comments
easwars 4d0eaa5
handle more review comments from dfawley
easwars f6f8b48
Merge remote-tracking branch 'upstream/master' into pc/mem
PapaCharlie 5aa0784
Address comments
PapaCharlie 284c19c
Merge branch 'pc/mem' into pc/newcodec
PapaCharlie 0d79099
Update after rebase on pc/mem
PapaCharlie 2ee31ca
Rename test
PapaCharlie c2d9987
Merge branch 'pc/mem' into pc/newcodec
PapaCharlie 3a8cac5
Rename to onFree
PapaCharlie 462e031
Merge branch 'pc/mem' into pc/newcodec
PapaCharlie 30f191b
Fix mem test
PapaCharlie aaca195
Change to MaterializeToBuffer
PapaCharlie 89ca8a9
Merge branch 'pc/mem' into pc/newcodec
PapaCharlie 84c5817
Fix vet failure
PapaCharlie 87df011
Unbreak uint cast
PapaCharlie e6c6731
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 5cbb1d7
Reuse `Buffer` instances
PapaCharlie 6e2146e
Fix `prepareMsg`
PapaCharlie 152db3d
Make `BufferPool.Put` take a `*[]byte`
PapaCharlie 981a5dc
Hack hack hack!
PapaCharlie 8c40132
Implement flate.Reader to prevent wrapping
PapaCharlie 5074715
Refactor splitting
PapaCharlie aa33829
Run it with tags instead
PapaCharlie 19cc6b3
More touchups, tests are passing
PapaCharlie 2efbe0d
Make leaks informational instead of failures
PapaCharlie 35f35e4
Enable test failures with tag
PapaCharlie 606b9f2
Split more stuff
PapaCharlie 5672329
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 8898f13
First pass at object reduction
PapaCharlie 4949fc4
Getting closer
PapaCharlie d4b487a
Pool ref counters
PapaCharlie 4b0c81e
More fixes
PapaCharlie 44376be
No-copy BufferSlice
PapaCharlie ebffde3
Finish up the job!
PapaCharlie b82bf1f
Fix compile error on default tag
PapaCharlie c87426e
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 83d0322
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 87a408c
Remove tags
PapaCharlie aaa3f16
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 6c38c43
Fix leakcheck
PapaCharlie 189e620
Fix grpchttp2
PapaCharlie d557400
Fix vet
PapaCharlie a7fb1d6
Fix tests
PapaCharlie 299260c
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie 6ad4793
Address Doug's comments
PapaCharlie dab5e1e
rerun tests
PapaCharlie 7ff751b
Add coments
PapaCharlie bcb1b41
Fix grammar
PapaCharlie 04691be
Fix test
PapaCharlie 27f2187
Merge remote-tracking branch 'upstream/master' into pc/newcodec
PapaCharlie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* | ||
* Copyright 2024 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package encoding | ||
|
||
import ( | ||
"strings" | ||
|
||
"google.golang.org/grpc/mem" | ||
) | ||
|
||
// CodecV2 defines the interface gRPC uses to encode and decode messages. Note | ||
// that implementations of this interface must be thread safe; a CodecV2's | ||
// methods can be called from concurrent goroutines. | ||
type CodecV2 interface { | ||
// Marshal returns the wire format of v. The buffers in the returned | ||
// [mem.BufferSlice] must have at least one reference each, which will be freed | ||
// by gRPC when they are no longer needed. | ||
Marshal(v any) (out mem.BufferSlice, err error) | ||
// Unmarshal parses the wire format into v. Note that data will be freed as soon | ||
// as this function returns. If the codec wishes to guarantee access to the data | ||
// after this function, it must take its own reference that it frees when it is | ||
// no longer needed. | ||
Unmarshal(data mem.BufferSlice, v any) error | ||
// Name returns the name of the Codec implementation. The returned string | ||
// will be used as part of content type in transmission. The result must be | ||
// static; the result cannot change between calls. | ||
Name() string | ||
} | ||
|
||
var registeredV2Codecs = make(map[string]CodecV2) | ||
|
||
// RegisterCodecV2 registers the provided CodecV2 for use with all gRPC clients and | ||
// servers. | ||
// | ||
// The CodecV2 will be stored and looked up by result of its Name() method, which | ||
// should match the content-subtype of the encoding handled by the CodecV2. This | ||
// is case-insensitive, and is stored and looked up as lowercase. If the | ||
// result of calling Name() is an empty string, RegisterCodecV2 will panic. See | ||
// Content-Type on | ||
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for | ||
// more details. | ||
// | ||
// If both a Codec and CodecV2 are registered with the same name, the CodecV2 | ||
// will be used. | ||
// | ||
// NOTE: this function must only be called during initialization time (i.e. in | ||
// an init() function), and is not thread-safe. If multiple Codecs are | ||
// registered with the same name, the one registered last will take effect. | ||
func RegisterCodecV2(codec CodecV2) { | ||
if codec == nil { | ||
panic("cannot register a nil CodecV2") | ||
} | ||
if codec.Name() == "" { | ||
panic("cannot register CodecV2 with empty string result for Name()") | ||
} | ||
contentSubtype := strings.ToLower(codec.Name()) | ||
registeredV2Codecs[contentSubtype] = codec | ||
} | ||
|
||
// GetCodecV2 gets a registered CodecV2 by content-subtype, or nil if no CodecV2 is | ||
// registered for the content-subtype. | ||
// | ||
// The content-subtype is expected to be lowercase. | ||
func GetCodecV2(contentSubtype string) CodecV2 { | ||
return registeredV2Codecs[contentSubtype] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* | ||
* Copyright 2024 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package proto | ||
|
||
import ( | ||
"fmt" | ||
|
||
"google.golang.org/grpc/encoding" | ||
"google.golang.org/grpc/mem" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
func init() { | ||
encoding.RegisterCodecV2(&codecV2{}) | ||
} | ||
|
||
// codec is a CodecV2 implementation with protobuf. It is the default codec for | ||
// gRPC. | ||
type codecV2 struct{} | ||
|
||
var _ encoding.CodecV2 = (*codecV2)(nil) | ||
|
||
func (c *codecV2) Marshal(v any) (data mem.BufferSlice, err error) { | ||
vv := messageV2Of(v) | ||
if vv == nil { | ||
return nil, fmt.Errorf("proto: failed to marshal, message is %T, want proto.Message", v) | ||
} | ||
|
||
size := proto.Size(vv) | ||
if mem.IsBelowBufferPoolingThreshold(size) { | ||
buf, err := proto.Marshal(vv) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data = append(data, mem.SliceBuffer(buf)) | ||
} else { | ||
pool := mem.DefaultBufferPool() | ||
buf := pool.Get(size) | ||
if _, err := (proto.MarshalOptions{}).MarshalAppend((*buf)[:0], vv); err != nil { | ||
pool.Put(buf) | ||
return nil, err | ||
} | ||
data = append(data, mem.NewBuffer(buf, pool)) | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func (c *codecV2) Unmarshal(data mem.BufferSlice, v any) (err error) { | ||
vv := messageV2Of(v) | ||
if vv == nil { | ||
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v) | ||
} | ||
|
||
buf := data.MaterializeToBuffer(mem.DefaultBufferPool()) | ||
defer buf.Free() | ||
// TODO: Upgrade proto.Unmarshal to support mem.BufferSlice. Right now, it's not | ||
// really possible without a major overhaul of the proto package, but the | ||
// vtprotobuf library may be able to support this. | ||
return proto.Unmarshal(buf.ReadOnlyData(), vv) | ||
} | ||
|
||
func (c *codecV2) Name() string { | ||
return Name | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.