-
Notifications
You must be signed in to change notification settings - Fork 5.3k
grpc: utilities for inter-converting grpc::ByteBuffer and Buffer::Instance. #6732
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
19 commits
Select commit
Hold shift + click to select a range
42d4dcb
Add utilities for converting between grpc::ByteBuffer and Buffer::Ins…
jplevyak 6014cd4
Fix CI test: use-after-move.
jplevyak 88ef0fc
Fix typo.
jplevyak 253b584
Address comments.
jplevyak 5e0763a
Address comments.
jplevyak c54b9da
Address comments.
jplevyak 2860281
Address alignment comment.
jplevyak 542ea54
Address additional comments.
jplevyak 887f759
Move google grpc specific code under compile options.
jplevyak 5f941b4
Merge branch 'master' of https://github.com/envoyproxy/envoy into raw…
jplevyak 84e9709
Address comments.
jplevyak deb27a3
Fix release build.
jplevyak 8f398ef
Do not ignore the return value of posix_memalign.
jplevyak 406bd50
Remove unnecessary dependency.
jplevyak 377de24
Address comments by adding comments.
jplevyak 8a9b7f4
Address comment.
jplevyak 70ecea2
Address comments.
jplevyak 5d716ae
Merge branch 'master' of https://github.com/envoyproxy/envoy into raw…
jplevyak 1c87b72
Address comments.
jplevyak 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,108 @@ | ||
| #include "common/grpc/google_grpc_utils.h" | ||
|
|
||
| #include <atomic> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <string> | ||
|
|
||
| #include "common/buffer/buffer_impl.h" | ||
| #include "common/common/assert.h" | ||
| #include "common/common/empty_string.h" | ||
| #include "common/common/enum_to_int.h" | ||
| #include "common/common/fmt.h" | ||
| #include "common/common/macros.h" | ||
| #include "common/common/stack_array.h" | ||
| #include "common/common/utility.h" | ||
|
|
||
| #include "absl/strings/match.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Grpc { | ||
|
|
||
| struct BufferInstanceContainer { | ||
| BufferInstanceContainer(int ref_count, Buffer::InstancePtr&& buffer) | ||
| : ref_count_(ref_count), buffer_(std::move(buffer)) {} | ||
| std::atomic<uint32_t> ref_count_; // In case gPRC dereferences in a different threads. | ||
| Buffer::InstancePtr buffer_; | ||
|
|
||
| static void derefBufferInstanceContainer(void* container_ptr) { | ||
| auto container = static_cast<BufferInstanceContainer*>(container_ptr); | ||
| container->ref_count_--; | ||
| // This is safe because the ref_count_ is never incremented. | ||
| if (container->ref_count_ <= 0) { | ||
| delete container; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| grpc::ByteBuffer GoogleGrpcUtils::makeByteBuffer(Buffer::InstancePtr&& buffer_instance) { | ||
| if (!buffer_instance) { | ||
| return {}; | ||
| } | ||
| Buffer::RawSlice on_raw_slice; | ||
| // NB: we need to pass in >= 1 in order to get the real "n" (see Buffer::Instance for details). | ||
| const int n_slices = buffer_instance->getRawSlices(&on_raw_slice, 1); | ||
| if (n_slices <= 0) { | ||
| return {}; | ||
| } | ||
| auto* container = new BufferInstanceContainer{n_slices, std::move(buffer_instance)}; | ||
| if (n_slices == 1) { | ||
| grpc::Slice one_slice(on_raw_slice.mem_, on_raw_slice.len_, | ||
| &BufferInstanceContainer::derefBufferInstanceContainer, container); | ||
| return {&one_slice, 1}; | ||
| } | ||
| STACK_ARRAY(many_raw_slices, Buffer::RawSlice, n_slices); | ||
jplevyak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| container->buffer_->getRawSlices(many_raw_slices.begin(), n_slices); | ||
| std::vector<grpc::Slice> slices; | ||
| slices.reserve(n_slices); | ||
| for (int i = 0; i < n_slices; i++) { | ||
| slices.emplace_back(many_raw_slices[i].mem_, many_raw_slices[i].len_, | ||
| &BufferInstanceContainer::derefBufferInstanceContainer, container); | ||
| } | ||
| return {&slices[0], slices.size()}; | ||
| } | ||
|
|
||
| struct ByteBufferContainer { | ||
| ByteBufferContainer(int ref_count) : ref_count_(ref_count) {} | ||
| ~ByteBufferContainer() { ::free(fragments_); } | ||
| uint32_t ref_count_; | ||
| Buffer::BufferFragmentImpl* fragments_ = nullptr; | ||
| std::vector<grpc::Slice> slices_; | ||
| }; | ||
|
|
||
| Buffer::InstancePtr GoogleGrpcUtils::makeBufferInstance(const grpc::ByteBuffer& byte_buffer) { | ||
| auto buffer = std::make_unique<Buffer::OwnedImpl>(); | ||
| if (byte_buffer.Length() == 0) { | ||
| return buffer; | ||
| } | ||
| // NB: ByteBuffer::Dump moves the data out of the ByteBuffer so we need to ensure that the | ||
| // lifetime of the Slice(s) exceeds our Buffer::Instance. | ||
| std::vector<grpc::Slice> slices; | ||
| byte_buffer.Dump(&slices); | ||
| auto* container = new ByteBufferContainer(static_cast<int>(slices.size())); | ||
| std::function<void(const void*, size_t, const Buffer::BufferFragmentImpl*)> releaser = | ||
| [container](const void*, size_t, const Buffer::BufferFragmentImpl*) { | ||
| container->ref_count_--; | ||
| if (container->ref_count_ <= 0) { | ||
| delete container; | ||
| } | ||
| }; | ||
| // NB: addBufferFragment takes a pointer alias to the BufferFragmentImpl which is passed in so we | ||
| // need to ensure that the lifetime of those objects exceeds that of the Buffer::Instance. | ||
| RELEASE_ASSERT(!::posix_memalign(reinterpret_cast<void**>(&container->fragments_), | ||
jplevyak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| alignof(Buffer::BufferFragmentImpl), | ||
| sizeof(Buffer::BufferFragmentImpl) * slices.size()), | ||
| "posix_memalign failure"); | ||
| for (size_t i = 0; i < slices.size(); i++) { | ||
| new (&container->fragments_[i]) | ||
jplevyak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Buffer::BufferFragmentImpl(slices[i].begin(), slices[i].size(), releaser); | ||
| } | ||
| for (size_t i = 0; i < slices.size(); i++) { | ||
| buffer->addBufferFragment(container->fragments_[i]); | ||
| } | ||
| container->slices_ = std::move(slices); | ||
| return buffer; | ||
| } | ||
|
|
||
| } // namespace Grpc | ||
| } // namespace Envoy | ||
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,33 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
|
|
||
| #include "envoy/buffer/buffer.h" | ||
|
|
||
| #include "grpcpp/grpcpp.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Grpc { | ||
|
|
||
| class GoogleGrpcUtils { | ||
| public: | ||
| /** | ||
| * Build grpc::ByteBuffer which aliases the data in a Buffer::InstancePtr. | ||
| * @param buffer source data container. | ||
| * @return byteBuffer target container aliased to the data in Buffer::Instance and owning the | ||
| * Buffer::Instance. | ||
| */ | ||
| static grpc::ByteBuffer makeByteBuffer(Buffer::InstancePtr&& buffer); | ||
|
|
||
| /** | ||
| * Build Buffer::Instance which aliases the data in a grpc::ByteBuffer. | ||
| * @param buffer source data container. | ||
| * @return a Buffer::InstancePtr aliased to the data in the provided grpc::ByteBuffer and | ||
| * owning the corresponding grpc::Slice(s). | ||
| */ | ||
| static Buffer::InstancePtr makeBufferInstance(const grpc::ByteBuffer& buffer); | ||
| }; | ||
|
|
||
| } // namespace Grpc | ||
| } // namespace Envoy |
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
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.